Skip to content

Instantly share code, notes, and snippets.

@stivio00
Created September 5, 2011 01:59
Show Gist options
  • Save stivio00/1193889 to your computer and use it in GitHub Desktop.
Save stivio00/1193889 to your computer and use it in GitHub Desktop.
PWM Controller class for javelin stamp
import stamp.core.*;
/** PWM Controller class
* in main: enter to a mainloop , command from the serial
* is using pwm to control a dc motor
*/
class PwmMotorController {
PWM pwm;
char command;
int speed;
public static void main(){
PwmMotorController program =
new PwmMotorController(CPU.pin1);
program.printMenu();
program.mainLoop();
}
public PwmMotorController(int pin){
this.speed = 255;
this.pwm = new PWM(pin,speed,255);
this.pwm.start();
}
void printMenu(){
System.out.println("PWM Motor Controller");
System.out.println(" type");
System.out.println(" z : speed up");
System.out.println(" x : speed down");
}
void mainLoop(){
char command;
while(true){
if(Terminal.byteAvailable()){
command = Terminal.getChar();
if(command == 'z'){
speedUp();
}else if(command == 'x'){
speedDown();
}
pwm.update(speed,255);
}
}
}
void speedUp(){System.out.println(" + speed: " + ++speed);}
void speedDown(){System.out.println(" - speed: " + --speed);}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment