Hi,
I have 2 sets of code(diffrent arduino(both mega), different timing), both are starting to do things once a button is pressed. I want both leds to turn on and off at the same time each loop, the button is connected to the same pin(with shared ground).
Both leds are connected to pin no.3.
What happens in my code is that it starts coordinated but with time I get a certain drift, and don't understand why. I tried to use the button timing to set them to start again each loop.
This is the code, the second code is complelty identical besides the second delay which is 5 instead of 2000:
int buttonPin = 22;
unsigned long roundStart = 0;
void setup() {
Serial.begin(9600);
Serial.println("LEDS");
setupPins();
Serial.println("You can press now");
}
void loop() {
if (digitalRead(buttonPin) == LOW ) {
Serial.println("start: ");
digitalWrite(3,HIGH);
Serial.println(String(millis()));
roundStart = millis();
while(1) {
digitalWrite(3,HIGH);
delay(2000);
digitalWrite(3,LOW);
delay(2000);
Serial.println(String("time since beggening of session: " + String(millis() - roundStart)));
resetTimes();
roundStart = millis();
Serial.println("round start: " + String(roundStart));
}
}
}
bool resetTimes() {
static unsigned long reachTime = 10000;
static bool entered = false;
Serial.println("entering while loop " + String(millis()) + " " + String(roundStart + reachTime));
while (millis() < roundStart + reachTime) {
entered = true;
}
if (!entered) {
Serial.println("Didn't go in!!!");
}
entered = false;
Serial.println(String(millis() - roundStart) + " " + String(millis()));
return true;
}
void setupPins() {
pinMode(buttonPin, INPUT_PULLUP);
for (int i = 2 ; i < 16; i++ ) {
pinMode(i, OUTPUT);
digitalWrite(i, LOW);
}
for (int i = 44 ; i < 47; i++ ) {
pinMode(i, OUTPUT);
digitalWrite(i, LOW);
}
}
Code2:
int buttonPin = 22;
unsigned long roundStart = 0;
void setup() {
Serial.begin(9600);
Serial.println("LEDS");
setupPins();
Serial.println("You can press now");
}
void loop() {
if (digitalRead(buttonPin) == LOW ) {
Serial.println("start: ");
digitalWrite(3,HIGH);
Serial.println(String(millis()));
roundStart = millis();
while(1) {
digitalWrite(3,HIGH);
delay(2000);
digitalWrite(3,LOW);
delay(5);
Serial.println(String("time since beggening of session: " + String(millis() - roundStart)));
resetTimes();
roundStart = millis();
Serial.println("round start: " + String(roundStart));
}
}
}
bool resetTimes() {
static unsigned long reachTime = 10000;
static bool entered = false;
Serial.println("entering while loop " + String(millis()) + " " + String(roundStart + reachTime));
while (millis() < roundStart + reachTime) {
entered = true;
}
if (!entered) {
Serial.println("Didn't go in!!!");
}
entered = false;
Serial.println(String(millis() - roundStart) + " " + String(millis()));
return true;
}
void setupPins() {
pinMode(buttonPin, INPUT_PULLUP);
for (int i = 2 ; i < 16; i++ ) {
pinMode(i, OUTPUT);
digitalWrite(i, LOW);
}
for (int i = 44 ; i < 47; i++ ) {
pinMode(i, OUTPUT);
digitalWrite(i, LOW);
}
}
Thanks!