Advertisement
Vendrick-Xander

DC Motor Variable Speed code

Jan 21st, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. const int motorOut = 9;
  2. void setup() {
  3. pinMode(motorOut, OUTPUT);
  4. Serial.begin(9600);
  5. while(! Serial)
  6. {
  7. Serial.println("Speed = 0 to 255");//user prompt for speed
  8. }
  9. }
  10.  
  11. void loop() {
  12. if(Serial.available() > 0)
  13. {
  14. int motorSpeed = Serial.parseInt();//takes input from the serial monitor
  15. if(motorSpeed >= 1 && motorSpeed <= 255)//ensures a value that is usable
  16. {
  17. analogWrite(motorOut, motorSpeed);
  18. }
  19. else if(motorSpeed == -1)//command to stop movement
  20. {
  21. analogWrite(motorOut, 0);
  22. }
  23. }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement