If statement problem

I’m having trouble with the if statement for some odd reason. I can get this to work:

if (sensorValue < 64)
{
  lc.setLed(0,0,0,true);
}

else 
{
  lc.setLed(0,0,0,false);
}

but when I try to do this:

if (40 <sensorValue < 64)
{
  lc.setLed(0,0,0,true);
}

else 
{
  lc.setLed(0,0,0,false);
}

it compiles but does not do what I want it to do. I am getting a value from a potentiometer and depending on the value it will light an led. I was eventually planning to do a whole bunch of else ifs.

Give this a try…

if ( (sensorValue > 40) && (sensorValue < 64) )

it compiles but does not do what I want it to do

C syntax allows some things that look valid but don’t work quite as expected. Your original if is evaluate this way (note: boolean expressions result in either an integer zero or an integer one)…

if ( (40 <sensorValue) < 64)
...
if ( (0 or 1) < 64)
...
if ( 1 )

Yes I tried that and it works great!!! Thanks!! Now I have a logic error. I’m trying to light an led depending on what the value is in two pots ie x and y on a dot matrix. I can light an led but clearing in between is giving not working like it should. Here is my code:

#include <LedControl.h> //Library to Control Max7221
/********************************************************************************************
*  Pin numbers vary depending on what is available
*  pin 10 is connected to the DataIn 
*  pin 9 is connected to the CLK 
*  pin 8 is connected to LOAD 
*  Sample for LedControl is
*  LedControl lc=LedControl(DataIn Pin, CLK Pin, LOAD Pin, # of Devices);
*
*  lc.setLed(address, row, column);
*  Max number of Devices is 8 for this library.
*******************************************************************************************/
LedControl lc=LedControl(10,9,8,4);

// Its good practice to wait between displays
//unsigned long delaytime=500;

int c;
int r;
int a;
int sensorPin = 0;    // select the input pin for the potentiometer
int sensorPin1 = 1;

 int sensorValue = 0;  // variable to store the value coming from the sensor
 int sensorValue1=0;

// Function to clear all Matrices
 void clearall() {
   for(int d=0;d < 3;d++) {
  //    delay(delaytime);
      lc.clearDisplay(d);
   }
 }
 
 void sensor(){
   
   sensorValue = analogRead(sensorPin);    
   sensorValue1 = analogRead(sensorPin1);
   Serial.println(sensorValue);
   Serial.println(sensorValue1);
   
if (sensorValue > 0 && sensorValue < 64 && sensorValue1 > 0 && sensorValue1<64)
{
  clearall();
  a=3; r=0; c=0;
}

else if (sensorValue >0  && sensorValue < 64 && sensorValue1 >= 64 && sensorValue1 < 128)
{
  clearall();
  a=3; r=0; c=1;
}
else if (sensorValue >0  && sensorValue < 64 && sensorValue1 >= 128 && sensorValue1 < 192)
{
  clearall();
  a=3; r=0; c=3;
}
 }
 
void setup() {
  //Set baud rate for serial communication
   Serial.begin(9600);
  //we have already set the number of devices when we created the LedControl
  int devices=lc.getDeviceCount();
  //we have to init all devices in a loop
  for(int address=0;address<devices;address++) {
    /*The MAX72XX is in power-saving mode on startup*/
    lc.shutdown(address,false);
    /* Set the brightness to a medium values */
    lc.setIntensity(address,8);
    /* and clear the display */
    lc.clearDisplay(address);
  }
}

void loop() { 
// If we needed to verify how many devices are connected we use:
//  int devices=lc.getDeviceCount();
  

  /*   lc.setLed(1,4,5,true);
     delay(100);
     lc.setLed(1,4,5,false);
     lc.setLed(0,0,0,true);
     delay(100);
     lc.setLed(0,0,0,false);
  */
  sensor s();
lc.setLed(0,0,0, true);

if(sensorValue >200){
  clearall();
}

}

It flickers when I run this code. I want it to stay lit, is there any other way of doing this?

It's beyond my experience. Hopefully, someone else can help.

Thanks for the thought anyway! I appreciate all the help. Hopefully Eberhard will see my thread. I seem to have lost his email hehe. It seems like a logical error not really hardware so we'll see what happens.

I don’t have time to dig through your code but if you use parentheses to make it absolutely clear what you want. perhaps the compiler will see it your way.

For example you have:

if (sensorValue > 0 && sensorValue < 64 && sensorValue1 > 0 && sensorValue1<64)
{
  clearall();
  a=3; r=0; c=0;
}

else if (sensorValue >0  && sensorValue < 64 && sensorValue1 >= 64 && sensorValue1 < 128)
{
  clearall();
  a=3; r=0; c=1;
}

I THINK you want

if ((sensorValue > 0) && (sensorValue < 64) && (sensorValue1 > 0) && (sensorValue1<64))
{
  clearall();
  a=3; r=0; c=0;
}

else if ( (sensorValue >0)  && (sensorValue < 64) && (sensorValue1 >= 64) && (sensorValue1 < 128))
{
  clearall();
  a=3; r=0; c=1;
}

Even expert coders who know the evaluation precidents get things wrong by not using enough parentheses.

I’m not sure exactly what’s going on in your code, but a quick look says you can optimize it a little:

Since (sensorValue > 0) && (sensorValue < 64) are common to all IF statements… I would bound them all by that test first:

if ( (sensorValue > 0) && (sensorValue < 64) ) 
{

  if ( (sensorValue1 > 0) && (sensorValue1 < 64) )
  {
    clearall();
    a=3; r=0; c=0;
  }

  else if ( (sensorValue1 >= 64) && (sensorValue1 < 128) )
  {
    clearall();
    a=3; r=0; c=1;
  }

  else if ( (sensorValue1 >= 128) && (sensorValue1 < 192) )
  {
    clearall();
    a=3; r=0; c=3;
  }

  else 
  {
   //IS THERE A DEFAULT STATE YOU WANT TO SET THE DISPLAY TO?
   //I wouldn't think you'd use else-if's, if you didn't want a else at the end
   //if not, just use if's
  }

}

Also, you have “sensor s();” in your code… not sure if it’s really that way in your code.

Got it working! I had it all in the loop so when I created functions it ran just fine. Here is how it looks now:

#include <LedControl.h> //Library to Control Max7221
/********************************************************************************************
*  Pin numbers vary depending on what is available
*  pin 10 is connected to the DataIn 
*  pin 9 is connected to the CLK 
*  pin 8 is connected to LOAD 
*  Sample for LedControl is
*  LedControl lc=LedControl(DataIn Pin, CLK Pin, LOAD Pin, # of Devices);
*
*  lc.setLed(address, row, column);
*  Max number of Devices is 8 for this library.
*******************************************************************************************/
LedControl lc=LedControl(10,9,8,4);

int sensorPin = 0;    // select the input pin for the potentiometer
int sensorPin1 = 1;
 int sensorValue = 0;
 int sensorValue1=0;

int c;
int r;
int a;

 
 void row(){
if (((sensorValue >=0)  && (sensorValue < 64)) or ((sensorValue >=512)  && (sensorValue < 576))) 
{
   r=0;
}

else if (((sensorValue >=64)  && (sensorValue < 128)) or ((sensorValue >=576)  && (sensorValue < 640)))
{
  r=1;
}
else if (((sensorValue >=128)  &&(sensorValue < 192))  or ((sensorValue >=640)  && (sensorValue < 704)))
{
  r=2;
}
else if (((sensorValue >= 192) && (sensorValue < 256)) or ((sensorValue >=704)  && (sensorValue < 768)))
{
   r=3;
}

else if (((sensorValue >=256)  && (sensorValue < 320)) or ((sensorValue >=768)  && (sensorValue < 832)))
{
  r=4;
}
else if (((sensorValue >=320)  &&(sensorValue < 384)) or ((sensorValue >=832)  && (sensorValue < 896)))
{
  r=5;
}
else if (((sensorValue >= 384) && (sensorValue < 448)) or ((sensorValue >=896)  && (sensorValue < 960)))
{
   r=6;
}

else if (((sensorValue >=448)  && (sensorValue < 512)) or ((sensorValue >=960)  && (sensorValue <= 1023)))
{
  r=7;
}

 }
 
 void cols(){
if (((sensorValue1 >=0)  && (sensorValue1 < 64)) or ((sensorValue1 >=512)  && (sensorValue1 < 576))) 
{
   c=0;
}

else if (((sensorValue1 >=64)  && (sensorValue1 < 128)) or ((sensorValue1 >=576)  && (sensorValue1 < 640)))
{
  c=1;
}
else if (((sensorValue1 >=128)  &&(sensorValue1 < 192))  or ((sensorValue1 >=640)  && (sensorValue1 < 704)))
{
  c=2;
}
else if (((sensorValue1 >= 192) && (sensorValue1 < 256)) or ((sensorValue1 >=704)  && (sensorValue1 < 768)))
{
   c=3;
}

else if (((sensorValue1 >=256)  && (sensorValue1 < 320)) or ((sensorValue1 >=768)  && (sensorValue1 < 832)))
{
  c=4;
}
else if (((sensorValue1 >=320)  &&(sensorValue1 < 384)) or ((sensorValue1 >=832)  && (sensorValue1 < 896)))
{
  c=5;
}
else if (((sensorValue1 >= 384) && (sensorValue1 < 448)) or ((sensorValue1 >=896)  && (sensorValue1 < 960)))
{
   c=6;
}

else if (((sensorValue1 >=448)  && (sensorValue1 < 512)) or ((sensorValue1 >=960)  && (sensorValue1 <= 1023)))
{
  c=7;
}

 }
  void address(){
if (((sensorValue >=0)  && (sensorValue < 512)) && ((sensorValue1 >=0)  && (sensorValue1 < 512))) 
{
   a=3;
}

else if (((sensorValue >=0)  && (sensorValue < 512)) && ((sensorValue1 >=512)  && (sensorValue1 < 1023)))
{
  a=2;
}
else if (((sensorValue >=512)  &&(sensorValue < 1023))  && ((sensorValue1 >=0)  && (sensorValue1 < 512)))
{
  a=1;
}
else if (((sensorValue >= 512) && (sensorValue < 1023)) && ((sensorValue1 >=512)  && (sensorValue1 < 1023)))
{
   a=0;
}

 }



 
   void setup() {

   Serial.begin(9600);
  //we have already set the number of devices when we created the LedControl
  int devices=lc.getDeviceCount();
  //we have to init all devices in a loop
  for(int address=0;address<devices;address++) {
    /*The MAX72XX is in power-saving mode on startup*/
    lc.shutdown(address,false);
    /* Set the brightness to a medium values */
    lc.setIntensity(address,8);
    /* and clear the display */
    lc.clearDisplay(address);
   }
   }
   void loop() { 
     
     sensorValue = analogRead(sensorPin);    
     sensorValue1 = analogRead(sensorPin1);
   Serial.println(sensorValue);
   Serial.println(sensorValue1); 
    address();
    row();
    cols();
    lc.setLed(a,r,c, true);
  //  lc.setLed(3,1,0, true);
     delay(200);
     lc.clearDisplay(3);
     lc.clearDisplay(2);
     lc.clearDisplay(1);
     lc.clearDisplay(0);
   }

Using two pots I can light an led depending on the value read. I used 4 max7221 and the LedControl library to control the leds. Now my problem is uart… any examples you guys recommend? I just want to send and receive decimal number but my receive isn’t working.