Thursday, December 12, 2013

Code

#include <Servo.h>

Servo shifterServo;   // Create Servo object for sensor head servo
Servo steerServo;        // Create Servo object for second servo

const int LEDPin = 13;    //control pin for led indicator
const int DcMotorPin = 8; //control in for propulsion
int button1 = 1;
int button2 = 2;
int button3 = 3;
int button4 = 4;






void setup() {
  Serial.begin (9600);
  
  pinMode(LEDPin, OUTPUT);       // Use LED indicator (if required)
  pinMode(DcMotorPin, OUTPUT);
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(button4, INPUT);
  shifterServo.attach(10);
  steerServo.attach(9);
  
  //shift into neutral and point straight ahead
  shifterServo. write (90);
  steerServo.write (90);
}
  
  
  //point straight
  
  void pointStraight(){
    steerServo.write (90);
  }
  
  // stopping
  void stopMotor() {
    Serial.println ("stopping");
    digitalWrite(DcMotorPin, LOW);
    digitalWrite(LEDPin, LOW);
  }
  
  // move forward
  void startMotor() {
    Serial.println ("Moving Forward");
    digitalWrite(DcMotorPin, HIGH);
    digitalWrite(LEDPin, HIGH);
  }
  
  //turn right
  void turnRight() {
    Serial.println("Turning Right");
    steerServo.write(30);
    digitalWrite(LEDPin, HIGH);
  }
  
  //turn left
  void turnLeft() {
    Serial.println("Turning Left");
    steerServo.write(160);
    digitalWrite(LEDPin, HIGH);
  }
  
  //shift high
  void shiftHigh(){
    Serial.println("Shifting High");
   shifterServo.write(75);
   digitalWrite(LEDPin, HIGH);
  }
  
  //shift low
  void shiftLow() {
    Serial.println("Shifting Low");
    shifterServo.write(105);
    digitalWrite(LEDPin, HIGH);
  }
  
  void loop() {
    
    //if throttle is opened
    if (button1 == HIGH){
      shiftHigh();
      startMotor();
    if (button2 == HIGH){
      shiftLow();
      startMotor();
    if (button3 == HIGH){
      turnRight();
    if (button4 == HIGH){
      turnLeft();
    }
    else{
      pointStraight();
    }
    }
    else{
      pointStraight();
    }
    }
    else {
      stopMotor();
    }
    }
    else {
      stopMotor();
    }
  }
      

No comments:

Post a Comment