Trouble communicating to multiple servos

I have searched the internet for hours and can not find a way to fix this code. I have three different servos running off a UNO board. I am communicating through Visual Studio to the UNO. I can get only one of the servos to respond correctly. I am sending a variable through the serial port that can be 0-3, 5-12 and 60-90. thought it was easier to send one variable and code the sketch to tell which servos what to do. the only section of the “if” statement that works is the tempservo section. I can’t figure why the others don’t work, All servos do function, for I have changed the pin assignments and the tempservo moves to a different servo.

the code is as follows

#include <Servo.h>

Servo fanservo;
Servo tempservo;
Servo modeservo;

int val;
int temp;
int mode;
int fan;

void setup() 
{
  Serial.begin(19200);
  fanservo.attach(11);
  tempservo.attach(9);
  modeservo.attach(10);
}

void loop()
{
}
  void serialEvent()
  {
  val = Serial.parseInt();
    {
      if (val >= 0 && val <= 3)
      {
        if (val = 0);
        {
          fan = 0;
          fanservo.write(fan);
        }
        if (val = 1)
        {
          fan = 30;
          fanservo.write(fan);
        }
        if (val = 2)
        {
          fan = 60;
          fanservo.write(fan);
        }
        if (val = 3)
        {
          fan = 90;
          fanservo.write(fan);
        }
      }
      else if (val >= 60 && val <= 90)
      {
        temp = val - 60;
        temp = temp * 4.5;
        tempservo.write(temp);
      }
      else if (val >= 5 && val <= 12)
      {
        if (val = 5)
        {
          mode = 0;          
          modeservo.write(mode); 
        }
        if (val = 6)
        {
          mode = 30;
          modeservo.write(mode);  
        }
        if (val = 7)
        {
          mode = 60;
          modeservo.write(mode); 
        }
        if (val = 8)
        {
          mode = 90;
          modeservo.write(mode); 
        }
        if (val = 9);
        {
          mode = 120;
          modeservo.write(mode); 
        }
        if (val = 10)
        {
          mode = 150;
          modeservo.write(mode); 
        }
        if (val = 11)
        {
          mode = 180;
          modeservo.write(mode); 
        }
        if (val = 12)
        {
          mode = 210;
          modeservo.write(mode); 
        }
      }
    }
  }

tempservo is the only servo responding correctly. I know the variables are working correctly in VS.
is there something simple I am doing wrong?

if (val = 0);

That initializes val to 0

You want (val == 0) in the tests (same with 1,2,..)

Worse than that,

        if (val = 0);
        {
          fan = 0;
          fanservo.write(fan);
        }

is a complete if statement followed by an un-conditionally executed block, the if doesn't affect
whether the block runs at all.
Lose the ';' on the if-line, its a complete null-statement. You meant:

        if (val == 0)  // no semicolon here
        {
          fan = 0;
          fanservo.write(fan);
        }

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example.

You could, perhaps, send <1,30> meaning move servo 1 to 30 degrees.

…R

MarkT:
Worse than that,

        if (val = 0);

{
          fan = 0;
          fanservo.write(fan);
        }



is a complete if statement followed by an un-conditionally executed block, the if doesn't affect
whether the block runs at all.
Lose the ';' on the if-line, its a complete null-statement. You meant:


if (val == 0)  // no semicolon here
        {
          fan = 0;
          fanservo.write(fan);
        }

I must admit I stopped at the assignment

I think the right advice is for the OP to go read the basics of programming

thanks guys - with all going in the house with the kids, could not see the obvious error. everything works as it should.

it didn't mean to bother J-M-L with the obvious. when learning it can be a little discouraging when asking for help you get beat down.

If by "beat down" you mean forum users are rather politely pointing out your mistakes, be thankful. People on other forums can be quite a bit more offensive.

gadgfreak:
thanks guys - with all going in the house with the kids, could not see the obvious error. everything works as it should.

it didn't mean to bother J-M-L with the obvious. when learning it can be a little discouraging when asking for help you get beat down.

Really it's the best advice I can think of. Building on sand doesn't work in software engineering. Being honest about one's skills is the best help I can think of. It does not mean I want to discourage you - rather encourage you to start from the right place, understand the fundamentals, variable assignments, Tests , syntax. Only then you will be able to make big progress. Go for it. It was really not meant as negative. We all started there, learning the basics and I'm happy about the few kicks in the butt I got along the way. They kept me honest with myself and on the right track.

Keep cranking and you'll get there.