Finalizing coilDriver code
This commit is contained in:
parent
83020a8238
commit
8c2571f52a
@ -1,7 +1,18 @@
|
|||||||
#include <SoftwareSerial.h>
|
#include <SoftwareSerial.h>
|
||||||
|
|
||||||
|
#define coil1pin 10
|
||||||
|
#define coil2pin 12
|
||||||
|
#define coil3pin 11
|
||||||
|
|
||||||
|
#define swSerialRX 4
|
||||||
|
#define swSerialTX 5
|
||||||
|
|
||||||
|
#define sensor1pin 4
|
||||||
|
#define sensor2pin 3
|
||||||
|
#define sensor3pin 2
|
||||||
|
|
||||||
int impulsDelay = 1;
|
int impulsDelay = 1;
|
||||||
int impulsLenght = 100;
|
int impulsLength = 5;
|
||||||
int potValue = 1;
|
int potValue = 1;
|
||||||
|
|
||||||
unsigned long previouscoil2trigger = 0;
|
unsigned long previouscoil2trigger = 0;
|
||||||
@ -11,21 +22,30 @@ unsigned long lastdisplaywrite = 0;
|
|||||||
|
|
||||||
int ballspeed = 0;
|
int ballspeed = 0;
|
||||||
|
|
||||||
SoftwareSerial mySerial(4, 5); // RX, TX
|
SoftwareSerial mySerial(swSerialRX, swSerialTX); // RX, TX
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
// put your setup code here, to run once:
|
|
||||||
pinMode(7,OUTPUT);
|
// coil triggers
|
||||||
pinMode(8,OUTPUT);
|
|
||||||
pinMode(9,OUTPUT);
|
pinMode(coil1pin,OUTPUT);
|
||||||
digitalWrite(9,HIGH);
|
pinMode(coil2pin,OUTPUT);
|
||||||
digitalWrite(8,HIGH);
|
pinMode(coil3pin,OUTPUT);
|
||||||
digitalWrite(7,HIGH);
|
|
||||||
pinMode(2,INPUT_PULLUP);
|
// pulling them HIGH
|
||||||
pinMode(3,INPUT_PULLUP);
|
|
||||||
pinMode(10,INPUT_PULLUP);
|
digitalWrite(coil1pin,HIGH);
|
||||||
//attachInterrupt(digitalPinToInterrupt(2), impuls1, FALLING);
|
digitalWrite(coil2pin,HIGH);
|
||||||
//attachInterrupt(digitalPinToInterrupt(3), impuls2, FALLING);
|
digitalWrite(coil3pin,HIGH);
|
||||||
|
|
||||||
|
// ball sensors
|
||||||
|
|
||||||
|
pinMode(sensor1pin,INPUT_PULLUP);
|
||||||
|
pinMode(sensor2pin,INPUT_PULLUP);
|
||||||
|
pinMode(sensor3pin,INPUT_PULLUP);
|
||||||
|
|
||||||
|
// preparing Serial connections
|
||||||
|
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
Serial.flush();
|
Serial.flush();
|
||||||
mySerial.begin(4800);
|
mySerial.begin(4800);
|
||||||
@ -33,40 +53,42 @@ void setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
// put your main code here, to run repeatedly:
|
|
||||||
unsigned long currentMillis = millis();
|
unsigned long currentMillis = millis();
|
||||||
|
// getting the pulse length from the pot
|
||||||
potValue = analogRead(A0);
|
potValue = analogRead(A0);
|
||||||
impulsLenght = map(potValue, 0, 1023, 0, 100);
|
impulsLength = map(potValue, 0, 1023, 0, 100);
|
||||||
|
// sending data to the displayDriver every 1s
|
||||||
if (currentMillis - lastdisplaywrite >= 1000) {
|
if (currentMillis - lastdisplaywrite >= 1000) {
|
||||||
lastdisplaywrite = currentMillis;
|
lastdisplaywrite = currentMillis;
|
||||||
mySerial.print(impulsLenght);
|
mySerial.print(impulsLength);
|
||||||
mySerial.print(",");
|
mySerial.print(",");
|
||||||
mySerial.print(lastcoil2trigger-previouscoil2trigger);
|
mySerial.print(lastcoil2trigger-previouscoil2trigger);
|
||||||
mySerial.print('\n');
|
mySerial.print('\n');
|
||||||
}
|
}
|
||||||
if (digitalRead(2)) {
|
// checking if ball detected, if yes then fire the pulse
|
||||||
|
if (digitalRead(sensor1pin)) {
|
||||||
impuls1();
|
impuls1();
|
||||||
} else if (digitalRead(3)) {
|
} else if (digitalRead(sensor2pin)) {
|
||||||
impuls2();
|
impuls2();
|
||||||
} else if (digitalRead(10)) {
|
} else if (digitalRead(sensor3pin)) {
|
||||||
impuls3();
|
impuls3();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void impuls1() {
|
void impuls1() {
|
||||||
delay(impulsDelay);
|
delay(impulsDelay);
|
||||||
digitalWrite(7, LOW);
|
digitalWrite(coil1pin, LOW);
|
||||||
delay(impulsLenght);
|
delay(impulsLength);
|
||||||
digitalWrite(7, HIGH);
|
digitalWrite(coil1pin, HIGH);
|
||||||
Serial.println(millis());
|
Serial.println(millis());
|
||||||
delay(50);
|
delay(50);
|
||||||
}
|
}
|
||||||
|
|
||||||
void impuls2() {
|
void impuls2() {
|
||||||
delay(impulsDelay);
|
delay(impulsDelay);
|
||||||
digitalWrite(8, LOW);
|
digitalWrite(coil2pin, LOW);
|
||||||
delay(impulsLenght);
|
delay(impulsLength);
|
||||||
digitalWrite(8, HIGH);
|
digitalWrite(coil2pin, HIGH);
|
||||||
Serial.println(millis());
|
Serial.println(millis());
|
||||||
delay(50);
|
delay(50);
|
||||||
previouscoil2trigger = lastcoil2trigger;
|
previouscoil2trigger = lastcoil2trigger;
|
||||||
@ -75,9 +97,9 @@ void impuls2() {
|
|||||||
|
|
||||||
void impuls3() {
|
void impuls3() {
|
||||||
delay(impulsDelay);
|
delay(impulsDelay);
|
||||||
digitalWrite(9, LOW);
|
digitalWrite(coil3pin, LOW);
|
||||||
delay(impulsLenght);
|
delay(impulsLength);
|
||||||
digitalWrite(9, HIGH);
|
digitalWrite(coil3pin, HIGH);
|
||||||
Serial.println(millis());
|
Serial.println(millis());
|
||||||
delay(50);
|
delay(50);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user