Advertisement
Guest User

Untitled

a guest
Jan 12th, 2016
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. /*
  2. Arduino Demo Software
  3. Connect via either Arduino Serial Monitor or HyperTerminal
  4. and you can control your robot via the W.A.S.D. keys.
  5. Any other key will stop your robot in it's travels.
  6.  
  7. This example code is in the public domain.
  8. */
  9. void setup(void){
  10. Serial.begin(9600);
  11. }
  12. void loop(void){
  13. while (Serial.available() < 1) {} // Wait until a character is received
  14. char val = Serial.read();
  15. switch(val){ // Perform an action depending on the command
  16. case 'w'://Move Forward
  17. forward();
  18. break;
  19.  
  20. case 's'://Move Backwards
  21. reverse();
  22. break;
  23.  
  24. case 'a'://Turn Left
  25. left();
  26. break;
  27.  
  28. case 'd'://Turn Right
  29. right();
  30. break;
  31.  
  32. default:
  33. halt();
  34. break;
  35. }
  36. }
  37. void forward() {
  38. Serial.println("Forward");
  39. analogWrite(11, 100);
  40. analogWrite(6, 100);
  41. }
  42.  
  43. void reverse() {
  44. Serial.println("Reverse");
  45. analogWrite(10, 100);
  46. analogWrite(5, 100);
  47. }
  48.  
  49. void halt() {
  50. Serial.println("Stopped");
  51. digitalWrite(11, LOW);
  52. digitalWrite(10, LOW);
  53. digitalWrite(6, LOW);
  54. digitalWrite(5, LOW);
  55. }
  56.  
  57. void left() {
  58. Serial.println("Left");
  59. digitalWrite(10, HIGH);
  60. digitalWrite(6, HIGH);
  61. }
  62.  
  63. void right() {
  64. Serial.println("Right");
  65. digitalWrite(11, HIGH);
  66. digitalWrite(5, HIGH);
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement