Controlling Fan w/ Serial Commands

Hello,

I am trying to control an Arduino with MATLAB. MATLAB will write a command (1 = power on, 2 = power off) to power pin 13. The issue I am having with my code is that the Arduino is non-responsive to any commands. I am currently using an LED since the fan is shipping.

int fan = 13;
char command;

void setup() {
  // put your setup code here, to run once:
  int fan = 13;
  Serial.begin(9600);
  pinMode(fan, OUTPUT); //sets pin 13 for output for fan
}


void loop() {
  // put your main code here, to run repeatedly:
 if(Serial.available() > 0){
    command = Serial.read();
    if(command = 1){
      //Serial.print(command);
      digitalWrite(fan, HIGH);
    }
    else
      Serial.print(command);
      digitalWrite(fan, LOW);
    }
  }
if(command = 1){

You are probably sending the character '1' not the number 1. Try

if(command = '1'){

For future reference the serial input basics thread may be of interest.

Also need == vs just 1 =.

And in the next step, try == instead of = in your condition ;)

jholland181: I am trying to control an Arduino with MATLAB. MATLAB will write a command (1 = power on, 2 = power off)

Are those commands sent as the ASCII characters '1' and '2' or as the numerical values 1 and 2? That alone makes a big difference in how to test for it in your code.