Friday, December 12, 2014

Here we have the code for the hovercraft


//Code for the Fall 2014 Design Project
//Not Your Average Housecats' Hovercraft

//Libraries required for Motor Shield
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
#include <Servo.h>

Servo rudder;  // create servo object to control a servo

int buttonPin = 2;
int rudderPotPin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin
int buttonState;
int val2;

Adafruit_MotorShield AFMS = Adafruit_MotorShield();

Adafruit_DCMotor *liftMotor = AFMS.getMotor(1);//Assign the lift motor to M1 port on Motor Shield

Adafruit_DCMotor *propulsionMotor = AFMS.getMotor(2);//Assign the propulsion motor to M2 port on Motor Shield


//Where are we putting the steering motors? What do we call them (surely not Adafruit_DCMotor)


void setup() {
  Serial.begin(9600);
  Serial.println("Hovercraft online.");
 
  rudder.attach(10);          //attaches rudder to pin 10
 
  AFMS.begin();
 
  //Turn on lift motor and set speed to maximum
  liftMotor->setSpeed(255);
  liftMotor->run(FORWARD);

 
  pinMode(buttonPin, INPUT);
  buttonState = digitalRead(buttonPin);

}

void loop(){
 
 
  val2 = digitalRead(buttonPin);
 
  if (val2 != buttonState) {
    if (val2 == LOW) {                   // the button is not pressed...
      Serial.println("Button just released");
      propulsionMotor->setSpeed(0);
      propulsionMotor->run(RELEASE);
     
    } else {                         // the button is pressed...
      Serial.println("Button just pressed");
      propulsionMotor->setSpeed(255);
      propulsionMotor->run(FORWARD);
    }
  }
 
  buttonState = val2;               // save new state in val2
 
  val = analogRead(rudderPotPin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  rudder.write(val);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there


}

No comments:

Post a Comment