Fan speed control

View previous topic View next topic Go down

Fan speed control

Post by oheeela on Thu May 12, 2016 4:30 am

So i tried to follow roboremo's dc motor speed project but changed the arduino coding and just add a diode to my circuit. I wanted the motor to only have three fixed speed. My problem is the motor turns on right away when powered on and not after command is given as it supposed to.And i'm not sure if the problem is because of the connection or the coding. Can someone please help?

Below is my schematic and my coding:


Code:
int motorPin = 9;
int speed;  
String readString = "";  

void setup() {
  pinMode(motorPin, speed);  
  analogWrite (motorPin, 0);
  Serial.begin(9600);
}

void loop() {
 if (Serial.available()>0);  
 {
  delay(3);
  char c = Serial.read();
  readString += c;
  
 if(readString.length() > 0){
  Serial.print(readString);
  
  if (readString == "on") {
    analogWrite(motorPin, 80);    
    if (readString == "slow"){
     analogWrite(motorPin, 80);
    }
    if (readString == "medium"){
     analogWrite (motorPin, 170);
    }
    if (readString == "fast"){  
     analogWrite (motorPin, 255);
    }
   }
 if (readString == "off"){
  analogWrite(motorPin, 0);
 }
 }
 }
}

oheeela

Posts : 2
Join date : 2016-05-12

View user profile

Back to top Go down

Re: Fan speed control

Post by Admin on Thu May 12, 2016 7:31 am

Circuit is OK.

There are problems with the code.

1) Why you added delay(3); ?

2) if (readString == "on")
I'm not sure that Arduino can compare strings with ==

3) if (readString == "on") {
analogWrite(motorPin, 80);
if (readString == "slow"){ ...
Second if is inside the first if, but string can not be "on" and "slow" at the same time, so the second if will never execute.

4) char c = Serial.read();
readString += c;
You keep adding characters to readString, but you never reset it.
If you send "on", then "slow", you think readString will be "slow",
but it will be "onslow".

Try to use an example from roboremo.com, and modify the exeCmd function.

Admin
Admin

Posts : 111
Join date : 2016-03-08

View user profile http://roboremo.makeforum.net

Back to top Go down

Re: Fan speed control

Post by oheeela on Tue May 17, 2016 5:18 pm

I did try to do exactly as from roboremo but my motor doesnt rotate at all. The app was connected to the HC-05 but when i moved the slider nothing happen. so i dont know what goes wrong.

Here is my circuit just in case something's wrong with it.


oheeela

Posts : 2
Join date : 2016-05-12

View user profile

Back to top Go down

Re: Fan speed control

Post by Admin on Fri Jun 03, 2016 9:24 am

Make sure you use the same BaudRate in HC-05 and Arduino sketch.

Admin
Admin

Posts : 111
Join date : 2016-03-08

View user profile http://roboremo.makeforum.net

Back to top Go down

Re: Fan speed control

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum