Hello!
Im working on an assignment, but are having trouble with some basic code. The goal is to have six LEDs cycling through phases, like traffic light. and the button should reset the cycling back to phase one. But im not getting any reaction while pressing the button. Can anyone help?
Also, how can i limit the println to only write the phases once as they occur, and not to-fold times?
unsigned long current;
unsigned long previous = 0;
int switchState = 0;
void setup() {
Serial.begin(9600);
pinMode(2, INPUT); // bryter
pinMode(3, OUTPUT); // Rød nord/syd
pinMode(4, OUTPUT); // Gul nord/syd
pinMode(5, OUTPUT); // Grønn nord/syd
pinMode(6, OUTPUT); // Rød øst/vest
pinMode(7, OUTPUT); // Gul øst/vest
pinMode(8, OUTPUT); // Grønn øst/vest
}
void loop() {
switchState = digitalRead(2);
current = millis()/1000;
if (switchState == HIGH) {
current = 0; // reset time if button is pressed
}
while(switchState == LOW) {
current = millis()/1000;
if (current < 30) {
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
Serial.println("Fase 1");
}
else {
digitalWrite(5, LOW);
digitalWrite(6, LOW);
}
if (current >= 30 && current < 35) {
digitalWrite(4, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
Serial.println("Fase 2");
}
else {
digitalWrite(4, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
}
if (current >= 35 && current < 75) {
digitalWrite(3, HIGH);
digitalWrite(8, HIGH);
Serial.println("Fase 3");
}
else {
digitalWrite(3, LOW);
digitalWrite(8, LOW);
}
if (current >= 75 && current < 80) {
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(7, HIGH);
Serial.println("Fase 4");
}
else {
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(7, LOW);
}
if (current >= 80) {
current = 0; // start over
}
}
}
to the loop. Then the switch state is updated each time through the while loop.
But,
switchState = digitalRead(2);
current = millis()/1000;
if (switchState == HIGH) {
current = 0; // reset time if button is pressed
}
That code reads the state of the switch and tests for the button being pushed. There are only 2 states that the switch can have. Pushed and not pushed. So if that test fails there can be only one reason, the switch is not pushed. So you can replace the while with an else statement. The code then becomes; if the switch is pressed reset the timer, else run the sequence.
switchState = digitalRead(2);
current = millis() / 1000;
if (switchState == HIGH)
{
current = 0; // reset time if button is pressed
}
else
{
current = millis() / 1000;
if (current < 30)
{
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
Serial.println("Fase 1");
}
else
do you really want to execute the code with all the ifs only when the button in a particular state? don't you wan t that code to always be executed?
and in all those if/else, if/else, if/else ... isn't there really only one case you want executed? would you want to execute an if case that set the LEDs one way and then execute one or more else cases later that sets the LEDs differently
and don't you want the time condition you are comparing to to be something other than zero if you want this to work more than once
to the loop. Then the switch state is updated each time through the while loop.
But,
That code reads the state of the switch and tests for the button being pushed. There are only 2 states that the switch can have. Pushed and not pushed. So if that test fails there can be only one reason, the switch is not pushed. So you can replace the while with an else statement. The code then becomes; if the switch is pressed reset the timer, else run the sequence.
I tried this, but whenever i push the button, the code just pauses, it does not go back to scratch. hmm.
do you really want to execute the code with all the ifs only when the button in a particular state? don't you wan t that code to always be executed?
and in all those if/else, if/else, if/else ... isn't there really only one case you want executed? would you want to execute an if case that set the LEDs one way and then execute one or more else cases later that sets the LEDs differently
and don't you want the time condition you are comparing to to be something other than zero if you want this to work more than once
... for your assignment
Probably a good idea to get those conditions outside the if/else loops, but im too early in my "career" to know how.
Been trying for hours now, but Im getting nowhere... I want to figure it out by myself, but time is limited and not really seeing any progress on this assignment...
But I were wondering, maybe a switch statement sould be a better choice?