0

Traffic light just stays on red rather than alternating. Wanted it to stay on for 10s then off for 10s, continuing ad infinitum. Dont want to use the delay function cos need to do other stuff while the LED continues to alternate. Thanks

int red = 10;  // red traffic light LED on pin 10
int redcounter;


// the setup routine runs once when you press reset:
void setup() 
{                
  // initialize the digital pin as an output.
  pinMode(red, OUTPUT);     

  digitalWrite(red, LOW);
  redcounter = 0;
}

// the loop routine runs over and over again forever:
void loop()
{
  redcounter = redcounter +1;
  if(redcounter==1000)
  {
     redcounter=0;
     if(digitalRead(red)==HIGH)
     {
       digitalWrite(red, LOW);
     }     
     if(digitalRead(red)==LOW)
     {
       digitalWrite(red, HIGH);
     } 
 }

3 Answers 3

1

You try to read a port which is configured as an OUTPUT. I don't know if this is supposed to work, but it would be more clear if you simply use another port as INPUT and feedback the signal you want to check in that port. I'm not sure however if it makes much sense to check the state of a signal you generate yourself (?). Moreover your redcounter is just "Active waiting", and arduino provides a delay function which does exactly that.

int red=10;
int signal=11;

void setup()
{                
    pinMode(red, OUTPUT);   
    pinMode(signal, INPUT);   
    digitalWrite(red, LOW);
}

void loop()
{
    delay(1000);
    if(digitalRead(signal)==HIGH)
    {
        digitalWrite(red, LOW);
    }      
    if(digitalRead(signal)==LOW)
    {
        digitalWrite(red, HIGH);
    }  
}
Sign up to request clarification or add additional context in comments.

Comments

0

Use elseif instead of if here:

 if(digitalRead(red)==HIGH)
 {
   digitalWrite(red, LOW);
 }     
 else if(digitalRead(red)==LOW)
 {
   digitalWrite(red, HIGH);
 } 

In your old solution every time red turned low, it was turned high a moment later.

6 Comments

have tried several values for 'if(redcounter==1000)' as well but no difference
Can you provide whole code? There's possibility that setup is not called, therefore redcounter is not initialised?
use redcounter>=1000 instead of redcounter==1000.
Whole code has been provided, Arduino automatically runs whatever is under the setup function first before running the loop
Good ideas but the LED still remains on
|
0

Two issues in your code are that digitalread will not read an output pin and if you use an increment counter you won't be able to accurately denote time. Sorry if I missed a bracket or something I was doing this on the mobile app. Use this:

int red = 10; // red traffic light LED on    pin 10
int redcounter;
boolean pinState = false;
int delayTime = 10000;

// the setup routine runs once when you press reset:
void setup() {
    // initialize the digital pin as an output.
    pinMode(red, OUTPUT);
    digitalWrite(red, LOW);
    redcounter = millis();
}

// the loop routine runs over and over again forever:
void loop() { 
    if((millis() - red counter) > delayTime) {
        redcounter=millis();

        if(pinState) {
            digitalWrite(red, LOW);
            pinState = false;
        }
        else {
            digitalWrite(red, HIGH);
            pinState = true;
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.