Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1.  
  2.  
  3. int motorPin = 9;
  4.  
  5. int buttonPin1 = 2;
  6. int buttonPin2 = 3;
  7.  
  8. int variableSpeed = 0;
  9.  
  10. void setup()
  11. {
  12. pinMode(motorPin, OUTPUT);
  13. pinMode(buttonPin1, INPUT);
  14. pinMode(buttonPin2, INPUT);
  15. Serial.begin(9600);
  16. }
  17.  
  18. void loop ()
  19. {
  20. if(digitalRead(buttonPin2) == 1)
  21. {
  22. Serial.println("B1");
  23. motorSpeed(50);
  24. }
  25. else if (digitalRead(buttonPin1) == 1)
  26. {
  27. Serial.println("B2");
  28. motorSpeed (-50);
  29. }
  30. delay(100);
  31. analogWrite(motorPin, variableSpeed);
  32. }
  33. int motorSpeed (int motorspeed){
  34. variableSpeed = motorspeed + variableSpeed;
  35.  
  36. if (variableSpeed > 255) {
  37. variableSpeed = 255;
  38. }
  39. else if (variableSpeed < 0) {
  40. variableSpeed = 0;
  41.  
  42. }
  43.  
  44. return variableSpeed;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement