Fablab ado > mbot et suivi de ligne
Dans l’IDE Arduino,
voici le code pour un suivi de ligne blanche sur un fond noir :
// suivi de ligne
// stephane beurret
#include "MeMCore.h"
#define ledPin 13
#define buttonPin 7
#define leftLed 1
#define rightLed 0
#define NONE 0
#define GAUCHE 1
#define DROITE 2
#define AVANT 3
MeRGBLed led(0, 30);
MeLineFollower lineFinder(PORT_2); /* Line Finder module can only be connected to PORT_2, PORT_4, PORT_5, PORT_6 of base shield. */
MeDCMotor motG(M1); //Motor1 is Left Motor
MeDCMotor motD(M2); //Motor2 is Right Motor
MeBuzzer buzzer;
MeLightSensor lightSensor(PORT_8);
MeUltrasonicSensor ultrasonic(PORT_3);
int oldMove=NONE;
int speedMot=100;
void Move(int leftSpeed, int rightSpeed) // entre -255 et +255
{
motG.run(-leftSpeed); //Motor1 (Left) forward is -negative
motD.run(rightSpeed); //Motor2 (Right) forward is +
}
void Stop()
{
Move(0,0);
}
void setup()
{
int sensorState = lineFinder.readSensors();
switch(sensorState)
{
case S1_IN_S2_IN: Serial.println("S1 IN - S2 IN"); break;
case S1_IN_S2_OUT: Serial.println("S1 IN - S2 OUT"); break;
case S1_OUT_S2_IN: Serial.println("S1 OUT - S2 IN"); break;
case S1_OUT_S2_OUT: Serial.println("S1 OUT - S2 OUT"); break;
default: break;
}
delay(200);
}
void loop()
{
int sensorState = lineFinder.readSensors();
switch(sensorState)
{
case S1_OUT_S2_OUT:
//Serial.println("S1 IN - S2 IN");
Move(speedMot, speedMot);
//oldMove=AVANT;
break;
case S1_OUT_S2_IN:
//Serial.println("S1 IN - S2 OUT");
Move(0, speedMot);
oldMove=GAUCHE;
break;
case S1_IN_S2_OUT:
//Serial.println("S1 OUT - S2 IN");
Move(speedMot, 0);
oldMove=DROITE;
break;
case S1_IN_S2_IN:
//Serial.println("S1 OUT - S2 OUT");
if (oldMove==GAUCHE)
Move(0, speedMot);
else if (oldMove==DROITE)
Move(speedMot,0);
else Move (0,0);
break;
default: break;
}
}
Partager cette page