# A few random coding questions - if else statement

I’m looking to do an “if else” statement. The arduino will be picking up an analog number from 0-1024 then converting this into another number that I can use as an output.

ie. If the input is less than 128, then the output is 1
If the input is between 129 and 256 then the output is 3
if the input is between 257 and 512 then the output is 7

How can I turn that statement into code?

Thanks a lot in advance. I’m a newbie at programming!!

Here is what I have now:

if (value1 < 128)
{
value1 == 1
}
else if (value1 ???
{
value1 == 3
}

What you have is the correct start, except for the missing ;s.

``````int option = 0;
if(value1 < 128)
option = 1;
else if(value1 < 257)
option = 3;
else if(value1 < 513)
option = 7;
``````

Note that assignment of a vaue is a single =, comparison of equality is a double ==.

Using the wrong one will not always give a compilation syntax error, bur will almost always give an unexpected or non-existent result.

A couple of things: analogRead returns a value between zero and 1023. When posting code, please use the # icon in the editor's toolbar. Your code is very long and repetitive. This usually means you could make it shorter and simpler, giving the bugs fewer dark corners to hide in.

It is also a bit short on comments.

Thanks for the tips. Once again, I’m new here so I don’t have a lot of experience with this webpage, or Arduino coding. I’m not sure how to make my code shorter as of yet. Im just trying to get it to work, but i’ve been having problems.

Here is a link to the EQ chip i’m using:

From this i’m running into a shift register array to power an 8x7 grid of LEDs.

I’m going to do a test of the code (I also added more comments):

``````int analogPin = 0;              // read from multiplexer using analog input 0
int strobePin = 2;               // strobe is attached to digital pin 2
int resetPin = 3;               // reset is attached to digital pin 3

int dataPin = 6;              // The Serial Data Pin to the Shift Register

int latchPin = 7;             // The Latch Pin to the Shift Register

int clockPin = 8;             // The Clock Pin to the Shift Register

int value1 = 0;
int value2 = 0;
int value3 = 0;
int value4 = 0;
int value5 = 0;
int value6 = 0;
int value7 = 0;
int option1 = 0;
int option2 = 0;
int option3 = 0;
int option4 = 0;
int option5 = 0;
int option6 = 0;
int option7 = 0;

void setup()
{

pinMode(analogPin, INPUT);     //analogPin is an input
pinMode(strobePin, OUTPUT);    //strobePin is an output
pinMode(resetPin, OUTPUT);    //resetPin is an output
analogReference(DEFAULT);     //sets analogReference to default
pinMode(dataPin, OUTPUT);    // Configure Digital Pins
pinMode(latchPin, OUTPUT);  // Configure Digital Pins
pinMode(clockPin, OUTPUT);  // Configure Digital Pins
digitalWrite(resetPin, LOW);     // reset off
digitalWrite(strobePin, HIGH);   // holds multiplex out in one spot until the start.

}

void loop()
{
digitalWrite(resetPin, HIGH);   //resets EQ chip
digitalWrite(resetPin, LOW);    //reset off

digitalWrite(strobePin, LOW);   // When strobe goes low, outputs multiplexed value to arduino analog in
delayMicroseconds(30); // to allow the output to settle

if(value1 < 128)
option1 = 1;
else if(value1 < 257)
option1 = 3;
else if(value1 < 385)
option1 = 7;
else if(value1 < 512)
option1 = 15;
else if(value1 < 640)
option1 = 31;
else if(value1 < 768)
option1 = 63;
else if(value1 < 896)
option1 = 127;
else if(value1 < 1024)
option1 = 255;

digitalWrite(strobePin, HIGH);
digitalWrite(strobePin, LOW);
delayMicroseconds(30); // to allow the output to settle

if(value2 < 128)
option2 = 1;
else if(value2 < 257)
option2 = 3;
else if(value2 < 385)
option2 = 7;
else if(value2 < 512)
option2 = 15;
else if(value2 < 640)
option2 = 31;
else if(value2 < 768)
option2 = 63;
else if(value2 < 896)
option2 = 127;
else if(value2 < 1024)
option2 = 255;

digitalWrite(strobePin, HIGH);
digitalWrite(strobePin, LOW);
delayMicroseconds(30); // to allow the output to settle

if(value3 < 128)
option3 = 1;
else if(value3 < 257)
option3 = 3;
else if(value3 < 385)
option3 = 7;
else if(value3 < 512)
option3 = 15;
else if(value3 < 640)
option3 = 31;
else if(value3 < 768)
option3 = 63;
else if(value3 < 896)
option3 = 127;
else if(value3 < 1024)
option3 = 255;

digitalWrite(strobePin, HIGH);
digitalWrite(strobePin, LOW);
delayMicroseconds(30); // to allow the output to settle

if(value4 < 128)
option4 = 1;
else if(value4 < 257)
option4 = 3;
else if(value4 < 385)
option4 = 7;
else if(value4 < 512)
option4 = 15;
else if(value4 < 640)
option4 = 31;
else if(value4 < 768)
option4 = 63;
else if(value4 < 896)
option4 = 127;
else if(value4 < 1024)
option4 = 255;

digitalWrite(strobePin, HIGH);
digitalWrite(strobePin, LOW);
delayMicroseconds(30); // to allow the output to settle

if(value5 < 128)
option5 = 1;
else if(value5 < 257)
option5 = 3;
else if(value5 < 385)
option5 = 7;
else if(value5 < 512)
option5 = 15;
else if(value5 < 640)
option5 = 31;
else if(value5 < 768)
option5 = 63;
else if(value5 < 896)
option5 = 127;
else if(value5 < 1024)
option5 = 255;

digitalWrite(strobePin, HIGH);
digitalWrite(strobePin, LOW);
delayMicroseconds(30); // to allow the output to settle

if(value6 < 128)
option6 = 1;
else if(value6 < 257)
option6 = 3;
else if(value6 < 385)
option6 = 7;
else if(value6 < 512)
option6 = 15;
else if(value6 < 640)
option6 = 31;
else if(value6 < 768)
option6 = 63;
else if(value6 < 896)
option6 = 127;
else if(value6 < 1024)
option6 = 255;

digitalWrite(strobePin, HIGH);
digitalWrite(strobePin, LOW);
delayMicroseconds(30); // to allow the output to settle

if(value7 < 128)
option7 = 1;
else if(value7 < 257)
option7 = 3;
else if(value7 < 385)
option7 = 7;
else if(value7 < 512)
option7 = 15;
else if(value7 < 640)
option7 = 31;
else if(value7 < 768)
option7 = 63;
else if(value7 < 896)
option7 = 127;
else if(value7 < 1024)
option7 = 255;

digitalWrite(latchPin, LOW);                       // Pull latch LOW to send data

shiftOut(dataPin, clockPin, MSBFIRST, option1);
shiftOut(dataPin, clockPin, MSBFIRST, option2);
shiftOut(dataPin, clockPin, MSBFIRST, option3);
shiftOut(dataPin, clockPin, MSBFIRST, option4);
shiftOut(dataPin, clockPin, MSBFIRST, option5);
shiftOut(dataPin, clockPin, MSBFIRST, option6);
shiftOut(dataPin, clockPin, MSBFIRST, option7);

delay(50);

digitalWrite(latchPin, HIGH);                      // Pull latch HIGH to stop sending data

}
``````

Note that assignment of a vaue is a single =, comparison of equality is a double ==.

Using the wrong one will not always give a compilation syntax error, bur will almost always give an unexpected or non-existent result.

In some code standards this is partially prevented by naming the const first.

``````if (x == 127)   ==>  if (127 == x)
``````

by using this code standard the compiler detects the "assignment iso compare bug".

Declarations like this:

``````int value1 = 0;
int value2 = 0;
int value3 = 0;
int value4 = 0;
int value5 = 0;
int value6 = 0;
int value7 = 0;
int option1 = 0;
int option2 = 0;
int option3 = 0;
int option4 = 0;
int option5 = 0;
int option6 = 0;
int option7 = 0;
``````

nearly always just beg for a couple of array declarations. http://arduino.cc/en/Reference/Array

``````int enableBits(int bits) {
int ret = 0;
for (int i=0; i<bits; i++) { ret |= 1 << i; }
return ret;
}
``````

You can make this:

``````if(value1 < 128)
option1 = 1;
else if(value1 < 257)
option1 = 3;
else if(value1 < 385)
option1 = 7;
else if(value1 < 512)
option1 = 15;
else if(value1 < 640)
option1 = 31;
else if(value1 < 768)
option1 = 63;
else if(value1 < 896)
option1 = 127;
else if(value1 < 1024)
option1 = 255;
``````

Into this:

``````option1 = (value1 / 128) + 1;//account for 128/128 = 0 not 1
option1 = enableBits(option1);
``````

Saves 87.5% lines

Then, following AWOL’s suggestion you can do:

``````for (int i=0; i<7; i++) {
digitalWrite(strobePin, HIGH);
digitalWrite(strobePin, LOW);
delayMicroseconds(30); // to allow the output to settle