Advertisement
j0h

RoboKun

j0h
Jul 11th, 2023
1,304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //This is the version with speed control,
  2. //use pins 4,5,9,10 (pwm) (on teensy 2.0)
  3.  
  4. //motor drive pins
  5. int m1FW=4;
  6. int m1RV=5;
  7. int m2FW=9;
  8. int m2RV=10;
  9.  
  10. //kill all function halt
  11. void halt();
  12.  
  13. //directional function prototypes
  14. void fw();
  15. void rv();
  16. void left();
  17. void right();
  18.  
  19.  
  20. //run time==dtime
  21. int dtime=1000; //used for turning
  22. //sped control
  23. unsigned int sped= 96;
  24.  
  25. void setup() {
  26.  Serial.begin(9600);
  27.  pinMode(m1FW, OUTPUT);
  28.  pinMode(m1RV, OUTPUT);
  29.  pinMode(m2FW, OUTPUT);
  30.  pinMode(m2RV, OUTPUT);
  31. }
  32. // lets make a fire!
  33. void loop() {
  34.  
  35.   char inByte = ' ';
  36.       if(Serial.available()){ // only send data back if data has been sent
  37.           char inByte = Serial.read(); // read the incoming data
  38.           Serial.println(inByte);
  39.   switch (inByte) {
  40.     //wasd ws=y ad=x
  41.     case 'w':
  42.       halt();
  43.       fw();
  44.       break;
  45.       case 'a':
  46.       halt();
  47.       left();  
  48.       break;    
  49.       case 's':
  50.       halt();
  51.       rv();
  52.       break;    
  53.       case 'd':
  54.       halt();
  55.       right();  
  56.       break;
  57.       case 'b':
  58.       halt();  
  59.       break;
  60.     default:
  61.       halt();  
  62.       break;
  63.         }
  64.     }
  65. }
  66.  
  67. void left(){
  68.   halt();
  69.   analogWrite(m1FW, sped);
  70.   analogWrite(m2RV, sped);  
  71.   delay(dtime);               // wait for a second
  72.   halt();
  73.   }
  74.  
  75. void right(){
  76.   halt();
  77.   analogWrite(m1RV,sped);
  78.   analogWrite(m2FW,sped);  
  79.   delay(dtime);               // wait for a second
  80.   halt();
  81.   }
  82.  
  83. void rv(){
  84.   analogWrite(m1RV,sped);
  85.   analogWrite(m2RV,sped);  
  86.   }
  87.  
  88. void fw(){
  89.   analogWrite(m1FW,sped);
  90.   analogWrite(m2FW,sped);  
  91.   }
  92.  
  93. void halt(){
  94.   //stop all that sht
  95.    analogWrite(m1FW, 0);
  96.    analogWrite(m1RV, 0);
  97.    analogWrite(m2FW, 0);
  98.    analogWrite(m2RV, 0);
  99.  
  100.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement