Why does this not work??

Hello!!!

Why does this not work? After that I see this should work, but it’s not. Why??

int led1 = 3;
int led2 = 5;

void setup()
{
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  
  Serial.begin(9600);
}

void loop()
{
  func1();
  delay(10000);
  func2();
  delay(10000);
}

void func1()
{
  for (int x = 0; x < 255; x = x + 5)
  {
    int omv = map(x,0,255,255,0);
    analogWrite(led1,x);
    analogWrite(led2,omv);
    delay(19);
  }

  for (int x = 255; x > 0; x = x - 5)
  {
    int omv = map(x,0,255,255,0);
    analogWrite(led1,x);
    analogWrite(led2,omv);
    delay(19);
  }
}

void func2()
{
  for (int x = 0; x < 255; x = x + 5)
  {
    analogWrite(led1,x);
    analogWrite(led2,x);
    delay(19);
  }

  for (int x = 255; x > 0; x = x - 5)
  {
    analogWrite(led1,x);
    analogWrite(led2,x);
    delay(19);
  }
}

What should it do ?
What does it actually do ?

You see that I have "Void func1()" and "void func2()" That is my function 1 and function 2. In void loop the meaning is to do func1 in 10 second and then do func2 in 10 second.

It looks like it should fade one led up and the other down and then reverse that. Wait ten seconds doing nothing, the led on pin 5 will stay lit during the 10 second wait time.

After the ten seconds of nothing is done, it looks like func2() will fade them both up and back down again together. Then another 10 seconds of nothing happening only this time both leds will be off during the wait.

Does that sound anything like what you see? If not then we need to talk about how you have it hooked up.

HansDia:
Hello!!!

Why does this not work? After that I see this should work, but it’s not. Why??

There’s a mistake in your code. You have this:

for (int x = 0; x < 255; x = x + 5)
{
int omv = map(x,0,255,255,0);
analogWrite(led1,x);
analogWrite(led2,omv);
delay(19);
}

The part in red is wrong. It SHOULD be:

for (int x = 0; x < 255; x += 5)
{
int omv = map(x,0,255,255,0);
analogWrite(led1,x);
analogWrite(led2,omv);
delay(19);
}

Krupski:
for (int x = 0; x < 255; x = x + 5)
for (int x = 0; x < 255; x += 5)

These two are equivalent.

Also for (int x = 255; x > 0; x = x - 5) change to for (int x = 255; x > 0; x -= 5)

for (int x = 0; x < 255; x = x + 5)
for (int x = 0; x < 255; x += 5)
These two are equivalent.

No, These two are equivalent
for (int x = 0; x < 255; x = (x + 5))
for (int x = 0; x < 255; x += 5)

steinie44:

for (int x = 0; x < 255; x = x + 5)
for (int x = 0; x < 255; x += 5)
These two are equivalent.

No, These two are equivalent
for (int x = 0; x < 255; x = (x + 5))
for (int x = 0; x < 255; x += 5)

The other two are equivalent as well. Arithmetic operations takes precedence over assignment operations.

The LEDs get out of sync maybe because no omv in func2{} ?

There's a mistake in your code

It's not a mistake. Perfectly legal code and it will work as you would expect it to work.

It does not work properly, it should first dim led1 and LED2 up and down where LED1 and LED2 runs opposite of each other in 10 seconds as func1 () says it should. So after that they should dimmed liked each other up and down for 10 seconds as FUNC2 () says. Then start over.

Sorry for my bar english :)

It does not work properly, it should first dim led1 and LED2 up and down where LED1 and LED2 runs opposite of each other in 10 seconds as func1 () says it should. So after that they should dimmed liked each other up and down for 10 seconds as FUNC2 () says. Then start over.

That's what it's doing. As far as i can see.

:slight_smile:

steinie44:

for (int x = 0; x < 255; x = x + 5)
for (int x = 0; x < 255; x += 5)
These two are equivalent.

No, These two are equivalent
for (int x = 0; x < 255; x = (x + 5))
for (int x = 0; x < 255; x += 5)

There is no “no”, they are equivalent and the extra parentheses are unnecessary.

Also the map could/should be replaced by simple arithmetic.

HansDia: It does not work properly, it should first dim led1 and LED2 up and down where LED1 and LED2 runs opposite of each other in 10 seconds as func1 () says it should. So after that they should dimmed liked each other up and down for 10 seconds as FUNC2 () says. Then start over.

Sorry for my bar english :)

But what does it actually do ? How are the LEDs wired ?

HansDia:
It does not work properly, it should first dim led1 and LED2 up and down where LED1 and LED2 runs opposite of each other in 10 seconds as func1 () says it should. So after that they should dimmed liked each other up and down for 10 seconds as FUNC2 () says. Then start over.

Sorry for my bar english :slight_smile:

You used delay, so you are separating the two functions for 10 seconds rather than running them for 10s.

You can try this to see if it does what you want, alternating between the two functions for 10s…

int led1 = 3;
int led2 = 5;
unsigned long ledTimeStart;

void setup()
{
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  
  Serial.begin(9600);
  ledTimeStart = millis();
}

void loop()
{
  if (millis() - ledTimeStart <= 10000UL)
  {
    func1();
  }
  else if (millis() - ledTimeStart <= 20000UL)
  {
    func2();
  }
  else 
  {
    ledTimeStart = millis();
  }
}

void func1()
{
  for (int x = 0; x < 255; x = x + 5)
  {
    int omv = map(x,0,255,255,0);
    analogWrite(led1,x);
    analogWrite(led2,omv);
    delay(19);
  }

  for (int x = 255; x > 0; x = x - 5)
  {
    int omv = map(x,0,255,255,0);
    analogWrite(led1,x);
    analogWrite(led2,omv);
    delay(19);
  }
}

void func2()
{
  for (int x = 0; x < 255; x = x + 5)
  {
    analogWrite(led1,x);
    analogWrite(led2,x);
    delay(19);
  }

  for (int x = 255; x > 0; x = x - 5)
  {
    analogWrite(led1,x);
    analogWrite(led2,x);
    delay(19);
  }
}

also,
FYI this expression will decrement an LED through its entire range of briteness in increments of 5:

for (int x = 255; x >= 0; x = x - 5)

yours, will not because it will not turn the led completely off (i.e. x=0):

for (int x = 255; x > 0; x = x - 5)

look at your loops to see if you want to bring the LED to full briteness or zero briteness and modify the expressions to reflect what you would like to have.

Not critical, but worth noting.

Thanks BulldogLowell it works, my next step is to chose func with serial.read, when it serial read 1 it does func1, and when it serial.read 2 it does func2, how can I do that??

thanks!!

HansDia: Thanks BulldogLowell it works, my next step is to chose func with serial.read, when it serial read 1 it does func1, and when it serial.read 2 it does func2, how can I do that??

thanks!!

Read the serial data into a variable and use if statements to check its values.

HansDia: Thanks BulldogLowell it works, my next step is to chose func with serial.read, when it serial read 1 it does func1, and when it serial.read 2 it does func2, how can I do that??

thanks!!

Check out the Nick Gammon examples, particularly the state machine. It does a nice job on serial reads...