Hi all,
I've been using Arduino for about a week now. Bought a new Uno and Motorshield and never done anything with electronics before but I saw the possibilities and couldn't miss out on an opportunity to tinker!
I've recently hacked a cheap RC Car and, using the Arduino Motor Shield I have managed to scour the forums and code the forward, reverse and left, right functions on the car.
What I would now like to do is to control these functions from either W,A,S,D or D-buttons on my mac (eventually gearing up towards xbox/ps controllers akin to Blair's WiFly Mini).
I know this is a question that has been asked before many times, but for my particular example I couldn't find anything that was answered.
I've attached the code that I am using in Arduino:
Any help is greatly appreciated and over course - cited!
Cheers, Adam
int valD = 0; // variable to store values from drive and steer motors
int valS = 0;
const int driveD = 12;
const int steerD = 13;
const int brakeD = 9;
const int brakeS = 8;
const int steerA = 11;
const int motorA = 3;
void setup()
{
Serial.begin(9600);
pinMode(driveD,OUTPUT); // Drive Motor - HIGH = Forwards, LOW = BACKWARDS
pinMode(steerD,OUTPUT); // Steer Motor - HIGH = Left, LOW = Right
pinMode(brakeD,OUTPUT); // Setup Disable (Brake) the Drive Motor
pinMode(brakeS,OUTPUT); // Setup Disable (Brake) the Steer Motor
digitalWrite(brakeD,HIGH); // Enables brake for Drive Motor
digitalWrite(brakeS,HIGH); // Enables brake for Steer Motor
}
int left() //create left turn function
{
// Perform Left Turn
digitalWrite(brakeS,LOW); // Turn off steering brake
digitalWrite(steerD,HIGH); // Set direction (Left)
analogWrite(steerA,255); // Perform Turn
valS = analogRead(steerA); // get analog signal value in prep for sending to serial monitor
Serial.println(valS); // print to serial monitor
delay(1000);
digitalWrite(brakeS,HIGH); // Stop turning
delay(1000);
}
int reverse() // create reverse motion function
{
//Perform Reverse Drive
digitalWrite(brakeD, LOW); // Disable Motor Brakes
digitalWrite(driveD,LOW); // Drive Backwards
analogWrite(motorA,255); // Set low initial speed
valD = analogRead(motorA); // get analog signal value in prep for sending to serial monitor
Serial.println(valD); // print to serial monitor
delay(1000);
digitalWrite(brakeD,HIGH); // Stop driving
delay(1000);
}
int right() // create right turn function
{
// Perform Right Turn
digitalWrite(brakeS,LOW); // Turn off steering brake
digitalWrite(steerD,LOW); // Set direction (Right)
analogWrite(steerA,255); // Perform Turn
valS = analogRead(steerA); // get analog signal value in prep for sending to serial monitor
Serial.println(valS); // print to serial monitor
delay(1000);
digitalWrite(brakeS,HIGH); // Stop turning
delay(1000);
}
int forward() // create forward motion function
{
//Perform Forward Drive
digitalWrite(brakeD, LOW); // Disable Motor Brakes
digitalWrite(driveD,HIGH); // Drive Forwards
analogWrite(motorA,255); // Set low initial speed
valD = analogRead(motorA); // get analog signal value in prep for sending to serial monitor
Serial.println(valD); // print to serial monitor
delay(1000);
digitalWrite(brakeD,HIGH); // Stop driving
delay(1000);
}
void loop()
{
forward() ;
right();
}