Go Down

Topic: Controlling Fan w/ Serial Commands (Read 95 times) previous topic - next topic

jholland181

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.

Code: [Select]


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);
    }
  }


groundFungus

#1
Apr 13, 2018, 02:06 am Last Edit: Apr 13, 2018, 02:08 am by groundFungus
  
Code: [Select]
if(command = 1){
You are probably sending the character '1' not the number 1.  
Try
 
Code: [Select]
if(command = '1'){

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

CrossRoads

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

sterretje

And in the next step, try == instead of = in your condition ;)
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

wvmarle

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.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Go Up