if and else statements not working with boolean

i have 2 values which fade from 0-255 and then 255-0 at the minute i can only get it to work so that

Value1 0-255
Value1 255-0
Value2 0-255
Value2 255-0

i would like it to do this so that as value1 hits 255 value2 starts its 0-255 run then 255-0 and this loops. so…

value1 0-255
value1 255-0 & value2 0-255
value2 255-0 &value1 0-255

I tried setting a boolean up to go true when value1 hit 255 and supposedly triggered the value2 run but it doesn’t work.

Any ideas?

int Pin1 = 9;         
int Pin2 = 10;           
int Value1;    
int Value2;    
boolean v1;
boolean v2;
int dt=10;


void setup()  
{
  Value1=0;
  Value2=0;

  v1=false;
  v2=false;

  pinMode(Pin1, OUTPUT);   
  pinMode(Pin2, OUTPUT);   

}

void loop()
{ 

  for(Value1 = 0 ; Value1 <= 255; Value1 ++) 
  {
    analogWrite(Pin1, Value1);          
    delay(dt);      
  } 

  for(Value1 = 255; Value1 >=0; Value1 --)   
  { 
    analogWrite(Pin1, Value1); 
    delay(dt); 
  } 


  if (Value1 ==255) {
    v1=true;
  }
  else {
    v1=false;
  }

  if (v1==true)                

  { 
    for(Value2 = 0 ; Value2 <= 255; Value2 ++) 
    { 
      analogWrite(Pin2, Value2);            
      delay(dt);                           
    } 
    for(Value2 = 255; Value2 >=0; Value2 --)    
    { 
      analogWrite(Pin2, Value2); 
      delay(dt); 


    }

  }

}

Your first for increments Value1 from 0 to 255. Your second for decrements Value1 from 255 to 0. How is Value1 supposed to be 255 when you hit the if statement?

Ah yes, I see what you mean, is there a way of making value1 increase and value2 decrease at the same time? without using the Boolean? Or could I use the 'switch' function to do what I need?

You can't use for loops. It executes one at a time. Try incrementing and decrementing without for loops. Put everything inside the void loop() function.

joshbailz: Ah yes, I see what you mean, is there a way of making value1 increase and value2 decrease at the same time? without using the Boolean? Or could I use the 'switch' function to do what I need?

int value = 0;
int incrementValue = 1;

void loop() 
{
    analogWrite(Pin1, value);  
    analogWrite(Pin2, 255 - value);  // think about it           
    delay(10);  // read blink without delay to rid yourself of delays      
    value = value + incrementValue;
    if (value == 255 || value == 0) 
    {
        incrementValue = incrementValue * -1;
    }
}

joshbailz:
Ah yes, I see what you mean, is there a way of making value1 increase and value2 decrease at the same time? without using the Boolean? Or could I use the ‘switch’ function to do what I need?

  for (value = 0; value < 256; value++) {
    analogWrite(Pin1, value);
    analogWrite(Pin2, !value);
    delay(dt);
  }

This does the same thing.

        incrementValue = - incrementValue;

...R

  if (Value1 ==255) {
    v1=true;
  }
  else {
    v1=false;
  }

  if (v1==true)       
    ..

Very tautological, that code is much simpler thus:

  v1 = Value1 == 255 ;  // a comparison returns a boolean result, assign this directly.

  if (v1)         // no need to compare a boolean to true or false, its already a boolean.
    ..

It won’t make any difference to the compiler but I think it would be clearer like this

v1 = (Value1 == 255);

…R

Possibly, to avoid confusion with multiple assignments, but for other comparisons its not needed:

  boolean greater = a > b ;