For the Arduino programming part, I'm using is Arduino Leonardo. The difference of programming coding of Leonardo with Uno is the "serial" command. If i'm not mistaken, for Leonardo to communicate via Bluetooth the command will be "Serial1.xx" as below. Meanwhile for Uno will be just "Serial.xx".
Before going into Arduino programming part, you have to configure your own Bluetooth module. For example: set the baudrate, name of the Bluetooth device, and password to let your Android device connect to your Bluetooth shield. I'm doing all this setting by using a software called hyperterminal. You can try to configure your bluetooth module first because everyone are using different shield & module so I'm not gonna emphasize on it here, you can always try to google it :) (Bluetooth module configuration). Also, make sure your computer had installed the correct driver for your own Bluetooth Dongle.
Bluetooth Shield, Module and Dongle that I'm using |
#include <Servo.h> //include servo
function
#define BUFFERSIZE 127
uint8_t inBuffer[BUFFERSIZE];
int inLength; // length of data in the
buffer
int numLoop = 0; // number of times we
looped
char val;
Servo myservo; //for servo function
int motor_LF=2; //declaring pin used for
the motor
int motor_LR=3;
int motor_RF=4;
int motor_RR=5;
int motor_LPWM = 13; //declaring pwm pin
int motor_RPWM = 11;
void setup() {
Serial1.begin(38400); //baud rate I set for bluetooth module is 38400
myservo.attach(9); //my servo at pin 9 as actuator
pinMode(motor_LF, OUTPUT); //set pin as output
pinMode(motor_LR, OUTPUT);
pinMode(motor_RF, OUTPUT);
pinMode(motor_RR, OUTPUT);
pinMode(motor_LPWM, OUTPUT);
pinMode(motor_RPWM, OUTPUT);
motor_reset(); // call function to reset the motor
}
void loop() {
// read string if available
if (Serial1.available()) {
inLength = 0;
while (Serial1.available()) {
val = Serial1.read();
}
Serial1.print("Arduino received: "); //send the text to the android device
Serial1.write(val);
Serial1.println();
}
if (val == 'k') //K
{ myservo.write(90); } //write the angle of servo should move
if (val == 'l') //L
{ myservo.write(30); }
if (val == 'm') ///M
{ myservo.write(178); }
if (val == 'w') //W – Forward motion of motor
{
digitalWrite(motor_LF, HIGH);
digitalWrite(motor_LR, LOW);
digitalWrite(motor_RF, HIGH);
digitalWrite(motor_RR, LOW);
analogWrite(motor_LPWM, 255); //sending pwm pulses
analogWrite(motor_RPWM, 255); //255 will be highest
}
if (val == 's') //S – Reverse motion
{
digitalWrite(motor_LF, LOW);
digitalWrite(motor_LR, HIGH );
digitalWrite(motor_RF, LOW);
digitalWrite(motor_RR, HIGH);
analogWrite(motor_LPWM, 255);
analogWrite(motor_RPWM, 255);
}
if (val == 'q') //Q - Left Forward
{
digitalWrite(motor_LF, HIGH);
digitalWrite(motor_LR, LOW);
digitalWrite(motor_RF, HIGH);
digitalWrite(motor_RR, LOW);
analogWrite(motor_LPWM, 128); //slower thn Right PWM but same direction
to turn left
analogWrite(motor_RPWM, 255);
}
if (val == 'e') //E - Right Forward
{
digitalWrite(motor_LF, HIGH);
digitalWrite(motor_LR, LOW);
digitalWrite(motor_RF, HIGH);
digitalWrite(motor_RR, LOW);
analogWrite(motor_LPWM, 255);
analogWrite(motor_RPWM, 128);
//slower thn left PWM but same direction to turn right
}
if (val == 'z') //Z - Left backward
{
digitalWrite(motor_LF, LOW);
digitalWrite(motor_LR, HIGH);
digitalWrite(motor_RF, LOW);
digitalWrite(motor_RR, HIGH);
analogWrite(motor_LPWM, 128);
analogWrite(motor_RPWM, 255);
}
if (val == 'c') //C - Right backward
{
digitalWrite(motor_LF, LOW);
digitalWrite(motor_LR, HIGH);
digitalWrite(motor_RF, LOW);
digitalWrite(motor_RR, HIGH);
analogWrite(motor_LPWM, 255);
analogWrite(motor_RPWM, 128);
}
if (val == 'a') //A - Spin Left
{
digitalWrite(motor_LF, LOW);
digitalWrite(motor_LR, HIGH);
digitalWrite(motor_RF, HIGH);
digitalWrite(motor_RR, LOW);
analogWrite(motor_LPWM, 255); //move in same speed but different
direction to spin
analogWrite(motor_RPWM, 255);
}
if (val == 'd') //D - Spin Right
{
digitalWrite(motor_LF, HIGH);
digitalWrite(motor_LR, LOW);
digitalWrite(motor_RF, LOW);
digitalWrite(motor_RR, HIGH);
analogWrite(motor_LPWM, 255);
analogWrite(motor_RPWM, 255);
}
if (val == ' ') //Space – Brake function
{
digitalWrite(motor_LF, LOW); //make all the pin LOW to stop the motor
digitalWrite(motor_LR, LOW);
digitalWrite(motor_RF, LOW);
digitalWrite(motor_RR, LOW);
analogWrite(motor_LPWM, 255); //both 255 to make the motor stop
immediately
analogWrite(motor_RPWM, 255);
}
if (val == 't') //T – Reset the motor
{
motor_reset();
}
}
void motor_reset() //reset function
{
digitalWrite(motor_LF, LOW);
digitalWrite(motor_LR, LOW);
digitalWrite(motor_RF, LOW);
digitalWrite(motor_RR, LOW);
analogWrite(motor_LPWM, 0);
analogWrite(motor_RPWM, 0);
}//END of arduino code
Parts of the coding credits to
Nadrew Chong Jia Ying
No comments:
Post a Comment