Arduino Projects: Difference between revisions

Line 442:
Source: [http://www.instructables.com/id/Arduino-Bluetooth-RC-Car-Android-Controlled/ instructables.com], [http://www.instructables.com/id/Control-DC-and-stepper-motors-with-L298N-Dual-Moto/ instructables.com]
 
<pre style="width: 75%; height: 10pc; overflow-y: scroll;">const int motor1Pin1 = 10;
const int motor1Pin2 = 11;
const int enablem1Pin3 = 12;
const int motor2Pin1 = 6;
const int motor2Pin2 = 5;
const int enablem2Pin3 = 3;
 
byte serialA;
 
void setup() {
Serial.begin(9600);
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(enablem1Pin3, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
pinMode(enablem2Pin3, OUTPUT);
}
 
void loop() {
if (Serial.available() > 0)
{
serialA = Serial.read();
Serial.println(serialA);
}
switch (serialA) {
// forward
case 'F':
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
digitalWrite(enablem1Pin3, HIGH);
digitalWrite(enablem2Pin3, HIGH);
break;
// left
case 'L':
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
digitalWrite(enablem1Pin3, HIGH);
digitalWrite(enablem2Pin3, LOW);
break;
// right
case 'R':
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
digitalWrite(enablem1Pin3, LOW);
digitalWrite(enablem2Pin3, HIGH);
break;
// forward left
case 'G':
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
digitalWrite(enablem1Pin3, HIGH);
digitalWrite(enablem2Pin3, HIGH);
break;
 
// forward right
case 'I':
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
digitalWrite(enablem1Pin3, HIGH);
digitalWrite(enablem2Pin3, HIGH);
break;
 
// backward left
case 'H':
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
digitalWrite(enablem1Pin3, HIGH);
digitalWrite(enablem2Pin3, HIGH);
break;
// backward right
case 'J':
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
digitalWrite(enablem1Pin3, HIGH);
digitalWrite(enablem2Pin3, HIGH);
break;
// backward
case 'B':
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
digitalWrite(enablem1Pin3, HIGH);
digitalWrite(enablem2Pin3, HIGH);
break;
// Stop
case 'S':
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
digitalWrite(enablem1Pin3, LOW);
digitalWrite(enablem2Pin3, LOW); }
}
</pre>
 
==Bluetooth RC Car with Speed Control==
 
<pre style="width: 75%; height: 10pc; overflow-y: scroll;"> const int motorA1 = 11; // L293 Connection Pin 2 of L293
const int motorA2 = 10; // Pin 7 of L293
const int motorB1 = 6; // Pin 10 of L293
const int motorB2 = 5; // Pin 14 of L293
 
const int lights = 13; //Leds connected to Arduino UNO Pin 12
const int buzzer = 8 ; //Buzzer,Speaker to Arduino UNO Pin 3
const int BTState = 7; //Bluetooth (HC-06 JY-MCU) State pin on pin 2 of Arduino
//Calculate Battery Level
const float maxBattery = 8.0; // Change value to your max battery voltage level!
int perVolt; // Percentage variable
float voltage = 0.0; // Read battery voltage
int level;
// Use it to make a delay... without delay() function!
long previousMillis = -1000*10;// -1000*10=-10sec. to read the first value. If you use 0 then you will take the first value after 10sec.
long interval = 1000*10; // interval at which to read battery voltage, change it if you want! (10*1000=10sec)
unsigned long currentMillis; //unsigned long currentMillis;
 
int i=0; //Useful Variables
int j=0;
int state;
int vSpeed=200; // Default speed, from 0 to 255
 
void setup() {
pinMode(motorA1, OUTPUT); // Set pins as outputs:
pinMode(motorA2, OUTPUT);
pinMode(motorB1, OUTPUT);
pinMode(motorB2, OUTPUT);
pinMode(lights, OUTPUT);
pinMode(BTState, INPUT);
Serial.begin(9600); // Initialize serial communication at 9600 bits per second:
}
void loop() {
if(digitalRead(BTState)==LOW) { state='S'; } //Stop car when connection lost or bluetooth disconnected
if(Serial.available() > 0){ //Save income data to variable 'state'
state = Serial.read();
}
if (state == '0'){ //Change speed if state is equal from 0 to 4. Values must be from 0 to 255 (PWM)
vSpeed=0;}
else if (state == '1'){
vSpeed=100;}
else if (state == '2'){
vSpeed=200;}
else if (state == '3'){
vSpeed=300;}
else if (state == '4'){
vSpeed=400;}
else if (state == '5'){
vSpeed=500;}
else if (state == '6'){
vSpeed=600;}
else if (state == '7'){
vSpeed=700;}
else if (state == '8'){
vSpeed=800;}
else if (state == '9'){
vSpeed=900;}
else if (state == 'q'){
vSpeed=1000;}
Serial.println(vSpeed);
Serial.println(state);
/***********************Forward****************************/
//If state is equal with letter 'F', car will go forward!
if (state == 'F') {
analogWrite(motorA1, vSpeed);
analogWrite(motorA2, 0);
analogWrite(motorB1, 0);
analogWrite(motorB2, 0);
}
/**********************Forward Left************************/
//If state is equal with letter 'G', car will go forward left
else if (state == 'G') {
analogWrite(motorA1, vSpeed);
analogWrite(motorA2, 0);
analogWrite(motorB1, 200);
analogWrite(motorB2, 0);
}
/**********************Forward Right************************/
//If state is equal with letter 'I', car will go forward right
else if (state == 'I') {
analogWrite(motorA1, vSpeed);
analogWrite(motorA2, 0);
analogWrite(motorB1, 0);
analogWrite(motorB2, 200);
}
/***********************Backward****************************/
//If state is equal with letter 'B', car will go backward
else if (state == 'B') {
analogWrite(motorA1, 0);
analogWrite(motorA2, vSpeed);
analogWrite(motorB1, 0);
analogWrite(motorB2, 0);
}
/**********************Backward Left************************/
//If state is equal with letter 'H', car will go backward left
else if (state == 'H') {
analogWrite(motorA1, 0);
analogWrite(motorA2, vSpeed);
analogWrite(motorB1, 200);
analogWrite(motorB2, 0);
}
/**********************Backward Right************************/
//If state is equal with letter 'J', car will go backward right
else if (state == 'J') {
analogWrite(motorA1, 0);
analogWrite(motorA2, vSpeed);
analogWrite(motorB1, 0);
analogWrite(motorB2, 200);
}
/***************************Left*****************************/
//If state is equal with letter 'L', wheels will turn left
else if (state == 'L') {
analogWrite(motorA1, 0);
analogWrite(motorA2, 0);
analogWrite(motorB1, 200);
analogWrite(motorB2, 0);
}
/***************************Right*****************************/
//If state is equal with letter 'R', wheels will turn right
else if (state == 'R') {
analogWrite(motorA1, 0);
analogWrite(motorA2, 0);
analogWrite(motorB1, 0);
analogWrite(motorB2, 200);
}
/************************Lights*****************************/
//If state is equal with letter 'W', turn leds on or of off
else if (state == 'W') {
if (i==0){
digitalWrite(lights, HIGH);
i=1;
}
else if (i==1){
digitalWrite(lights, LOW);
i=0;
}
state='n';
}
/**********************Horn sound***************************/
//If state is equal with letter 'V', play (or stop) horn sound
else if (state == 'V'){
if (j==0){
tone(buzzer, 1000);//Speaker on
j=1;
}
else if (j==1){
noTone(buzzer); //Speaker off
j=0;
}
state='n';
}
/************************Stop*****************************/
//If state is equal with letter 'S', stop the car
else if (state == 'S'){
analogWrite(motorA1, 0);
analogWrite(motorA2, 0);
analogWrite(motorB1, 0);
analogWrite(motorB2, 0);
}
/***********************Battery*****************************/
currentMillis = millis(); //Read battery voltage every 10sec.
if(currentMillis - (previousMillis) > (interval)) {
previousMillis = currentMillis;
voltage = (analogRead(A0)*5.015 / 1024.0)*11.132; //Read voltage from analog pin A0 and make calibration
//Calculate percentage...
perVolt = (voltage*100)/ maxBattery;
if (perVolt<=75) { level=0; }
else if (perVolt>75 && perVolt<=80) { level=1; } // Battery level
else if (perVolt>80 && perVolt<=85) { level=2; } //Min ------------------------ Max
else if (perVolt>85 && perVolt<=90) { level=3; } // | 0 | 1 | 2 | 3 | 4 | 5 | >
else if (perVolt>90 && perVolt<=95) { level=4; } // ------------------------
else if (perVolt>95) { level=5; }
Serial.println(level);
}
}
</pre>