Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <Servo.h>
- double angle_rad = PI/180.0;
- double angle_deg = 180.0/PI;
- Servo servo_steer;
- Servo servo_throt;
- char inputchar = char();
- String string1 = String();
- String commandype = String();
- String commandvalue = String();
- double steering;
- double throttle;
- char lightstate = 'B';
- void setup(){
- Serial.begin(115200);
- servo_steer.attach(9); // init pin
- servo_throt.attach(10); // init pin
- // Horn
- pinMode(12,OUTPUT);
- // Headlight pwm ports
- pinMode(6,OUTPUT);
- pinMode(5,OUTPUT);
- // Taillight pwm ports
- pinMode(3,OUTPUT);
- pinMode(11,OUTPUT);
- servo_steer.write(90); // write to servo
- servo_throt.write(91); // write to servo
- analogWrite(3,50);
- analogWrite(5,50);
- analogWrite(6,255);
- analogWrite(11,255);
- }
- void receive_bt() {
- while(!((( inputchar==';'))))
- {
- _loop();
- while(!(Serial.available()))
- {
- _loop();
- }
- inputchar= char(Serial.read());
- string1 += inputchar;
- }
- }
- void loop(){
- inputchar= "";
- string1= "";
- commandype= "";
- commandvalue= "";
- receive_bt();
- commandype= string1.substring(0, 1);
- commandvalue= string1.substring(1, string1.indexOf(';'));
- Serial.print("New instructions recieved;");
- Serial.println(string1);
- Serial.println(commandype);
- Serial.println(commandvalue);
- Serial.println(steering);
- if((( commandype)==("X"))){
- steering = commandvalue.toDouble();
- servo_steer.write(steering); // write to servo
- }
- if((( commandype)==("Y"))){
- throttle = commandvalue.toDouble();
- servo_throt.write(throttle); // write to servo
- if((( string1)==("Y91;"))){
- analogWrite(3,255);
- analogWrite(11,255);
- }
- if((( string1)!=("Y91;"))){
- if ( lightstate != 'A'){
- analogWrite(3,50);
- analogWrite(11,50);}
- if ( lightstate == 'A'){
- analogWrite(3,0);
- analogWrite(11,0);}
- }
- }
- if((( commandype)==("A"))){
- analogWrite(3,0);
- analogWrite(5,0);
- analogWrite(6,0);
- analogWrite(11,0);
- lightstate = 'A';
- }
- if((( commandype)==("B"))){
- analogWrite(5,50);
- analogWrite(6,50);
- analogWrite(3,50);
- analogWrite(11,50);
- lightstate = 'B';
- }
- if((( commandype)==("C"))){
- analogWrite(5,255);
- analogWrite(6,255);
- analogWrite(3,30);
- analogWrite(11,30);
- lightstate = 'C';
- }
- if((( commandype)==("H"))){
- tone(12,262,100);
- delay(150);
- tone(12,262,100);
- delay(150);
- tone(12,262,350);
- delay(350);
- // turn taillights nback on because pwm get switched of by the tone library
- if ( lightstate != 'A'){
- analogWrite(3,30);
- analogWrite(11,30);}
- }
- inputchar= "";
- string1= "";
- }
- void _delay(float seconds){
- long endTime = millis() + seconds * 1000;
- while(millis() < endTime)_loop();
- }
- void _loop() {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement