IF statements within IF Else staement ?

Hi

I got a minor issue - with turning off 3 LED`s when a surden RFID tag is reconized.

Got this:

else if(compareTag(tag, tag4))
{
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5)
{
// sets the value (range from 0 to 255):
analogWrite(3, fadeValue);
analogWrite(5, fadeValue);
analogWrite(6, fadeValue);
delay(30);}
}

Bu this turns on all LED`s and them dim them - how do I integrate IF statements - so I only go into dim mode on each LED - if the LED are above level 200 ?

Regards Brian Ingemann

Well post the whole code and we might be able to tell you.
Use the # icon when posting code.

You can use if statements anywhere, inside a function, inside an if block, inside a for loop, etc.

You need to clarify your requirements. There is no such thing as dim mode. Where are you setting the analog value for those pins elsewhere?

I assume you want to fade from the current level? (posting the entire code would help)

If so you need to keep a variable that remembers this level and fades down from that. At present you always fade from 255.


Rob

int RFIDResetPin = 13;

//Reconized RFID tags
char tag1[13] = "4500F7739E5F"; //RØD
char tag2[13] = "4B00DA2EB40B"; //GRØN
char tag3[13] = "4500D9558049"; //BLÅ
char tag4[13] = "4500F76F63BE"; //SORT


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

  pinMode(RFIDResetPin, OUTPUT);
  digitalWrite(RFIDResetPin, HIGH);

  //LEDs
  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
}

void loop(){

  char tagString[13];
  int index = 0;
  boolean reading = false;

  while(Serial.available()){

    int readByte = Serial.read(); //read next available byte

    if(readByte == 2) reading = true; //begining of tag
    if(readByte == 3) reading = false; //end of tag

    if(reading && readByte != 2 && readByte != 10 && readByte != 13){
      //store the tag
      tagString[index] = readByte;
      index ++;
    }
  }

  checkTag(tagString); //Check if it is a match
  clearTag(tagString); //Clear the char of all value
  resetReader(); //eset the RFID reader
}

void checkTag(char tag[]){
///////////////////////////////////
//Check the read tag against known tags
///////////////////////////////////

  if(strlen(tag) == 0) return; //empty, no need to contunue

  if(compareTag(tag, tag1))
  { // if matched tag1, do this
    for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) 
    { 
    // sets the value (range from 0 to 255):
     analogWrite(3, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
     delay(30);  
    }                       
  }
  
  
  else if(compareTag(tag, tag2))
  { //if matched tag2, do this
    for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) 
    { 
    // sets the value (range from 0 to 255):
     analogWrite(5, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
     delay(30);  
     }
  }
  
  else if(compareTag(tag, tag3))
  {
    for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) 
    { 
    // sets the value (range from 0 to 255):
     analogWrite(6, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
     delay(30);  
     }
   }
  
  else if(compareTag(tag, tag4))
  {
    for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) 
    { 
    // sets the value (range from 0 to 255):
     analogWrite(3, fadeValue); 
     analogWrite(5, fadeValue);
     analogWrite(6, fadeValue);
     delay(30);}  
    }

  
  else
  {
    Serial.println(tag); //read out any unknown tag
  }

}


void resetReader(){
///////////////////////////////////
//Reset the RFID reader to read again.
///////////////////////////////////
  digitalWrite(RFIDResetPin, LOW);
  digitalWrite(RFIDResetPin, HIGH);
  delay(150);
}

void clearTag(char one[]){
///////////////////////////////////
//clear the char array by filling with null - ASCII 0
//Will think same tag has been read otherwise
///////////////////////////////////
  for(int i = 0; i < strlen(one); i++){
    one[i] = 0;
  }
}

boolean compareTag(char one[], char two[]){
///////////////////////////////////
//compare two value to see if same,
//strcmp not working 100% so we do this
///////////////////////////////////

  if(strlen(one) == 0) return false; //empty

  for(int i = 0; i < 12; i++){
    if(one[i] != two[i]) return false;
  }

  return true; //no mismatches
}

TAG1 turns on LED 1 (PIN 3)
TAG2 turns on LED 2 (PIN5)
TAG3 turns on LED3 (PIN6)

TAG4 must only turn off the LED, which are on

With current code - TAG4 turns on all 3 LED´s - and them dims them to level 0....

I somehow need to fell the level - and only dim to level 0 - if the LED is on...

When you scan a tag, it fades the matching LED up to full brightness, in a blocking fashion. You need to keep track of having done that, in an array.

bool ledState[3]; // global

After fading the LED up, in each case, set ledState[n] to true (where n is 0 for tag 1, 1 for tag 2, etc.).

Then, in the tag 4 case:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5)
{
// sets the value (range from 0 to 255):
if(ledState[0]
analogWrite(3, fadeValue);
with similar additions for the other two pins.

After fading them all off, set ledState[n] to false, for n=0, 1, and 2.

Where do I define the Bool ledState`s ?

Where do I define the Bool ledState`s ?

See the comment?

Sorry - I am new to this forum.

Comment - I see your comment in Pink - but that does not refer to anything...?

I am not that experienced in Boolean statements...

That's 'bool' not Bool, look in his post for 'bool'.