Advertisement
wluijkx

VW T1 Arduino code

Jan 27th, 2020
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.07 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <Servo.h>
  3.  
  4. double angle_rad = PI/180.0;
  5. double angle_deg = 180.0/PI;
  6. Servo servo_steer;
  7. Servo servo_throt;
  8. char inputchar = char();
  9. String string1 = String();
  10. String commandype = String();
  11. String commandvalue = String();
  12. double steering;
  13. double throttle;
  14. char lightstate = 'B';
  15.  
  16.  
  17. void setup(){
  18.     Serial.begin(115200);
  19.     servo_steer.attach(9); // init pin
  20.     servo_throt.attach(10); // init pin
  21. // Horn
  22.     pinMode(12,OUTPUT);
  23. // Headlight pwm ports
  24.     pinMode(6,OUTPUT);
  25.     pinMode(5,OUTPUT);
  26. // Taillight pwm ports
  27.     pinMode(3,OUTPUT);
  28.     pinMode(11,OUTPUT);
  29.  
  30.     servo_steer.write(90); // write to servo
  31.     servo_throt.write(91); // write to servo
  32.        analogWrite(3,50);
  33.        analogWrite(5,50);
  34.        analogWrite(6,255);
  35.        analogWrite(11,255);
  36.  
  37. }
  38.  
  39. void receive_bt() {
  40.     while(!((( inputchar==';'))))
  41.     {
  42.         _loop();
  43.         while(!(Serial.available()))
  44.         {
  45.             _loop();
  46.         }
  47.          inputchar= char(Serial.read());
  48.          string1 += inputchar;
  49.     }
  50. }
  51.  
  52. void loop(){
  53.  
  54.  
  55.  
  56. inputchar= "";
  57. string1= "";
  58. commandype= "";
  59. commandvalue= "";
  60.  
  61.   receive_bt();  
  62.  
  63.     commandype= string1.substring(0, 1);
  64.     commandvalue= string1.substring(1, string1.indexOf(';'));
  65.    
  66.    
  67.  
  68.     Serial.print("New instructions recieved;");
  69.     Serial.println(string1);
  70.     Serial.println(commandype);
  71.     Serial.println(commandvalue);
  72.     Serial.println(steering);
  73.  
  74.  
  75.    if((( commandype)==("X"))){
  76.     steering = commandvalue.toDouble();
  77.         servo_steer.write(steering); // write to servo
  78.     }    
  79.  
  80.    if((( commandype)==("Y"))){
  81.     throttle = commandvalue.toDouble();
  82.         servo_throt.write(throttle); // write to servo
  83.    if((( string1)==("Y91;"))){
  84.  
  85.        analogWrite(3,255);
  86.        analogWrite(11,255);
  87.     }    
  88.    if((( string1)!=("Y91;"))){
  89.     if ( lightstate != 'A'){
  90.        analogWrite(3,50);
  91.        analogWrite(11,50);}
  92.     if ( lightstate == 'A'){
  93.        analogWrite(3,0);
  94.        analogWrite(11,0);}
  95.     }
  96.    }    
  97.  
  98.    if((( commandype)==("A"))){
  99.  
  100.        analogWrite(3,0);
  101.        analogWrite(5,0);
  102.        analogWrite(6,0);
  103.        analogWrite(11,0);
  104.        lightstate = 'A';
  105.    }
  106.  
  107.    if((( commandype)==("B"))){
  108.  
  109.  
  110.        analogWrite(5,50);
  111.        analogWrite(6,50);
  112.        analogWrite(3,50);
  113.        analogWrite(11,50);
  114.        lightstate = 'B';
  115.    }
  116.  
  117.    if((( commandype)==("C"))){
  118.  
  119.        analogWrite(5,255);
  120.        analogWrite(6,255);
  121.        analogWrite(3,30);
  122.        analogWrite(11,30);
  123.        lightstate = 'C';
  124.    }
  125.  
  126.    if((( commandype)==("H"))){
  127.     tone(12,262,100);
  128.     delay(150);
  129.     tone(12,262,100);
  130.     delay(150);
  131.     tone(12,262,350);
  132.     delay(350);
  133. // turn taillights nback on because pwm get switched of by the tone library
  134.     if ( lightstate != 'A'){
  135.        analogWrite(3,30);
  136.        analogWrite(11,30);}
  137.  
  138.     }    
  139.  
  140.    
  141.     inputchar= "";
  142.     string1= "";
  143. }
  144.  
  145. void _delay(float seconds){
  146.     long endTime = millis() + seconds * 1000;
  147.     while(millis() < endTime)_loop();
  148. }
  149. void _loop() {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement