1
int timer = 1000;
int thispin = 5;

void setup() {
  for (int thispin = 5; thispin < 8; thispin++) 
    pinMode(thispin, OUTPUT);
}

void loop() {
  for (int thispin = 5; thispin < 8; thispin++) 
    digitalWrite(thispin, HIGH);

  delay(timer);
  digitalWrite(thispin, LOW);


  for (int thispin = 7; thispin = 5; thispin--) 
    digitalWrite(thispin, HIGH);

  delay(timer);
  digitalWrite(thispin, LOW);
}

I have the above code. I thought that how it was written would cause the led's to flash but they all stay on continuously. Could anybody help to explain what it is i have done wrong? I am very new to this and I'm just fiddling basically trying to learn. Thank you!

6
  • 1
    Added code tags and indentation - it should be more obvious what is done by loops and what is not executed repeatedly by loops. Also ; thispin = 5; ... is an assigment not comparation. – KIIV Oct 4 '20 at 17:33
  • you only set pin 5 LOW. it is the global thispin – Juraj Oct 4 '20 at 17:53
  • if you coded the program correctly, how long do you think that the low output would last after this line? ... digitalWrite(thispin, LOW); ... in your code, what is the value of thispin? – jsotola Oct 4 '20 at 21:20
  • please review the documentation of the for loop ... it does not work as you think – jsotola Oct 4 '20 at 21:23
  • A = won't work. You have to change it to ==. – Python Schlange Oct 5 '20 at 12:59
3

The objective of the code is not clear. It seems the idea is to repeatedly blink 3 LEDs, connected on pins 5,6 and 7. To do so, the code needs slight modification-

int timer = 1000;
int thispin = 5;

void setup() {
  
  for (int thispin = 5; thispin < 8; thispin++) 
    pinMode(thispin, OUTPUT);
}

void loop() {
  
  for (int thispin = 5; thispin < 8; thispin++) 
    digitalWrite(thispin, HIGH); 
 
    delay(timer); 

  
  for (int thispin = 5; thispin < 8; thispin++) 
    digitalWrite(thispin, LOW);

    delay(timer); 

}

Also, since you wanted to know why the LEDs stay ON continuously with your code- This is because there is no delay statement after any of the digitalWrite-LOW statements. If there's no delay, the LEDs may turn off for a fraction of a millisecond, which won't be noticeable.

1

From the given code it is not entirely clear what you are trying to achieve. If I understood correctly, you want the pins to light up for one second and then go out. First from 5 to 7, and then from 7 to 5. Try the given code.

int timer = 1000;

void setup() 
{
  for (int thispin = 5; thispin < 8; thispin++) 
    pinMode(thispin, OUTPUT);
}

void loop() 
{
  for (int thispin = 5; thispin < 8; thispin++)
  { 
    digitalWrite(thispin, HIGH);
    delay(timer);
    digitalWrite(thispin, LOW);
  }

  delay(timer);

  for (int thispin = 7; thispin >= 5; thispin--) 
  {
    digitalWrite(thispin, HIGH);
    delay(timer);
    digitalWrite(thispin, LOW);
  }
}

Also, using the same name for globals variable and loop iterators is bad style.

0

In the last for() loop, the limit expression, thispin =5; was probably meant to say thispin <=5;. As it is written, the expression is an assignment, not a test, and it will always be true, because 1) an assignment statement is also an expression having a value equal to the value assigned; and 2) the assigned value, '5', being non-zero, is equivalent to 'true'), so this loop will never end. thispin will get decremented forever and repeatedly set pin 5 to HIGH. It leaves the other pins alone, but they were already high, and the LEDs stay lit, which is what you observed.

(There are a couple of other errors, as noted earlier by previous responders, which will also prevent your code from operating as you expected.)

0

Exchange the last lines:

delay(timer);  
digitalWrite(thispin, LOW);

It is 2 seconds on and maybe a few hundred nanoseconds on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.