Fonction car complet (logiciel)
- Détails
- Catégorie : Kit robot arduino
- Publication : jeudi 24 septembre 2020 15:07
- Écrit par Bensky
- Affichages : 46437
Fonction car complet (partie logiciel) :
Cette fonction permet de réaliser le SW pour faire la voiture complète. Ce soft permet de réaliser toutes les fonctions en même temps.
On peut ainsi utiliser la fonction télécommande infra rouge, la fonction bluetooth, la fonction AUTO GO et la fonction Avancer, reculer et tourner à droite et à gauche.
Programme Soft Bluetooth car complet.
Soft voiture Arduino complet. (soft à compiler.
Attention bien intégrer les bibliothèques demandé et au bon endroit.
// The direction of the car's movement
// ENA ENB IN1 IN2 IN3 IN4 Description
// HIGH HIGH HIGH LOW LOW HIGH Car is runing forward
// HIGH HIGH LOW HIGH HIGH LOW Car is runing back
// HIGH HIGH LOW HIGH LOW HIGH Car is turning left
// HIGH HIGH HIGH LOW HIGH LOW Car is turning right
// HIGH HIGH LOW LOW LOW LOW Car is stoped
// HIGH HIGH HIGH HIGH HIGH HIGH Car is stoped
// LOW LOW N/A N/A N/A N/A Car is stoped
#include <IRremote.h>
#include <IRremoteInt.h>
#include <Servo.h> //servo library
Servo myservo; // create servo object to control servo
////////// IR REMOTE CODES //////////
#define F 16736925 // FORWARD
#define B 16754775 // BACK
#define L 16720605 // LEFT
#define R 16761405 // RIGHT
#define S 16712445 // STOP
#define UNKNOWN_F 5316027 // FORWARD
#define UNKNOWN_B 2747854299 // BACK
#define UNKNOWN_L 1386468383 // LEFT
#define UNKNOWN_R 553536955 // RIGHT
#define UNKNOWN_S 3622325019 // STOP
//Line Tracking IO define
#define LT_R !digitalRead(10)
#define LT_M !digitalRead(4)
#define LT_L !digitalRead(2)
#define RECV_PIN 12
int Echo = A4;
int Trig = A5;
/*define channel enable output pins*/
#define ENA 5 // Left wheel speed
#define ENB 6 // Right wheel speed
/*define logic control output pins*/
#define IN1 7 // Left wheel forward
#define IN2 8 // Left wheel reverse
#define IN3 9 // Right wheel reverse
#define IN4 11 // Right wheel forward
#define carSpeed 150 // initial speed of car >=0 to <=255
#define LED 13
int rightDistance = 0, leftDistance = 0, middleDistance = 0;
int obstacleloop=0, lineloop=0, autogoloop=0;
// infra red
IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned long val;
unsigned long preMillis;
//unsigned char carSpeed = 150;
bool state = LOW;
char getstr;
void forward(){
digitalWrite(ENA, carSpeed);
digitalWrite(ENB, carSpeed);
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);
Serial.println("Forward");
}
void back(){
digitalWrite(ENA, carSpeed);
digitalWrite(ENB, carSpeed);
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);
Serial.println("Back");
}
void left(){
digitalWrite(ENA,HIGH);
digitalWrite(ENB,HIGH);
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);
Serial.println("Left");
}
void right(){
digitalWrite(ENA,HIGH);
digitalWrite(ENB,HIGH);
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);
Serial.println("Right");
}
void stop(){
digitalWrite(ENA,LOW);
digitalWrite(ENB,LOW);
Serial.println("Stop!");
}
void stateChange(){
state = !state;
digitalWrite(LED, state);
Serial.println("Light");
}
//Ultrasonic distance measurement Sub function
int Distance_test() {
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(20);
digitalWrite(Trig, LOW);
float Fdistance = pulseIn(Echo, HIGH);
Fdistance= Fdistance / 58;
return (int)Fdistance;
}
void setup() {
myservo.attach(3); // attach servo on pin 3 to servo object
Serial.begin(9600);
pinMode(LED, OUTPUT);
pinMode(Trig, OUTPUT);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
pinMode(ENA,OUTPUT);
pinMode(ENB,OUTPUT);
pinMode(LT_R,INPUT);
pinMode(LT_M,INPUT);
pinMode(LT_L,INPUT);
stop();
irrecv.enableIRIn();
}
void obstacle()
{
myservo.write(95); //setservo position according to scaled value
delay(500);
middleDistance = Distance_test();
if(middleDistance <= 20) {
stop();
delay(500);
myservo.write(5);
delay(1000);
rightDistance = Distance_test();
delay(500);
myservo.write(180);
delay(1000);
//myservo.write(180);
//delay(1000);
leftDistance = Distance_test();
//delay(500);
//myservo.write(90);
//delay(1000);
Serial.print("right:");
Serial.println(rightDistance);
Serial.print("left:");
Serial.println(leftDistance);
if(rightDistance > leftDistance) {
right();
delay(360);
}
else if(rightDistance < leftDistance) {
left();
delay(360);
}
else if((rightDistance <= 20) || (leftDistance <= 20)) {
back();
delay(180);
}
else {
forward();
}
}
else
{
forward();
}
}
void autogo()
{
forward(); //go forward
delay(1000);//delay 1000 ms
back(); //go back
delay(500);
left(); //turning left
delay(700);
right(); //turning right
delay(500);
}
void line() {
if(LT_M){
forward();
}
else if(LT_R) {
right();
while(LT_R);
}
else if(LT_L) {
left();
while(LT_L);
}
}
void loop()
{
myservo.write(95); //setservo position according to scaled value
delay(500);
middleDistance = Distance_test();
Serial.println(middleDistance);
if(middleDistance <= 20){obstacle();forward();}
getstr = Serial.read();
switch(getstr)
{
case 'f': forward();autogoloop=0;lineloop=0; break;
case 'b': back();autogoloop=0;lineloop=0;break;
case 'l': left();autogoloop=0;lineloop=0; break;
case 'r': right();autogoloop=0;lineloop=0; break;
case 's': stop();autogoloop=0;lineloop=0; break;
case 'a': stateChange(); break;
case 'o': obstacle();autogoloop=0;lineloop=0;break;
case 'g': autogo();autogoloop=1;lineloop=0;break;
case 'n': line();lineloop=1;autogoloop=0;break;
default: break;
}
if (autogoloop==1)
{
forward(); //go forward
delay(1000);//delay 1000 ms
back(); //go back
delay(500);
left(); //turning left
delay(700);
right(); //turning right
delay(500);
forward ();
delay(1000);
}
if (lineloop==1)
{
{
if(LT_M){
forward();
}
else if(LT_R) {
right();
while(LT_R);
}
else if(LT_L) {
left();
while(LT_L);
}
}
}
if (irrecv.decode(&results)){
preMillis = millis();
val = results.value;
Serial.println(val);
irrecv.resume();
switch(val){
case F:
case UNKNOWN_F: forward(); break;
case B:
case UNKNOWN_B: back(); break;
case L:
case UNKNOWN_L: left(); break;
case R:
case UNKNOWN_R: right();break;
case S:
case UNKNOWN_S: stop(); break;
default: break;
}
}
/*else{
if(millis() - preMillis > 500){
stop();
preMillis = millis();
}
}*/
}