Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // MrLunk's Autonomous Vehikel 2017 - Serial motion control
- #include <Servo.h>
- #include <Wire.h>
- Servo myservo;
- // const int led = 13;
- int usbRead =0;
- Servo SteeringServo;
- int MaxSteerAngle = 30;
- int Speed = 100;
- int MaxSpeed = 60;
- int RPWM_Output = 5; // Arduino PWM output pin 5; connect to IBT-2 pin 1 (RPWM)
- int LPWM_Output = 6; // Arduino PWM output pin 6; connect to IBT-2 pin 2 (LPWM)
- // --------------------------------------------------------------
- void setup() {
- Serial.begin(9600);
- // pinMode(led, OUTPUT);
- // digitalWrite(led, LOW);
- pinMode(RPWM_Output, OUTPUT);
- pinMode(LPWM_Output, OUTPUT);
- SteeringServo.attach(7);
- }
- // --------------------------------------------------------------
- void loop() {
- if(Serial.available()) {
- usbRead = Serial.read() - '0';
- Serial.println(usbRead);
- if(usbRead == 8) {
- Forwards();
- usbRead = 0;
- }
- if(usbRead == 7) {
- StopEngine();
- usbRead = 0;
- }
- if(usbRead == 5) {
- SteerStraight();
- usbRead = 0;
- }
- if(usbRead == 2) {
- Backwards();
- usbRead = 0;
- }
- if(usbRead == 4) {
- SteerRight();
- usbRead = 0;
- }
- if(usbRead == 6) {
- SteerLeft();
- usbRead = 0;
- }
- }
- }
- // --------------Routine definitions------------------------------
- void Forwards(){
- analogWrite(LPWM_Output, 0);
- analogWrite(RPWM_Output, MaxSpeed);
- // delay(200);
- }
- void SteerStraight(){
- SteeringServo.write(90);
- }
- void StopEngine(){
- analogWrite(LPWM_Output, 0);
- analogWrite(RPWM_Output, 0);
- }
- void Backwards(){
- analogWrite(LPWM_Output, MaxSpeed); // Backwards
- analogWrite(RPWM_Output, 0);
- // delay(200);
- }
- void SteerRight(){
- SteeringServo.write(90 - MaxSteerAngle);
- }
- void SteerLeft(){
- SteeringServo.write(90 + MaxSteerAngle);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement