int pwm_a = 9;
int pwm_b = 10;
int dir_a = 7;
int dir_b = 8;
#define BUTTON 4
#define BUT 3
int val = 0;
int vall =0;
#define trigPin 6
#define echoPin 5
void setup()
{
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(pwm_a, OUTPUT);
pinMode(pwm_b, OUTPUT);
pinMode(dir_a, OUTPUT);
pinMode(dir_b, OUTPUT);
pinMode(BUTTON,INPUT);// bouton gauche
pinMode(BUT,INPUT);
analogWrite(pwm_a,127);
analogWrite(pwm_b,127);
}
void loop()
{
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000);
delayMicroseconds(10); //
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
val = digitalRead(BUTTON);
vall = digitalRead(BUT);
if (val == HIGH){
digitalWrite(dir_a,LOW);
digitalWrite(dir_b,LOW);
analogWrite(pwm_a,127);
analogWrite(pwm_b,127);
}
else{
digitalWrite(dir_b,HIGH);
digitalWrite(dir_a,HIGH);
delay(250);
digitalWrite(dir_b,LOW);
digitalWrite(dir_a, HIGH);
delay(500);
}
if (vall == HIGH){
digitalWrite(dir_a,LOW);
digitalWrite(dir_b,LOW);
analogWrite(pwm_a,127);
analogWrite(pwm_b,127);
}
else{
digitalWrite(dir_b,HIGH);
digitalWrite(dir_a,HIGH);
delay(250);
digitalWrite(dir_b,HIGH);
digitalWrite(dir_a,LOW);
delay(500);
}
if (distance < 8) {
digitalWrite(dir_b,HIGH);
digitalWrite(dir_a,HIGH);
delay(500);
digitalWrite(dir_b,HIGH);
digitalWrite(dir_a,LOW);
delay(2000);
}
else {
digitalWrite(dir_a,LOW);
digitalWrite(dir_b,LOW);
analogWrite(pwm_a,127);
analogWrite(pwm_b,127);
}
}
22 octobre 2013
capteur de contact et moteur cc robot zumo pololu
// contrôle de 2 moteurs cc avec 2 capteurs de contact avec chassis pololu zumo
int pwm_a = 9; // Entrée PWMA du zumo shield arduino sur broche 9
int pwm_b = 10; // Entrée PWMB du zumo shield arduino sur broche10
int dir_a = 7; // Entrée DIRA " " " sur broche 7
int dir_b = 8; // Entrée DIRB " " " sur broche 8
#define BOUTTON_A 4 // capteur de contact A sur broche 4
#define BOUTTON_B 3 // capteur de contact B sur broche 3
int val = 0; // les données val et vall sont égale à 0
int vall =0;
void setup()
{
pinMode(pwm_a, OUTPUT); // met la broche 9 en sortie
pinMode(pwm_b, OUTPUT); //met la broche 10 en sortie
pinMode(dir_a, OUTPUT);
pinMode(dir_b, OUTPUT);
pinMode(BOUTTON_A,INPUT);// bouton_A gauche en entrée
pinMode(BOUTTON_B,INPUT);
analogWrite(pwm_a,127); // moteurs à 50% de la vitesse maxi (valeur max 255)
analogWrite(pwm_b,127);
}
void loop()
{
val = digitalRead(BOUTTON_A); // les capteurs de contact
vall = digitalRead(BOUTTON_B);
if (val == HIGH){ // si boutton_A égal high (pas de contact)
digitalWrite(dir_a,LOW); // moteurs tournent dans un sens
digitalWrite(dir_b,LOW);
analogWrite(pwm_a,127); // à 50% de la vitesse max
analogWrite(pwm_b,127);
}
else{ // sinon ,
digitalWrite(dir_b,HIGH); //changer de sens moteur
digitalWrite(dir_a,HIGH);
delay(250); // attendre 2,5 sec
digitalWrite(dir_b,LOW); // permet de tourner selon le contact
digitalWrite(dir_a, HIGH);
delay(250);
}
if (vall == HIGH){
digitalWrite(dir_a,LOW);
digitalWrite(dir_b,LOW);
analogWrite(pwm_a,127);
analogWrite(pwm_b,127);
}
else{
digitalWrite(dir_b,HIGH);
digitalWrite(dir_a,HIGH);
delay(250);
digitalWrite(dir_b,HIGH);
digitalWrite(dir_a,LOW);
delay(250);
}
}
17 octobre 2013
Capteur US(ultrason) HC-SR04
#define trigPin 10
#define echoPin 12
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
}
#define echoPin 12
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
}
Inscription à :
Articles (Atom)