Useing if and eles over and over to control relay with pot ?

im not understanding this i guess i have a 2 axis joy stick using 2 pots for movement of a arm with only 2 pivot points so far but i want to use the if and else and else if example to do something with my 2 pots

i want it to be setup simply like this not sure if i can do what i want this way i hoping i can

if my pot 1 is 0 to 400 turn on relay 10

else \ pot 1 is 400 to 600 delay 500 milliseconds and this free point in the center of the pots travel i want it to do nothing and remain still

else if pot 1 is 600 to 1000 turn on relay 20

now and the end of this part in the loop i want to repeat this code to have it do same actions but check pot 2 and use relays 30 and 40 this time so i can move my left to right and my up and down..

thes code below has soo meany errors its pathetic but im not any good at this soo the code below is what i want to do but i guess i cant set it up this way soo it Wont work HELP !!!!!!

int sensepinState = 0;  
int sensorValue = 1;


void setup() {
  pinMode(40, OUTPUT);//relay 10 moves arm up
  pinMode(42, OUTPUT); //relay 20 moves arm down 
  pinMode(44, OUTPUT);//relay 30 moves arm left
  pinMode(46, OUTPUT); //relay 40 moves arm right
  pinMode(50, OUTPUT); // relay relay 50 for voltage polarity swap to toggle fwd and rev of dc motor


  Serial.begin(9600);

}


void loop() {


  sensepinState = analogRead(0);// read the input pin: of up down pot


  if (sensepinState = 0,400 )// compare the sensepin to its previous state then do some thing 1st delay then print to serial

  {  


    Serial.print("sensor = " );                       
    Serial.print(sensorValue);      

    digitalWrite(50, HIGH);//FWD POLAITY ON RELAY 50
    digitalWrite(42, LOW); //MOTOR 2 RELAY OFF
    digitalWrite(40, HIGH);//MOTOR 1 RELAY ON

  }
  else  // if the state has changed, increment the steps to leds with the diffrance 
  { 
    Serial.print("sensor = " );                       
    Serial.print(sensorValue);      

    digitalWrite(50, LOW);//REV POLAITY ON RELAY 50
    digitalWrite(40, LOW);//MOTOR 1 RELAY OFF
    digitalWrite(42, HIGH);//MOTOR 2 RELAY ON


  }
  else if
  {
    // kick off any relays left on
    digitalWrite(42, LOW); //MOTOR 2 RELAY OFF
    digitalWrite(50, LOW);//REV POLAITY ON RELAY 50
    digitalWrite(40, LOW);//MOTOR 1 RELAY OFF
    
    delay(500);
    
  }
   delay(500); // start read of second pot
  {


  sensepinValue = analogRead(1);// read the input pin: left right pot


  if (sensorValue = 500 )// compare the sensepin to its previous state then do some thing 1st delay then print to serial

  {  


    Serial.print("sensor = " );                       
    Serial.print(sensorValue);      

    digitalWrite(50, HIGH);//FWD POLAITY ON RELAY 50
    digitalWrite(42, LOW); //MOTOR 2 RELAY OFF
    digitalWrite(40, HIGH);//MOTOR 1 RELAY ON

  }
  else  // if the state has changed, increment the steps to leds with the diffrance 
  { 
    Serial.print("sensor = " );                       
    Serial.print(sensorValue);      

    digitalWrite(50, LOW);//REV POLAITY ON RELAY 50
    digitalWrite(44, LOW);//MOTOR 1 RELAY OFF
    digitalWrite(46, HIGH);//MOTOR 2 RELAY ON


  }
  else if
  {
    // kick off any relays left on
    digitalWrite(46, LOW); //MOTOR 2 RELAY OFF
    digitalWrite(50, LOW);//REV POLAITY ON RELAY 50
    digitalWrite(44, LOW);//MOTOR 1 RELAY OFF
    
    delay(500);
    
  }

Moderator edit: All-caps thread subject fixed. (Nick Gammon)

if (sensepinState = 0,400 )

That's one big problem right there. I've never seen that syntax (x == y,z to mean y<x<z) used, and can find no indication that it's supposed to work in the documentation--help me out here, someone? Anyway, you should use == when testing equality, because = defines equality.
For example,

x = 0;
if (x = 1) { return TRUE; } else { return FALSE; }

Is invalid, because x = 1 stores the value 1 in x and does not return a true/false test result. Whereas

x = 0;
if (x == 1) { return TRUE; } else { return FALSE; }

Always returns false, because x is zero, and == tests for equality without asserting it.

Another problem is at

else  // if the state has changed, increment the steps to leds with the diffrance 
  { 
    <Omitted>
  }
  else if
  {
    <Omitted>
  }

First off, else if statements should always go before the final else statement, or they'll never get executed. Secondly, else if requires a test operator, just like if.

You also have a { in the middle of nowhere with no closing } (right after "delay(500); // start read of second pot"), and furthermore you're missing the closing } for your loop() void. Then you have

if (sensorValue = 500 )

again; this should be

if (sensorValue == 500 )

One last tip: when asking for help on code that returns errors, include the errors; it helps us make sure we catch everything!

xolroc:

if (sensepinState = 0,400 )

That's one big problem right there. I've never seen that syntax (x == y,z to mean y<x<z) used, and can find no indication that it's supposed to work in the documentation--help me out here, someone?

That is the "comma operator". So it is assigning 400 to sensepinState, and testing if it is true, which it will be.

OK ill keep the last part in mind to include the errors but i knew this would not compile so i didn't bother trying to

i can get a

if and
else

statement to work fine for one motor up and down the problems start with adding a 3rd thing to the statement being the else if soo your saying i need to put the else if before the else and there needs to be another analog read in there to test the pot in-between the true false and eles ? if i got that right ?

ok soo where im i going wrong with ending that segment of if to else and then wanting to repeat selecting a different pot to read ?
i cant just add them together ill work on what you hve said for now and see if it will compile thank for you help

I would also suggest you look into the switch case statement.

For my own personal use, what exactly does that do? Can it be used to test if a variable is within a range of values, as ez8mm was trying to use it?

ok well im trying to think how this works here the 0 to 400 pot read should be the if and the 600 to 1000 should be the else if
so if it returns somthing between 400 and 600 it going to exacute the final else statement right so the if == 400 is not going to givce me a rang 0 to 400 just 400 ... so i should be useing less than > 400

void loop() {


  sensepinState = analogRead(0);// read the input pin: of up down pot


  if (sensepinState < 400 )// compare the sensepin to its previous state then do some thing 1st delay then print to serial

  {  


    Serial.print("sensor = " );                       
    Serial.print(sensorValue);      

    digitalWrite(50, HIGH);//FWD POLAITY ON RELAY 50
    digitalWrite(42, LOW); //MOTOR 2 RELAY OFF
    digitalWrite(40, HIGH);//MOTOR 1 RELAY ON

  }
  else if  // if the state has changed, ?
  { 
    Serial.print("sensor = " );                       
    Serial.print(sensorValue);      

    digitalWrite(50, LOW);//REV POLAITY ON RELAY 50
    digitalWrite(40, LOW);//MOTOR 1 RELAY OFF
    digitalWrite(42, HIGH);//MOTOR 2 RELAY ON


  }
  else
  {
    // kick off any relays left on
    digitalWrite(42, LOW); //MOTOR 2 RELAY OFF
    digitalWrite(50, LOW);//REV POLAITY ON RELAY 50
    digitalWrite(40, LOW);//MOTOR 1 RELAY OFF
    
    delay(500);
    
  }

errr on pompile says unexpected '(' before '{' token

else if  // if the state has changed, ?

That should be

else if (sensepinstate >= 400 && sensepinstate < 600)

xolroc:
For my own personal use, what exactly does that do? Can it be used to test if a variable is within a range of values, as ez8mm was trying to use it?

No, not at all. In fact it isn't used much.

To test a range you need what you suggested:

if (sensepinstate >= 400 && sensepinstate < 600)

That's about the only way.

xolroc:
For my own personal use, what exactly does that do?

What it does, exactly, is evaluate the expressions and retain the last one as the result. So for example:

a = 1, 2, 3, 4, 5;

This assigns 5 to a. Not very useful, huh? Sometimes people use them in "for" loops, like this:

for (a = 2, b = 3;  
     a < 5; 
     a++, b++)

In that case the initial expression assigns 2 to a, and 3 to b. At the end of each loop it adds 1 to a, and 1 to b. So it has its uses there.

i was not able to get the ( i ) and the ( &&) to do what i wanted but by corecting where the the if else and else statement were fliping them with ecother that is . i acoplished what i wanted to to with a simple true false < 400 > 600 . that left me with a nice dead spot in my analog joy stick with a forward and reverse movement to my motor with a stop in the middle and was able to use a relay to cut poer from supply to save energy and give it a chance to cool down . :slight_smile: thanks for the guidence struggilg with the concept of c code lay outs ...