Go Down

Topic: If..else?? (Read 1 time) previous topic - next topic

hammer

Hi

I'm trying to put a random generator together. I have three LED's that should light up randomly but I'm stuck in the programing.

At my first
"If (random = 1)" it brings up the ERROR exspected unqualified-id before 'if'

What is the problem here??

Please help me! :-/

There is no reset button in my life, but wtf!
The GFX kick ass!!!

AlphaBeta

#1
Jul 26, 2009, 02:24 am Last Edit: Jul 26, 2009, 02:25 am by AlphaBeta Reason: 1
Need to see some more code.

If (random = 1)

Is literally nothing.

If is not a keyword, if is.
random does not exist if it is not defined (byte random = 0;)
random = 1 is not a comparison, but an assignment try random == 1
This condition does not have a code block to execute, and will therefore do nothing

http://arduino.cc/en/Reference/RandomSeed
http://arduino.cc/en/Reference/Random

A correct if test that checks to see that the value of random is exactly 1
Quote

if (random==1){
 //do code
}



Hope this helps, if not, post all your code :)

Kitep

It further looks like "random" itself isn't valid.  Instead you needs random(3) to return a value 0-2.

Quote
if (random(3)==1){   //generate random# from 0-2
//do code
}


Good luck!

hammer

Hi

It does still make that Error


Heres the code

 /*
 Fading
 
 This example shows how to fade an LED using the analogWrite() function.
 
 The circuit:
 * LED attached from digital pin 9 to ground.
 
 Created 1 Nov 2008
 By David A. Mellis
 Modified 17 June 2009
 By Tom Igoe
 
 http://arduino.cc/en/Tutorial/Fading
 
 */

int ledPin = 9;    // LED connected to digital pin 9
int ledPin1 = 10;
int ledPin2 = 11;
long randNumber;

void setup()  {
Serial.begin(9600);

 // if analog input pin 0 is unconnected, random analog
 // noise will cause the call to randomSeed() to generate
 // different seed numbers each time the sketch runs.
 // randomSeed() will then shuffle the random function.
 randomSeed(analogRead(0));

}
void loop()  {
  randNumber = random(3);
  Serial.println(randNumber);
 
  delay (50);
}

 if(random = 1);  // The Problem Comes Here  error: expected unqualified-id before 'if'
{
   // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);        
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  }
 
  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);        
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  }
}
}
else  if (random = 2);
{
   // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin1, fadeValue);        
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  }    
 
   // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin1, fadeValue);        
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  }
 
}

else if (random = 3);
{
   // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin2, fadeValue);        
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  }
 
  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin2, fadeValue);        
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  }
 
}

if (random = 0);
{
 return loop
}
There is no reset button in my life, but wtf!
The GFX kick ass!!!

AlphaBeta

http://arduino.pastebin.com/f65d8adde

In short:

Still need to remember == when doing comparisons.
An if test has no ; after it.
random is nothing
but a function name, and it can not be called without using ()
:)

hammer

There is no reset button in my life, but wtf!
The GFX kick ass!!!

Go Up