donmike25

bluetooth

Feb 21st, 2016
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. int motorPin1 = 8;
  2. int motorPin2 = 10;
  3. int enablePin = 5;
  4. int state;
  5. int flag=0; //makes sure that the serial only prints once the state
  6.  
  7. void setup() {
  8. // sets the pins as outputs:
  9. pinMode(motorPin1, OUTPUT);
  10. pinMode(motorPin2, OUTPUT);
  11. pinMode(enablePin, OUTPUT);
  12. // sets enablePin high so that motor can turn on:
  13. digitalWrite(enablePin, HIGH);
  14. // initialize serial communication at 9600 bits per second:
  15. Serial.begin(9600);
  16. }
  17.  
  18. void loop() {
  19. //if some date is sent, reads it and saves in state
  20. if(Serial.available() > 0){
  21. state = Serial.read();
  22. flag=0;
  23. }
  24. // if the state is '0' the DC motor will turn off
  25. if (state == '0') {
  26. digitalWrite(motorPin1, LOW);
  27. digitalWrite(motorPin2, LOW);
  28. if(flag == 0){
  29. Serial.println("Motor: off");
  30. flag=1;
  31. }
  32. }
  33. // if the state is '1' the motor will turn right
  34. else if (state == '1') {
  35. digitalWrite(motorPin1, LOW);
  36. digitalWrite(motorPin2, HIGH);
  37. if(flag == 0){
  38. Serial.println("Motor: right");
  39. flag=1;
  40. }
  41. }
  42. // if the state is '2' the motor will turn left
  43. else if (state == '2') {
  44. digitalWrite(motorPin1, HIGH);
  45. digitalWrite(motorPin2, LOW);
  46. if(flag == 0){
  47. Serial.println("Motor: left");
  48. flag=1;
  49. }
  50. }
  51.  
  52. else if (state == '3') {
  53. digitalWrite(motorPin1, HIGH);
  54. digitalWrite(motorPin2, HIGH);
  55. if(flag == 0){
  56. Serial.println("Motor: both");
  57. flag=1;
  58. }
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment