Hi, apologies if this is something I ought to understand but I've been trying everything to get the following code to work. Have inserted some Serial println lines to debug with the output to them included below the code. The problem is that once the loop has incremented the 'tiktok' timer to 14 it then increments to 9 rather than 15.
int in1 = 7; // motor to pin 7
const int buzzer = 9; //buzzer to arduino pin 9
int motState = HIGH; // motorstate used to set the motor high or low
unsigned long previousMillis = 0; // will store last time motor was updated
long OnTimeA = 2700; // milliseconds of on-time
long OnTimeB = 5200; // milliseconds of on-time
long OnTimeC = 15600; // milliseconds of on-time
long OffTime = 3300; // milliseconds of off-time
long tiktok;
char tune;
void setup() {
pinMode(in1, OUTPUT); // Set motor - pin 7 as an output
digitalWrite(in1, HIGH); // Set motor high by default
pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output
tiktok = 0;
Serial.begin(9600);
}
void loop() {
// check to see if it's time to change the state of the motor
unsigned long currentMillis = millis();
if((motState == LOW) && (currentMillis - previousMillis >= OnTimeA) &&
( (tiktok >= 1) && (tiktok <= 8) ||
(tiktok >= 10) && (tiktok <= 14) ||
(tiktok >= 16) && (tiktok <= 19)))
{
motState = HIGH; // Turn it off
previousMillis = currentMillis; // Remember the time
digitalWrite(in1, motState); // Update the actual motor
Serial.println(tiktok);
Serial.println(motState);
Serial.println(tune);
}
else if ((motState == HIGH) && (currentMillis - previousMillis >= OffTime) &&
( (tiktok >= 0) && (tiktok <= 7) ||
(tiktok >= 9) && (tiktok <= 13) ||
(tiktok >= 15) && (tiktok <= 18)))
{
char tune = 'A';
tone(buzzer, 100);
delay(500);
noTone(buzzer); // Stop sound...
motState = LOW; // turn it on
previousMillis = currentMillis; // Remember the time
digitalWrite(in1, motState); // Update the actual motor
++tiktok;
Serial.println(tiktok);
Serial.println(motState);
Serial.println(tune);
}
else if((motState == LOW) && (currentMillis - previousMillis >= OnTimeB) && ((tiktok = 9) ||
(tiktok = 15)))
{
motState = HIGH; // Turn it off
previousMillis = currentMillis; // Remember the time
digitalWrite(in1, motState); // Update the actual motor
Serial.println(tiktok);
Serial.println(motState);
Serial.println(tune);
}
else if ((motState == HIGH) && (currentMillis - previousMillis >= OffTime) && ((tiktok = 8) ||
(tiktok = 14)))
{
char tune = 'B';
tone(buzzer, 200);
delay(500);
noTone(buzzer);
motState = LOW; // turn it on
previousMillis = currentMillis; // Remember the time
digitalWrite(in1, motState); // Update the actual motor
++tiktok;
Serial.println(tiktok);
Serial.println(motState);
Serial.println(tune);
}
else if((motState == LOW) && (currentMillis - previousMillis >= OnTimeC) && (tiktok = 0))
{
motState = HIGH; // Turn it off
previousMillis = currentMillis; // Remember the time
digitalWrite(in1, motState); // Update the actual motor
Serial.println(tiktok);
Serial.println(motState);
Serial.println(tune);
}
else if ((motState == HIGH) && (currentMillis - previousMillis >= OffTime) && (tiktok = 19))
{
char tune = 'C';
tone(buzzer, 300);
delay(500);
noTone(buzzer);
motState = LOW; // turn it on
previousMillis = currentMillis; // Remember the time
digitalWrite(in1, motState); // Update the actual motor
tiktok = 0;
Serial.println(tiktok);
Serial.println(motState);
Serial.println(tune);
}
}
Serial output:
1
0
A
1
1
2
0
A
2
1
3
0
A
3
1
4
0
A
4
1
5
0
A
5
1
6
0
A
6
1
7
0
A
7
1
8
0
A
8
1
9
0
B
9
1
10
0
A
10
1
11
0
A
11
1
12
0
A
12
1
13
0
A
13
1
14
0
A
14
1
9 *******THIS SHOULD BE 15 ?********
0
B
9
1
10
0
A
10
1
11
0
A
11
1
Any help would be greatly appreciated.