Convoyeur_Robot_One/src/main.cpp

102 lines
3.4 KiB
C++

#include <Arduino.h>
#include <Wire.h>
#include <SparkFun_APDS9960.h> //https://cdn.sparkfun.com/assets/learn_tutorials/3/2/1/Avago-APDS-9960-datasheet.pdf
SparkFun_APDS9960 apds = SparkFun_APDS9960();
uint8_t proximity_data = 0;
//The ENA, IN1 and IN2 terminals are connected to ports 10, 9 and 8.
#define borneENA 10
#define borneIN1 9
#define borneIN2 8
#define delaiChangementVitesse 20 // This is the time it takes the motor to increase or decrease its speed by 1.
#define vitesseMinimale 0 // Is the minimum speed of the engine
#define vitesseMaximale 63 // Is the maximum speed of the motor. It can be set up to 255 which corresponds to 100% of its capacity.
const char MARCHE_AVANT = 'V';
const char MARCHE_ARRIERE = 'R';
int vitesse;
void setup() {
pinMode(borneENA, OUTPUT);
pinMode(borneIN1, OUTPUT);
pinMode(borneIN2, OUTPUT);
Serial.begin(9600); // We initialize the connection between the Arduino and the IDE in order to transmit the program.
if ( apds.init() ) { //Configures I2C communications and initializes registers to default.
Serial.println(F("APDS-9960 initialization complete"));
} else {
Serial.println(F("Something went wrong during APDS-9960 init!"));
}
if ( apds.enableProximitySensor(false) ) { // Starts the proximity sensor on a APDS-9960
Serial.println(F("Proximity sensor is now running"));
} else {
Serial.println(F("Something went wrong during sensor init!"));
}
if ( !apds.setProximityGain(PGAIN_2X) ) { // Set the receiver gain for proximity detection Value Gain 2x
Serial.println(F("Something went wrong trying to set PGAIN"));
}
}
void configurerSensDeRotationPontA(char sensDeRotation) { // Adds a new function for the direction of rotation.
if(sensDeRotation == MARCHE_AVANT) {
digitalWrite(borneIN1, HIGH);
digitalWrite(borneIN2, LOW);
}
if(sensDeRotation == MARCHE_ARRIERE) {
digitalWrite(borneIN1, LOW);
digitalWrite(borneIN2, HIGH);
}
}
void changeVitesseMoteurPontA(int nouvelleVitesse) { // Adds a new function for the speed.
analogWrite(borneENA, nouvelleVitesse); //indicates the new speed at the ENA terminal.
}
void loop() {
configurerSensDeRotationPontA(MARCHE_AVANT); // Instructs the motor to move in the forward direction.
if ( !apds.readProximity(proximity_data) ) { //Allows you to retrieve data from the sensor.
Serial.println("Error reading proximity value");
} else {
Serial.print("Proximity: ");
Serial.println(proximity_data); //
}
while (proximity_data <= 15 && vitesse<=vitesseMaximale ) // Loop that allows the conveyor to remain running as long as no object has passed the sensor.
{
if ( !apds.readProximity(proximity_data) ) {
Serial.println("Error reading proximity value");
} else {
Serial.print("Proximity: ");
Serial.println(proximity_data);
}
Serial.println(vitesse);
changeVitesseMoteurPontA(vitesse);
delay(delaiChangementVitesse);
if (vitesse < vitesseMaximale ){
vitesse++; // increases motor speed by 1.
}
Serial.println(proximity_data);
}
for(; vitesse>vitesseMinimale ; vitesse--) { //decreases the speed until it stops.
changeVitesseMoteurPontA(vitesse);
delay(delaiChangementVitesse);
}
delay(2000); //stays off for 2 seconds.
}