#include <L298N.h> // Library for L298N Motor Driver Module
#include <Ultrasonic.h> // Library for Ultrasonic sensor
#include <LedControl.h> // Library for Led Matrix 8x8
//START CONFIGURATION LED MATRIX
int DIN = 35;
int CS = 34;
int CLK = 32;
LedControl lc = LedControl(DIN, CLK, CS, 0);
int NUMBYTE = 1;
String NUMBYTE_str;
//END CONFIGURATION LED MATRIX
//START CONFIGURATION Voltage sensor
const int voltageSensorPin = A3; // sensor pin
float vIn; // measured voltage (3.3V = max. 16.5V, 5V = max 25V)
float vOut;
float voltageSensorVal; // value on pin A3 (0 - 1023)
const float factor = 5.128; // reduction factor of the Voltage Sensor shield
const float vCC = 5.00; // Arduino input voltage (measurable by voltmeter)
//END CONFIGURATION Voltage sensor
//START CONFIGURATION Front Ultrasonic Module pins
const int trigPin = 52; // 10 microsecond high pulse causes chirp , wait 50 us
const int echoPin = 53; // Width of high pulse indicates distance
//END CONFIGURATION Front Ultrasonic Module pins
// START CONFIGURATION Left Ultrasonic Module pins
const int Left_trigPin = 46; // 10 microsecond high pulse causes chirp , wait 50 us
const int Left_echoPin = 47; // Width of high pulse indicates distance
// END CONFIGURATION Left Ultrasonic Module pins
// START CONFIGURATION RightUltrasonic Module pins
const int Right_trigPin = 40; // 10 microsecond high pulse causes chirp , wait 50 us
const int Right_echoPin = 41; // Width of high pulse indicates distance
// END CONFIGURATION RightUltrasonic Module pins
boolean first_run = true;
int randNumberdelay = 2000; // Initialize Variable
int buzzerPin = 2;
int pin_AUTO = 8; //switch Auto /man (REMINDER: ENABLE PULL UP RESISTOR)
int pin_RIGHT = 23; //Right Limit switch
int pin_LEFT = 22; //Left Limit switch
int pin_BLADES = 4; //Relay for blade motor
int pin_anti_tipping = 24; // Overturning Sensor
// Read distance from the ultrasonic sensor , return distance in mm //
// Speed of sound in dry air , 20C is 343 m/s
// pulseIn returns time in microseconds (10^-6)
// 2d=p*10^-6s*343m/s=p*0.00343m=p*0.343mm/us
unsigned int readDistance()
{
digitalWrite ( trigPin , HIGH );
delayMicroseconds (10);
digitalWrite ( trigPin , LOW );
unsigned long period = pulseIn ( echoPin, HIGH );
return period * 343 / 2000;
}
Ultrasonic ultrasonic(52, 53);
int distance;
unsigned int readDistance_Left()
{
digitalWrite ( Left_trigPin , HIGH );
delayMicroseconds (10);
digitalWrite ( Left_trigPin , LOW );
unsigned long period = pulseIn ( Left_echoPin, HIGH );
return period * 343 / 2000;
}
Ultrasonic Left_ultrasonic(46, 47);
int Left_distance;
unsigned int readDistance_Right()
{
digitalWrite ( Right_trigPin , HIGH );
delayMicroseconds (10);
digitalWrite ( Right_trigPin , LOW );
unsigned long period = pulseIn ( Right_echoPin, HIGH );
return period * 343 / 2000;
}
Ultrasonic Right_ultrasonic(40, 41);
int Right_distance;
/* The setup() function is called when a sketch starts. It is used to initialize variables, pin modes, start using libraries, etc. This function will only run once, after each power up or reset of the Arduino board. */
void setup()
{
Serial.begin(9600); // Baud Rate for Serial Communication
Serial1.begin (9600); // Baud Rate for Serial Bluetooth Communication
pinMode(10, INPUT); // Digital Pin 10 as Input Pin for Motor A
pinMode(9, INPUT); // Digital Pin 9 as Input Pin for Motor A
pinMode(7, INPUT); // Digital Pin 7 as Input Pin for Motor B
pinMode(6, INPUT); // Digital Pin 6 as Input Pin for Motor B
pinMode(11, INPUT); // PWM Pin 11 as Input Pin for Motor A
pinMode(5, INPUT); // PWM Pin 5 as Input Pin for Motor B
pinMode(pin_BLADES, OUTPUT); // Relay for blade motor
pinMode(buzzerPin, OUTPUT); // Buzzer
pinMode(pin_AUTO, INPUT); //switch Auto / BLUETOOTH man
digitalWrite(pin_AUTO, HIGH); //enable pull up resistor (Needed because the switch does not have resistence)
pinMode(pin_RIGHT, INPUT); // Digital Pin 22 as Input Pin for Right Limit switch
pinMode(pin_LEFT, INPUT); // Digital Pin 23 as Input Pin for Left Limit switch
pinMode(pin_anti_tipping, INPUT); // Digital Pin 24 as Input Pin for Overturning Sensor
//LED MATRIX
lc.shutdown(0, false);
lc.setIntensity(0, 15); //Adjust the brightness maximum is 15
lc.clearDisplay(0);
}
void loop()
// put your main code here, to run repeatedly:
// FOR DEBUG PURPOSE UNCOMMENT Serial.print()
{
// START LED MATRIX
//Facial Expression
byte smile[8] = {0x3C, 0x42, 0x99, 0xA5, 0x81, 0xA5, 0x42, 0x3C};
byte neutral[8] = {0x3C, 0x42, 0xBD, 0x81, 0x81, 0xA5, 0x42, 0x3C};
byte sad[8] = {0x3C, 0x42, 0xA5, 0x99, 0x81, 0xA5, 0x42, 0x3C};
//Arrow
byte arrow_up[8] = {0x18, 0x3C, 0x7E, 0xFF, 0x18, 0x18, 0x18, 0x18};
byte arrow_down[8] = {0x18, 0x18, 0x18, 0x18, 0xFF, 0x7E, 0x3C, 0x18};
byte arrow_left[8] = {0x08, 0x0C, 0x0E, 0xFF, 0xFF, 0x0E, 0x0C, 0x08};
byte arrow_right[8] = {0x10, 0x30, 0x70, 0xFF, 0xFF, 0x70, 0x30, 0x10};
byte arrow_up_right[8] = {0x03, 0x87, 0x8E, 0x9C, 0xB8, 0xF0, 0xE0, 0xFE};
byte arrow_down_right[8] = {0xFE, 0xE0, 0xF0, 0xB8, 0x9C, 0x8E, 0x87, 0x03};
byte arrow_up_left[8] = {0xC1, 0xE1, 0x71, 0x39, 0x1D, 0x0F, 0x07, 0xFF};
byte arrow_down_left[8] = {0x7F, 0x07, 0x0F, 0x1D, 0x39, 0x71, 0xE1, 0xC0};
//Alternate Pattern
byte d1[8] = {0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55};
byte d2[8] = {0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA};
//Moving car
byte b1[8] = {0x00, 0x00, 0x00, 0x00, 0x18, 0x3C, 0x18, 0x3C};
byte b2[8] = {0x00, 0x00, 0x00, 0x18, 0x3C, 0x18, 0x3C, 0x00};
byte b3[8] = {0x00, 0x00, 0x18, 0x3C, 0x18, 0x3C, 0x00, 0x00};
byte b4[8] = {0x00, 0x18, 0x3C, 0x18, 0x3C, 0x00, 0x00, 0x00};
byte b5[8] = {0x18, 0x3C, 0x18, 0x3C, 0x00, 0x00, 0x00, 0x00};
byte b6[8] = {0x3C, 0x18, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x18};
byte b7[8] = {0x18, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x18, 0x3C};
byte b8[8] = {0x3C, 0x00, 0x00, 0x00, 0x00, 0x18, 0x3C, 0x18};
//STOP
byte halt[8] = {0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81};
// END LED MATRIX
voltageSensorVal = analogRead(voltageSensorPin); // read the current sensor value (0 - 1023)
vOut = (voltageSensorVal / 1024) * vCC; // convert the value to the real voltage on the analog pin
vIn = vOut * factor; // convert the voltage on the source by multiplying with the factor
/* Serial.print("Voltage = ");
Serial.print(vIn);
Serial.println("V");
//delay(1000);
*/
// val = digitalRead(pin_AUTO);
if (digitalRead(pin_AUTO) == HIGH) // read the AUTO/Bluetooth switch
{
//************************START AUTO MODE************************
if (first_run == true) {
if (vIn < 9)
{
printByte(sad); //If voltage battery is below 9V the facial expression is sad
} else {
printByte(smile);//If voltage battery is over 9V the facial expression is smile
}
// at the beginning wait 5sec and activate the buzzer three times
delay(5000);
digitalWrite(buzzerPin, HIGH);
delay(1000);
digitalWrite(buzzerPin, LOW);
delay(1000);
digitalWrite(buzzerPin, HIGH);
delay(1000);
digitalWrite(buzzerPin, LOW);
delay(1000);
digitalWrite(buzzerPin, HIGH);
delay(1000);
digitalWrite(buzzerPin, LOW);
delay(1000);
digitalWrite(pin_BLADES, HIGH);
first_run = false;
//Serial.println("first_run! ");
}
distance = ultrasonic.read();
Left_distance = Left_ultrasonic.read();
Right_distance = Right_ultrasonic.read();
//Serial.println(distance);
int vicino;
int randNumberL;
int randNumberR;
int randNumberRETRO;
//Serial.print("distance in cm: ");
//Serial.println(distance);
//delay(1000);
if (digitalRead(pin_LEFT) == LOW) // read the LEFT limit switch
{
//Serial.print("PHISICAL OBSTACLE ON LEFT! ");
Stop();
printByte(halt);
delay(1000);
//GO REAR
printByte(arrow_up);
digitalWrite(10, HIGH); // Pin 10 is High for Motor A
digitalWrite(9, LOW); // Pin 9 is Low
digitalWrite(6, LOW); // Pin 6 is Low
digitalWrite(7, HIGH); // Pin 7 is High for Motor B
delay(1000);
Stop();
delay(1000);
//TURN RIGHT
printByte(arrow_right);
digitalWrite(10, LOW); // Pin 10 is Low
digitalWrite(9, HIGH); // Pin 9 is High for Motor A
digitalWrite(6, LOW); // Pin 6 is Low
digitalWrite(7, HIGH); // Pin 7 is High for Motor B
randNumberdelay = random(2000, 3000);
delay(randNumberdelay);
Stop();
delay(1000);
} else if (digitalRead(pin_RIGHT) == LOW) // read the RIGHT limit switch
{
//Serial.print("PHISICAL OBSTACLE ON RIGHT! ");
Stop();
printByte(halt);
delay(1000);
//GO REAR
printByte(arrow_up);
digitalWrite(10, HIGH); // Pin 10 is High for Motor A
digitalWrite(9, LOW); // Pin 9 is Low
digitalWrite(6, LOW); // Pin 6 is Low
digitalWrite(7, HIGH); // Pin 7 is High for Motor B
delay(1000);
Stop();
delay(1000);
//TURN LEFT
printByte(arrow_left);
digitalWrite(10, HIGH); // Pin 10 is High for Motor A
digitalWrite(9, LOW); // Pin 9 is Low
digitalWrite(6, HIGH); // Pin 6 is High for Motor B
digitalWrite(7, LOW); // Pin 7 is Low
randNumberdelay = random(2000, 3000);
delay(randNumberdelay);
Stop();
delay(1000);
}
//ANTI-TIPPING SENSOR
else if (digitalRead(pin_anti_tipping) == LOW)
{
//Serial.print("DANGER! ");
digitalWrite(pin_BLADES, LOW);
printByte(sad);
Stop();
first_run = true;
delay(5000);
// END ANTI-TIPPING
}
else if (Left_distance < 20 ) {
// Serial.print("OBSTACLE ON LEFT! ");
Stop();
printByte(halt);
delay(1000);
//GO REAR
printByte(arrow_up);
digitalWrite(10, HIGH); // Pin 10 is High for Motor A
digitalWrite(9, LOW); // Pin 9 is Low
digitalWrite(6, LOW); // Pin 6 is Low
digitalWrite(7, HIGH); // Pin 7 is High for Motor B
delay(1000);
Stop();
delay(1000);
//TURN RIGHT
printByte(arrow_right);
digitalWrite(10, LOW); // Pin 10 is Low
digitalWrite(9, HIGH); // Pin 9 is High for Motor A
digitalWrite(6, LOW); // Pin 6 is Low
digitalWrite(7, HIGH); // Pin 7 is High for Motor B
randNumberdelay = random(2000, 3000);
delay(randNumberdelay);
Stop();
}
else if (Right_distance < 20 ) {
// Serial.print("OBSTACLE ON RIGHT! ");
Stop();
printByte(halt);
delay(1000);
//GO REAR
printByte(arrow_up);
digitalWrite(10, HIGH); // Pin 10 is High for Motor A
digitalWrite(9, LOW); // Pin 9 is Low
digitalWrite(6, LOW); // Pin 6 is Low
digitalWrite(7, HIGH); // Pin 7 is High for Motor B
delay(1000);
Stop();
delay(1000);
//TURN LEFT
printByte(arrow_left);
digitalWrite(10, HIGH); // Pin 10 is High for Motor A
digitalWrite(9, LOW); // Pin 9 is Low
digitalWrite(6, HIGH); // Pin 6 is High for Motor B
digitalWrite(7, LOW); // Pin 7 is Low
randNumberdelay = random(2000, 3000);
delay(randNumberdelay);
Stop();
}
else if (distance > 20 ) {
//GO FORWARD
// Serial.print("FORWARD! ");
switch (NUMBYTE) {
case 1:
printByte(b1);
break;
case 2:
printByte(b2);
break;
case 3:
printByte(b3);
break;
case 4:
printByte(b4);
break;
case 5:
printByte(b5);
break;
case 6:
printByte(b6);
break;
case 7:
printByte(b7);
break;
case 8:
printByte(b8);
break;
default:
printByte(b1);
break;
}
delay(50);
NUMBYTE = NUMBYTE + 1;
if (NUMBYTE > 8) {
NUMBYTE = 1;
}
digitalWrite(10, LOW); // Pin 10 is Low
digitalWrite(9, HIGH); // Pin 9 is High for Motor A
digitalWrite(6, HIGH); // Pin 6 is High for Motor B
digitalWrite(7, LOW); // Pin 7 is Low
analogWrite(11, 255 * 0.8);
analogWrite(5, 255 * 0.8);
} else
{
Stop();
printByte(halt);
delay(1000);
//GO REAR
printByte(arrow_up);
digitalWrite(10, HIGH); // Pin 10 is High for Motor A
digitalWrite(9, LOW); // Pin 9 is Low
digitalWrite(6, LOW); // Pin 6 is Low
digitalWrite(7, HIGH); // Pin 7 is High for Motor B
delay(1000);
Stop();
delay(1000);
//TURN LEFT
printByte(arrow_left);
digitalWrite(10, HIGH); // Pin 10 is High for Motor A
digitalWrite(9, LOW); // Pin 9 is Low
digitalWrite(6, HIGH); // Pin 6 is High for Motor B
digitalWrite(7, LOW); // Pin 7 is Low
randNumberdelay = random(2000, 3000);
delay(randNumberdelay);
Stop();
}
//************************END AUTO MODE************************
}
else {
//Serial.println("Bluetooth");
//Stop();
//************************START BLUETOOTH MANUAL MODE************************
//USE BLUETOOTH RC CONTROL APP TO CONNECT
if (Serial1.available() > 0) // Check if Data is available
{
int data = Serial1.read(); // Read the Data
Stop();
if (data == 'L') // For Left Movement
{
//Serial.println("BLUETOOTH LEFT! ");
analogWrite(11, 250);
analogWrite(5, 250);
digitalWrite(10, HIGH); // Pin 10 is High for Motor A
digitalWrite(9, LOW); // Pin 9 is Low
digitalWrite(6, HIGH); // Pin 6 is High for Motor B
digitalWrite(7, LOW); // Pin 7 is Low
printByte(arrow_left);
}
else if (data == 'R') // For Right Movement
{
//Serial.println("BLUETOOTH RIGHT! ");
analogWrite(11, 250);
analogWrite(5, 250);
digitalWrite(10, LOW); // Pin 10 is Low
digitalWrite(9, HIGH); // Pin 9 is High for Motor A
digitalWrite(6, LOW); // Pin 6 is Low
digitalWrite(7, HIGH); // Pin 7 is High for Motor B
printByte(arrow_right);
}
else if (data == 'F') // For Forward Movement
{
//Serial.println("BLUETOOTH FORWARD! ");
analogWrite(11, 250);
analogWrite(5, 250);
digitalWrite(10, LOW); // Pin 10 is Low
digitalWrite(9, HIGH); // Pin 9 is High for Motor A
digitalWrite(6, HIGH); // Pin 6 is High for Motor B
digitalWrite(7, LOW); // Pin 7 is Low
//GO FORWARD
//Serial.print("FORWARD! ");
switch (NUMBYTE) {
case 1:
printByte(b1);
break;
case 2:
printByte(b2);
break;
case 3:
printByte(b3);
break;
case 4:
printByte(b4);
break;
case 5:
printByte(b5);
break;
case 6:
printByte(b6);
break;
case 7:
printByte(b7);
break;
case 8:
printByte(b8);
break;
default:
printByte(b1);
break;
}
//delay(50);
NUMBYTE = NUMBYTE + 1;
if (NUMBYTE > 8) {
NUMBYTE = 1;
}
}
else if (data == 'B') // For Backward Movement
{
//Serial.println("BACKWARD ! ");
analogWrite(11, 250);
analogWrite(5, 250);
digitalWrite(10, HIGH); // Pin 10 is High for Motor A
digitalWrite(9, LOW); // Pin 9 is Low
digitalWrite(6, LOW); // Pin 6 is Low
digitalWrite(7, HIGH); // Pin 7 is High for Motor B
switch (NUMBYTE) {
case 1:
printByte(b1);
break;
case 2:
printByte(b2);
break;
case 3:
printByte(b3);
break;
case 4:
printByte(b4);
break;
case 5:
printByte(b5);
break;
case 6:
printByte(b6);
break;
case 7:
printByte(b7);
break;
case 8:
printByte(b8);
break;
default:
printByte(b1);
break;
}
//delay(50);
NUMBYTE = NUMBYTE - 1;
if (NUMBYTE < 1) {
NUMBYTE = 9;
}
}
else if (data == 'G') // Forward left
{
//Serial.println("BLUETOOTH FORWARD LEFT! ");
analogWrite(11, 50);
analogWrite(5, 255);
digitalWrite(10, LOW); // Pin 10 is Low
digitalWrite(9, HIGH); // Pin 9 is High for Motor A
digitalWrite(6, HIGH); // Pin 6 is High for Motor B
digitalWrite(7, LOW); // Pin 7 is Low
printByte(arrow_up_left);
}
else if (data == 'I') // Forward right
{
analogWrite(11, 255);
analogWrite(5, 50);
digitalWrite(10, LOW); // Pin 10 is Low
digitalWrite(9, HIGH); // Pin 9 is High for Motor A
digitalWrite(6, HIGH); // Pin 6 is High for Motor B
digitalWrite(7, LOW); // Pin 7 is Low
printByte(arrow_up_right);
}
else if (data == 'J') // Back right
{
analogWrite(11, 255);
analogWrite(5, 50);
digitalWrite(10, HIGH); // Pin 10 is High for Motor A
digitalWrite(9, LOW); // Pin 9 is Low
digitalWrite(6, LOW); // Pin 6 is Low
digitalWrite(7, HIGH); // Pin 7 is High for Motor B
printByte(arrow_down_right);
}
else if (data == 'H') // Back left
{
analogWrite(11, 50);
analogWrite(5, 255);
digitalWrite(10, HIGH); // Pin 10 is High for Motor A
digitalWrite(9, LOW); // Pin 9 is Low
digitalWrite(6, LOW); // Pin 6 is Low
digitalWrite(7, HIGH); // Pin 7 is High for Motor B
printByte(arrow_down_left);
}
else if (data == 'q') // For 100% speed
{
analogWrite(11, 255);
analogWrite(5, 255);
}
else if (data == '9') // For 90% speed
{
analogWrite(11, 255 * 0.9);
analogWrite(5, 255 * 0.9);
}
else if (data == '8') // For 80% speed
{
analogWrite(11, 255 * 0.8);
analogWrite(5, 255 * 0.8);
}
else if (data == '7') // For 70% speed
{
analogWrite(11, 255 * 0.7);
analogWrite(5, 255 * 0.7);
}
else if (data == '6') // For 60% speed
{
analogWrite(11, 255 * 0.6);
analogWrite(5, 255 * 0.6);
}
else if (data == '5') // For 50% speed
{
analogWrite(11, 255 * 0.5);
analogWrite(5, 255 * 0.5);
}
else if (data == '4') // For 40% speed
{
analogWrite(11, 255 * 0.4);
analogWrite(5, 255 * 0.4);
}
else if (data == '3') // For 30% speed
{
analogWrite(11, 255 * 0.3);
analogWrite(5, 255 * 0.3);
}
else if (data == '2') // For 20% speed
{
analogWrite(11, 255 * 0.2);
analogWrite(5, 255 * 0.2);
}
else if (data == '1') // For 10% speed
{
analogWrite(11, 255 * 0.1);
analogWrite(5, 255 * 0.1);
}
else if (data == '0') // For 0% speed
{
Stop();
}
else if (data == 'X') // Activate relay
{
digitalWrite(pin_BLADES, HIGH);
}
else if (data == 'x') // deactivate relay
{
digitalWrite(pin_BLADES, LOW);
}
else if (data == 'V') // Activate Buzzer
{
digitalWrite(buzzerPin, HIGH);
}
else if (data == 'v') // deactivate Buzzer
{
digitalWrite(buzzerPin, LOW);
}
}
first_run = true;
}
//************************END BLUETOOTH MANUAL MODE************************
}
void Stop() // Function to Stop the Motor
{
digitalWrite(10, LOW); // Pin 10 is Low
digitalWrite(9, LOW); // Pin 9 is Low
digitalWrite(6, LOW); // Pin 6 is Low
digitalWrite(7, LOW); // Pin 7 is Low
}
//PRINT ON MATRIX DISPLAY 8x8
void printByte(byte character [])
{
int i = 0;
for (i = 0; i < 8; i++)
{
lc.setRow(0, i, character[i]);
}
}
Comments