Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Grove_I2C_Motor_Driver.h"
- #define I2C_ADDRESS 0x0f
- int capteur1 = 3; // connected to digital pin
- int capteur2 = 6; // connected to digital pin
- int capteur3 = 5; // connected to digital pin
- int capteur4 = 7; // connected to digital pin
- void setup() {
- Motor.begin(I2C_ADDRESS);
- pinMode(capteur1, INPUT); // initialize the digital pin as an output:
- pinMode(capteur2, INPUT); // initialize the digital pin as an output:
- pinMode(capteur3, INPUT); // initialize the digital pin as an output:
- pinMode(capteur4, INPUT); // initialize the digital pin as an output:
- Serial.begin(9600); // initialize serial communications at 9600 bps:
- }
- void loop() {
- if (LOW == digitalRead(capteur2) && LOW == digitalRead(capteur3)) {
- Motor.speed(MOTOR1, 40);
- Motor.speed(MOTOR2, 40);
- }
- if (HIGH == digitalRead(capteur2)) {
- Motor.speed(MOTOR2, -40);
- Motor.speed(MOTOR1, 70);
- }
- if (HIGH == digitalRead(capteur3)) {
- Motor.speed(MOTOR2, 70);
- Motor.speed(MOTOR1, -40);
- }
- if (HIGH == digitalRead(capteur3) && HIGH == digitalRead(capteur2)) {
- Motor.speed(MOTOR2, 30);
- Motor.speed(MOTOR1, 30);
- }
- if (HIGH == digitalRead(capteur4)) {
- while (HIGH != digitalRead(capteur3)) {
- Motor.speed(MOTOR1, -100);
- Motor.speed(MOTOR2, 100);
- }
- }
- if (HIGH == digitalRead(capteur1)) {
- while (HIGH != digitalRead(capteur2)) {
- Motor.speed(MOTOR1, 100);
- Motor.speed(MOTOR2, -100);
- }
- }
- if (HIGH == digitalRead(capteur1) && HIGH == digitalRead(capteur4)) {
- int alea;
- alea = rand() % 2;
- if ( alea == 0) {
- while (HIGH != digitalRead(capteur3)) {
- Motor.speed(MOTOR1, 100);
- Motor.speed(MOTOR2, -100);
- }
- }
- if ( alea == 1) {
- while (HIGH != digitalRead(capteur2)) {
- Motor.speed(MOTOR1, 100);
- Motor.speed(MOTOR2, -100);
- }
- }
- }
- }
- 11:18
- #include "Grove_I2C_Motor_Driver.h"
- #define I2C_ADDRESS 0x0f
- #define COMPTE_TOUR 2
- int tour = 0;
- int capteur1 = 3; // connected to digital pin
- int capteur2 = 6; // connected to digital pin
- int capteur3 = 5; // connected to digital pin
- int capteur4 = 7; // connected to digital pin
- struct path {
- int x;
- int y;
- };
- path voiture = {4, 0}; //coordonnees voiture
- path point = {4, 5}; //coordonnees point d'arrivée
- path Map = {5, 8};
- int direct = 1; //direction de la voiture 1=x+ 2=x- 3=y+ 4=y-
- void setup() {
- Motor.begin(I2C_ADDRESS);
- pinMode(capteur1, INPUT); // initialize the digital pin as an output:
- pinMode(capteur2, INPUT); // initialize the digital pin as an output:
- pinMode(capteur3, INPUT); // initialize the digital pin as an output:
- pinMode(capteur4, INPUT); // initialize the digital pin as an output:
- attachInterrupt(digitalPinToInterrupt(COMPTE_TOUR), compte_les_inters, RISING);
- Serial.begin(9600); // initialize serial communications at 9600 bps:
- }
- void compte_les_inters() {
- tour++;
- }
- void loop() {
- if (LOW == digitalRead(capteur2) && LOW == digitalRead(capteur3)) {
- tour = 0;
- if (direct == 1) {
- Motor.speed(MOTOR1, 100);
- Motor.speed(MOTOR2, 100);
- voiture.x = voiture.x + (tour / 120);
- }
- else if (direct == 2) {
- Motor.speed(MOTOR1, 100);
- Motor.speed(MOTOR2, 100);
- voiture.x = voiture.x - (tour / 120);
- }
- else if (direct == 3) {
- Motor.speed(MOTOR1, 100);
- Motor.speed(MOTOR2, 100);
- voiture.y = voiture.y + (tour / 120);
- }
- else if (direct == 4) {
- Motor.speed(MOTOR1, 100);
- Motor.speed(MOTOR2, 100);
- voiture.y = voiture.y - (tour / 120);
- }
- }
- if (HIGH == digitalRead(capteur2)) {
- Motor.speed(MOTOR2, -40);
- Motor.speed(MOTOR1, 70);
- }
- if (HIGH == digitalRead(capteur3)) {
- Motor.speed(MOTOR2, 70);
- Motor.speed(MOTOR1, -40);
- }
- if (HIGH == digitalRead(capteur3) && HIGH == digitalRead(capteur2)) {
- Motor.speed(MOTOR2, 25);
- Motor.speed(MOTOR1, 25);
- }
- if (HIGH == digitalRead(capteur4)) {
- tour = 0;
- if (direct == 1 && voiture.y > point.y) {
- while (HIGH != digitalRead(capteur3)) {
- Motor.speed(MOTOR1, 100);
- Motor.speed(MOTOR2, -100);
- }
- if ((tour / 120) > 0.5) {
- direct = 2;
- } else {
- direct = 1;
- }
- }
- if (direct == 1 && voiture.y < point.y ) {
- while (HIGH != digitalRead(capteur3)) {
- Motor.speed(MOTOR1, -100);
- Motor.speed(MOTOR2, 100);
- direct = 4;
- }
- }
- if (direct == 1 && (voiture.x + 1) > point.x) {
- while (HIGH != digitalRead(capteur3)) {
- Motor.speed(MOTOR1, -100);
- Motor.speed(MOTOR2, 100);
- direct = 4;
- }
- }
- if (direct == 2 && voiture.y < point.y) {
- while (HIGH != digitalRead(capteur3)) {
- Motor.speed(MOTOR1, 100);
- Motor.speed(MOTOR2, -100);
- direct = 1;
- }
- if ((tour / 120) > 0.5) {
- direct = 1;
- } else {
- direct = 2;
- }
- }
- if (direct == 2 && voiture.y < point.y ) {
- while (HIGH != digitalRead(capteur3)) {
- Motor.speed(MOTOR1, -100);
- Motor.speed(MOTOR2, 100);
- direct = 3;
- }
- }
- if (direct == 2 && (voiture.x + 1) > point.x) {
- while (HIGH != digitalRead(capteur3)) {
- Motor.speed(MOTOR1, -100);
- Motor.speed(MOTOR2, 100);
- direct = 3;
- }
- }
- if (direct == 3 && voiture.x > point.x ) {
- while (HIGH != digitalRead(capteur3)) {
- Motor.speed(MOTOR1, 100);
- Motor.speed(MOTOR2, -100);
- direct = 1;
- }
- if ((tour / 120) > 0.5) {
- direct = 4;
- } else {
- direct = 3;
- }
- }
- if (direct == 3 && voiture.x < point.x ) {
- while (HIGH != digitalRead(capteur3)) {
- Motor.speed(MOTOR1, -100);
- Motor.speed(MOTOR2, 100);
- direct = 1;
- }
- }
- if (direct == 3 && (voiture.y + 1) > point.y) {
- while (HIGH != digitalRead(capteur3)) {
- Motor.speed(MOTOR1, -100);
- Motor.speed(MOTOR2, 100);
- direct = 1;
- }
- }
- if (direct == 4 && voiture.x < point.x ) {
- while (HIGH != digitalRead(capteur3)) {
- Motor.speed(MOTOR1, 100);
- Motor.speed(MOTOR2, -100);
- }
- if ((tour / 120) > 0.5) {
- direct = 3;
- } else {
- direct = 4;
- }
- }
- if (direct == 4 && voiture.x > point.x ) {
- while (HIGH != digitalRead(capteur3)) {
- Motor.speed(MOTOR1, -100);
- Motor.speed(MOTOR2, 100);
- direct = 2;
- }
- }
- if (direct == 4 && (voiture.y + 1) > point.y) {
- while (HIGH != digitalRead(capteur3)) {
- Motor.speed(MOTOR1, -100);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement