Skip to content

Instantly share code, notes, and snippets.

@stivio00
Created September 5, 2011 02:03
Show Gist options
  • Save stivio00/1193898 to your computer and use it in GitHub Desktop.
Save stivio00/1193898 to your computer and use it in GitHub Desktop.
A Speed driver ussing PWM and a Arduino
int pin = 11;
int val = 255;
int command;
void printMsg(){
Serial.println("Welcome to speed controller");
Serial.println(" type ");
Serial.println(" - z : to speed up ");
Serial.println(" - x : to speed down");
Serial.println(" - s : set manually(submenu)");
}
void setup(){
Serial.begin(9600);
pinMode(pin, OUTPUT);
printMsg();
}
void loop(){
analogWrite(pin,val);
if(Serial.available()>0){
command = Serial.read();
if(command == int('z')){
val = val +1;
Serial.println("+ speed \n ->");
Serial.print(val*100/255);
Serial.println("% ");
}else if(command == int('x')){
val = val -1;
Serial.println("- speed");
Serial.print(val*100/255);
Serial.println("% ");
}else if(command == int('s')){
Serial.println("o : off");
Serial.println("f : full on");
while(Serial.available()>0){
analogWrite(pin,val);
command = Serial.read();
if(command == int('o')){
val = 0;
Serial.println(" -> speed: 0%");
}else if(command == int('f')){
val = 255;
Serial.println(" -> speed: 100%");
}else{
Serial.println("Unknow command");
continue;
}
}
}else{
Serial.println("unknow command:"+command);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment