Multiple iff statements....

i all i am currently programming multiple sensors... 1 analogue controlling a bar chart of LEDs and then 2 more that only light up LEDs in certain ranges. I also have 2 digital switched but they are a completely different story....

My Code that works currently is for the 1 analogue controlling the bar chart and currently stands at:

 // these constants won't change:
 const int analogPinVol = 0;    // the pin that the volume is attached to
 const int ledCount = 6;    // the number of LEDs in the bar graph
 const int analogPinPlay = 1; // the pin that the play/pause is attached to
 const int analogPinSource = 2; // the pin that the source is attached to
 const int buttonPin = 2; // the pin that the skip sensor 1 is attached to
 const int sensorRightPin = 1; // the pin that the skip sensor 2 is attached to
 const int ledPinPlay = 6; // the pin that the LED for Play/pause is attached to
 const int ledPinSource = 5; // the pin that the LED for source is attached to
 const int ledPinPrevious = 4; // the pin that the LED for Previous track is attached to
 const int ledPinNext = 3; // the pin that the LED for Next track is attached to
 
 // Variables will change:
 int ledPins[] = { 
   7, 9, 10, 11, 12, 13 };   // an array of pin numbers to which LEDs are attached
 

  

 
 void setup() {
   // loop over the pin array and set them all to output:
   for (int thisLed = 0; thisLed < ledCount; thisLed++) {
     pinMode(ledPins[thisLed], OUTPUT); 
     pinMode(ledPinPlay, OUTPUT);  // declare the ledPinplay as an OUTPUT
     pinMode(ledPinSource, OUTPUT);  // declare the ledPinplay as an OUTPUT
     pinMode(ledPinPrevious, OUTPUT);  // declare the ledPinplay as an OUTPUT
     pinMode(ledPinNext, OUTPUT);  // declare the ledPinplay as an OUTPUT
     pinMode(buttonPin, INPUT); // declare the left sensor as an INPUT



   }
 }

 void loop() {
   
   // read the volume:
   int sensorReading = analogRead(analogPinVol);
   // map the result to a range from 0 to the number of LEDs:
   int ledLevel = map(sensorReading, 120, 600, 0, ledCount);
   // loop over the LED array:
   for (int thisLed = 0; thisLed < ledCount; thisLed++) {
     // if the array element's index is less than ledLevel,
     // turn the pin for this element on:
     if (thisLed < ledLevel) {
       digitalWrite(ledPins[thisLed], HIGH);
     } 
     // turn off all pins higher than the ledLevel:
     else {
       digitalWrite(ledPins[thisLed], LOW); 
     }
   }
 }

Ignore most of the int as they arent programmed yet... But i am trying to combine this with an analogue lighting an LED which is currently:

 // these constants won't change:
 const int analogPinVol = 0;    // the pin that the volume is attached to
 const int ledCount = 6;    // the number of LEDs in the bar graph
 const int analogPinPlay = 1; // the pin that the play/pause is attached to
 const int analogPinSource = 2; // the pin that the source is attached to
 const int buttonPin = 2; // the pin that the skip sensor 1 is attached to
 const int sensorRightPin = 1; // the pin that the skip sensor 2 is attached to
 const int ledPinPlay = 6; // the pin that the LED for Play/pause is attached to
 const int ledPinSource = 5; // the pin that the LED for source is attached to
 const int ledPinPrevious = 4; // the pin that the LED for Previous track is attached to
 const int ledPinNext = 3; // the pin that the LED for Next track is attached to
 
 // Variables will change:
 int ledPins[] = { 
   7, 9, 10, 11, 12, 13 };   // an array of pin numbers to which LEDs are attached
 int val = 0; 

 void setup() {
   // loop over the pin array and set them all to output:
   for (int thisLed = 0; thisLed < ledCount; thisLed++) {
     pinMode(ledPins[thisLed], OUTPUT); 
     pinMode(ledPinPlay, OUTPUT);  // declare the ledPinplay as an OUTPUT
     pinMode(ledPinSource, OUTPUT);  // declare the ledPinplay as an OUTPUT
     pinMode(ledPinPrevious, OUTPUT);  // declare the ledPinplay as an OUTPUT
     pinMode(ledPinNext, OUTPUT);  // declare the ledPinplay as an OUTPUT
     pinMode(buttonPin, INPUT); // declare the left sensor as an INPUT



   }
 }

 void loop() {
   

   val = analogRead(analogPinPlay);
   if (val > 160) {
     digitalWrite(ledPinPlay, HIGH);
   }
   else {
     //
     digitalWrite(ledPinPlay, LOW);
    }
 }

Again... ignore the other int as they arent used yet.

I am just very unsure on how to combine these if statements and the times i have tried it with:

 void loop() {
   
 
   val = analogRead(analogPinPlay);
   // read the volume:
   int sensorReading = analogRead(analogPinVol);
   // map the result to a range from 0 to the number of LEDs:
   int ledLevel = map(sensorReading, 120, 600, 0, ledCount);
   // loop over the LED array:
   for (int thisLed = 0; thisLed < ledCount; thisLed++) {
     // if the array element's index is less than ledLevel,
     // turn the pin for this element on:
     if (thisLed < ledLevel) {
       digitalWrite(ledPins[thisLed], HIGH);
        if (val > 160) {
     digitalWrite(ledPinPlay, HIGH);
     } 
     // turn off all pins higher than the ledLevel:
     else {
       digitalWrite(ledPins[thisLed], LOW); 
        digitalWrite(ledPinPlay, LOW);
     }
   }
 }

I get the error: a function -definition is not allowed here before { token.

Can anyone help please....

I also want to do the same with 'analogPinSource' do i just write a new value area and double the if commands???

I am so confused.

I don't think the last snippet tells the whole story - please post the whole sketch.

Hi,

here is the solution:

 void loop() {
   // first handle the led bar
   // read the volume:
   int sensorReading = analogRead(analogPinVol);
   // map the result to a range from 0 to the number of LEDs:
   int ledLevel = map(sensorReading, 120, 600, 0, ledCount);
   // loop over the LED array:
   for (int thisLed = 0; thisLed < ledCount; thisLed++) {
     // if the array element's index is less than ledLevel,
     // turn the pin for this element on:
     if (thisLed < ledLevel) {
       digitalWrite(ledPins[thisLed], HIGH);
     }
     // turn off all pins higher than the ledLevel:
     else {
       digitalWrite(ledPins[thisLed], LOW);
     }
   }

   // then do the Play Led
   val = analogRead(analogPinPlay);
   if (val > 160) {
     digitalWrite(ledPinPlay, HIGH);
   }
   else {
     //
     digitalWrite(ledPinPlay, LOW);
    }
 }

Good luck
Mike

i have entered this code:

 /*
   LED bar graph
  
   Turns on a series of LEDs based on the value of an analog sensor.
   This is a simple way to make a bar graph display. Though this graph
   uses 10 LEDs, you can use any number by changing the LED count
   and the pins in the array.
   
   This method can be used to control any series of digital outputs that
   depends on an analog input.
  
   The circuit:
    * LEDs from pins 2 through 11 to ground
  
  created 26 Jun 2009
  by Tom Igoe 
  
  http://www.arduino.cc/en/Tutorial/BarGraph
  */

 // these constants won't change:
 const int analogPinVol = 0;    // the pin that the volume is attached to
 const int ledCount = 6;    // the number of LEDs in the bar graph
 const int analogPinPlay = 1; // the pin that the play/pause is attached to
 const int analogPinSource = 2; // the pin that the source is attached to
 const int buttonPin = 2; // the pin that the skip sensor 1 is attached to
 const int sensorRightPin = 1; // the pin that the skip sensor 2 is attached to
 const int ledPinPlay = 6; // the pin that the LED for Play/pause is attached to
 const int ledPinSource = 5; // the pin that the LED for source is attached to
 const int ledPinPrevious = 4; // the pin that the LED for Previous track is attached to
 const int ledPinNext = 3; // the pin that the LED for Next track is attached to
 
 // Variables will change:
 int ledPins[] = { 
   7, 9, 10, 11, 12, 13 };   // an array of pin numbers to which LEDs are attached
 int val = 0

  

 
 void setup() {
   // loop over the pin array and set them all to output:
   for (int thisLed = 0; thisLed < ledCount; thisLed++) {
     pinMode(ledPins[thisLed], OUTPUT); 
     pinMode(ledPinPlay, OUTPUT);  // declare the ledPinplay as an OUTPUT
     pinMode(ledPinSource, OUTPUT);  // declare the ledPinplay as an OUTPUT
     pinMode(ledPinPrevious, OUTPUT);  // declare the ledPinplay as an OUTPUT
     pinMode(ledPinNext, OUTPUT);  // declare the ledPinplay as an OUTPUT
     pinMode(buttonPin, INPUT); // declare the left sensor as an INPUT



   }
 }

 void loop() {
   // first handle the led bar
   // read the volume:
   int sensorReading = analogRead(analogPinVol);
   // map the result to a range from 0 to the number of LEDs:
   int ledLevel = map(sensorReading, 120, 600, 0, ledCount);
   // loop over the LED array:
   for (int thisLed = 0; thisLed < ledCount; thisLed++) {
     // if the array element's index is less than ledLevel,
     // turn the pin for this element on:
     if (thisLed < ledLevel) {
       digitalWrite(ledPins[thisLed], HIGH);
     }
     // turn off all pins higher than the ledLevel:
     else {
       digitalWrite(ledPins[thisLed], LOW);
     }
   }

   // then do the Play Led
   val = analogRead(analogPinPlay);
   if (val > 160) {
     digitalWrite(ledPinPlay, HIGH);
   }
   else {
     //
     digitalWrite(ledPinPlay, LOW);
    }
 }

and recieve the error:

error: expected unqualified-id before the numeric constant

It didnt highlight an issue in the code and i am very confused

does anyone have an idea what that means?

thanks

 int val = 0

is your problem.

BTW, your loop in "setup" is a bit wasteful - it reinitialises "ledPinPlay", "ledPinSource" etc over and over again. Not a problem, but not useful either.

how come? it works when it isnt combined with another if statement?

if i remove the LED bar section :

  http://www.arduino.cc/en/Tutorial/BarGraph
  */

 // these constants won't change:
 const int analogPinVol = 0;    // the pin that the volume is attached to
 const int ledCount = 6;    // the number of LEDs in the bar graph
 const int analogPinPlay = 1; // the pin that the play/pause is attached to
 const int analogPinSource = 2; // the pin that the source is attached to
 const int buttonPin = 2; // the pin that the skip sensor 1 is attached to
 const int sensorRightPin = 1; // the pin that the skip sensor 2 is attached to
 const int ledPinPlay = 6; // the pin that the LED for Play/pause is attached to
 const int ledPinSource = 5; // the pin that the LED for source is attached to
 const int ledPinPrevious = 4; // the pin that the LED for Previous track is attached to
 const int ledPinNext = 3; // the pin that the LED for Next track is attached to
 
 // Variables will change:
 int ledPins[] = { 
   7, 9, 10, 11, 12, 13 };   // an array of pin numbers to which LEDs are attached
 int val = 0; 
 
 void setup() {
   // loop over the pin array and set them all to output:
   for (int thisLed = 0; thisLed < ledCount; thisLed++) {
     pinMode(ledPins[thisLed], OUTPUT); 
     pinMode(ledPinPlay, OUTPUT);  // declare the ledPinplay as an OUTPUT
     pinMode(ledPinSource, OUTPUT);  // declare the ledPinplay as an OUTPUT
     pinMode(ledPinPrevious, OUTPUT);  // declare the ledPinplay as an OUTPUT
     pinMode(ledPinNext, OUTPUT);  // declare the ledPinplay as an OUTPUT
     pinMode(buttonPin, INPUT); // declare the left sensor as an INPUT



   }
 }

 void loop() {
   

   val = analogRead(analogPinPlay);
   if (val > 160) {
     digitalWrite(ledPinPlay, HIGH);
   }
   else {
     digitalWrite(ledPinPlay, LOW);
   }
 }
int val = 0;

Spot the difference?

the space?

; :slight_smile:

Beautiful, thankyou :).

Another little qwuestion is do you know what the error:

Error: expected constructor, destructor, or type conversion before '=' token means?

I have tried to put in another analogue sensor to turn on the LED using the same if statement using this code:

 // these constants won't change:
 const int analogPinVol = 0;    // the pin that the volume is attached to
 const int ledCount = 6;    // the number of LEDs in the bar graph
 const int analogPinPlay = 1; // the pin that the play/pause is attached to
 const int analogPinSource = 2; // the pin that the source is attached to
 const int buttonPin = 2; // the pin that the skip sensor 1 is attached to
 const int sensorRightPin = 1; // the pin that the skip sensor 2 is attached to
 const int ledPinPlay = 6; // the pin that the LED for Play/pause is attached to
 const int ledPinSource = 5; // the pin that the LED for source is attached to
 const int ledPinPrevious = 4; // the pin that the LED for Previous track is attached to
 const int ledPinNext = 3; // the pin that the LED for Next track is attached to
 
 // Variables will change:
 int ledPins[] = { 
   7, 9, 10, 11, 12, 13 };   // an array of pin numbers to which LEDs are attached
 int val = 0;
 int valSource = 0;
  

 
 void setup() {
   // loop over the pin array and set them all to output:
   for (int thisLed = 0; thisLed < ledCount; thisLed++) {
     pinMode(ledPins[thisLed], OUTPUT); 
     pinMode(ledPinPlay, OUTPUT);  // declare the ledPinplay as an OUTPUT
     pinMode(ledPinSource, OUTPUT);  // declare the ledPinplay as an OUTPUT
     pinMode(ledPinPrevious, OUTPUT);  // declare the ledPinplay as an OUTPUT
     pinMode(ledPinNext, OUTPUT);  // declare the ledPinplay as an OUTPUT
     pinMode(buttonPin, INPUT); // declare the left sensor as an INPUT



   }
 }

 void loop() {
   // first handle the led bar
   // read the volume:
   int sensorReading = analogRead(analogPinVol);
   // map the result to a range from 0 to the number of LEDs:
   int ledLevel = map(sensorReading, 120, 600, 0, ledCount);
   // loop over the LED array:
   for (int thisLed = 0; thisLed < ledCount; thisLed++) {
     // if the array element's index is less than ledLevel,
     // turn the pin for this element on:
     if (thisLed < ledLevel) {
       digitalWrite(ledPins[thisLed], HIGH);
     }
     // turn off all pins higher than the ledLevel:
     else {
       digitalWrite(ledPins[thisLed], LOW);
     }
   }

   // then do the Play Led
   val = analogRead(analogPinPlay);
   if (val > 160) {
     digitalWrite(ledPinPlay, HIGH);
   }
   else {
     //
     digitalWrite(ledPinPlay, LOW);
    }
 }
   // then do the Source Led
   valSource = analogRead(analogPinSource);
   if (valSource > 160) {
     digitalWrite(ledPinNext, HIGH);
   }
   else {
     //
     digitalWrite(ledPinNext, LOW);
    }
 }

and i get that value on the line:

 valSource = analogRead(analogPinSource);

:s can i not have two val = analogRead ?

I think because that line is outside "loop()".
Check your braces {}

ahh yeah.... i changed it to just one } after else rather than } }

Do you know much about setting up a sequence of two digital infrared measurement sensors?

I have this code:

boolean sensorLeftTriggered = false;
boolean sensorRightTriggered = false;

int sensorLeft = LOW;
int sensorRight = LOW;

long sensorLeftTime = 0;
long sensorRightTime = 0;

int sensorLeftPin = 1;
int sensorRightPin = 2;

void setup()
{
   pinMode(sensorLeftPin, INPUT);
   pinMode(sensorRightPin, INPUT);
}

void loop()
{
   int sensorLeftValue = digitalRead(sensorLeftPin);
   int sensorRightValue = digitalRead(sensorRightPin);

   if(sensorLeftValue == HIGH && sensorLeftValue != sensorLeft)
   {
       // Sensor triggered, and was not triggered before
       sensorLeftTime = millis();
       sensorLeftTriggered = true;
   }
   sensorLeft = sensorLeftValue;

   if(sensorRightValue == HIGH && sensorRightValue != sensorRight)
   {
       // Sensor triggered, and was not triggered before
       sensorRightTime = millis();
       sensorRightTriggered = true;
   }
   sensorRight = sensorRightValue;

   // See if both sensors have been triggered
   if(sensorLeftTriggered && sensorRightTriggered)
   {
       if(sensorLeftTime - sensorRightTime > 0)
       {
           // Sensor right was triggered, then sensor left
       }
       else
       {
           // Sensor left was triggered, then sensor right
       }
   }
}

and i have my digital sensors into pins 1 and 2 but when i upload this code nothing happens, i dont know how to get a reading from the sensor?

Sorry... i am completely new to this, and am really struggling.

The thing about debugging (as about much else in life) is to make things as easy as possible for yourself.
You have a serial connection to the PC, so use it.

Rip out all the stuff that doesn't actually read things from your sensor (all the "if" stuff), and just print the readings you get back from the sensor, and put it in "loop".
When you've got the sensors working, then build on that.

See now i really dont understand what you mean there, im sorry.

The issue im having is i need to just have these sensors up and working and making lights turn on so i can test some hand movements over them in certain areas. it currently doesnt matter to much if the coding is totally corrent. I just need to have sensors do certain things and stick it all together. I know its a bad way to do it but at the moment its the way i have to do it. ILater down the line will will do proper programming and get it sorted to a nice standard.

I just need to add on two infrared digital sensors so that when a user goes from left to right (over sensor 1 then 2) it triggers one LED and right to left (over sensor 2 then 1) it triggers a different LED. I have that code i just posted but it doesnt seem to create any results thus making me think i have no readings being registered to trigger the LED's

The issue im having is i need to just have these sensors up and working and making lights turn on so i can test some hand movements over them in certain areas

That's exactly what I'm saying:

  1. You need to have the sensors up and working
  2. You need to turn lights on based on sensor readings.

However, you don't know how to do 1) yet, so building a sketch with the lights in depending on 1) working is just making more complexity.

Trying to write code on top of code / hardware you don't know works (forget neat/elegant/efficient for now) is simply asking for trouble.

void setup ()
{
  Serial.begin (9600);
  // other pin initialisation
}
void loop () 
{
   int sensorLeftValue = digitalRead(sensorLeftPin);
   int sensorRightValue = digitalRead(sensorRightPin);
   Serial.print (sensorLeftValue, DEC);
   Serial.print (sensorRightValue, DEC);
}

is all you should be concentrating on for now.