Go Down

Topic: Why its not working ? (Read 202 times) previous topic - next topic

Jmarcinho

Aug 18, 2018, 05:42 am Last Edit: Aug 18, 2018, 05:50 am by Jmarcinho
Hi everyone,

I`ve spent about three hours already trying to make it work, but with no success.
I mean to blink a led with a increasing and then decreasing delay rate, using delay.
So I wrote the following simple code, but led is blinking with a constant rate, looks like 1000ms.
What Am I doing wrong ?
Thanks a lot.

Code: [Select]
int led = 10;

void setup() {
    pinMode(led, OUTPUT);
}

void loop() {
  for (int i = 1000; i >= 50; i - 50){
    digitalWrite(led, HIGH);
    delay(i);
    digitalWrite(led, LOW);
    delay(i);   
  }
  for (int i = 50; i <= 1000; i + 50){
    digitalWrite(led, HIGH);
    delay(i);
    digitalWrite(led, LOW);
    delay(i);
  }
}

vaj4088

The third parameter in your for statements calculates the subtraction or addition of 50 from/to i AND THEN THROWS AWAY THE RESULT.

larryd

#2
Aug 18, 2018, 06:06 am Last Edit: Aug 18, 2018, 06:06 am by larryd
Hum?

Code: [Select]
int led = 10;

void setup() {
    pinMode(led, OUTPUT);
}

void loop() {
  for (int i = 1000; i >= 50; i = i - 50){
    digitalWrite(led, HIGH);
    delay(i);
    digitalWrite(led, LOW);
    delay(i);    
  }
  for (int i = 50; i <= 1000; i = i + 50){
    digitalWrite(led, HIGH);
    delay(i);
    digitalWrite(led, LOW);
    delay(i);
  }
}




No technical PMs.
The last thing you did is where you should start looking.

Silente

Do you have the same problem witch the last code posted?

bidouilleelec

Hello Jmarcinho,

You have two warnings from the compiler like:
Quote
C:\Users\Philippe10\Documents\Arduino\sketch_aug18a\sketch_aug18a.ino:9:33: warning: for increment expression has no effect [-Wunused-value]

   for (int i = 1000; i >= 50; i - 50){

                                 ^

C:\Users\Philippe10\Documents\Arduino\sketch_aug18a\sketch_aug18a.ino:15:34: warning: for increment expression has no effect [-Wunused-value]

   for (int i = 1000; i <= 200; i + 50){

                                  ^
See (example two)

file:///F:/Donn%C3%A9esM6/Programmes_M6/ArduinoMooc/Arduino/reference/www.arduino.cc/en/Reference/For.html

Code: [Select]
for (int i = 1000; i >= 50;  i - 50)

"i-50" is not a decrement instruction.
"i = i-50" is.

Regards,
bidouilleelec




Robin2

I`ve spent about three hours already trying to make it work,
Three hours is a very short amount of time for a programming problem :)

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Jmarcinho

Three hours is a very short amount of time for a programming problem :)

...R
Hehehe, but not for such a simple code. I guess.
Thanks

Jmarcinho

Hello Jmarcinho,

You have two warnings from the compiler like:
See (example two)

file:///F:/Donn%C3%A9esM6/Programmes_M6/ArduinoMooc/Arduino/reference/www.arduino.cc/en/Reference/For.html

Code: [Select]
for (int i = 1000; i >= 50;  i - 50)

"i-50" is not a decrement instruction.
"i = i-50" is.

Regards,
bidouilleelec

Thanks everyone. It`s working now. That was the problem.





Go Up