Niveau APPRENTISSAGE : ► Fort
Prérequis :
Version IDE :
Bibliothèque :
Vidéo de démonstration :
Schéma de câblage :

Code :
#include "Wire.h"
#include "Adafruit_PWMServoDriver.h"
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x40);
#define MIN_PULSE_WIDTH 600
#define MAX_PULSE_WIDTH 2600
#define FREQUENCY 50
int BtBlanc = 2;
int BtJaune = 3;
int BtRouge = 4;
int BtVert = 5;
bool EtatBtBlanc;
bool EtatBtJaune;
bool EtatBtRouge;
bool EtatBtVert;
void setup() {
pwm.begin();
pwm.setPWMFreq(FREQUENCY);
pinMode(BtBlanc, INPUT_PULLUP);
pinMode(BtJaune, INPUT_PULLUP);
pinMode(BtRouge, INPUT_PULLUP);
pinMode(BtVert, INPUT_PULLUP);
}
int pulseWidth(int angle) {
int pulse_wide, analog_value;
pulse_wide = map(angle, 0, 180, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
analog_value = int(float(pulse_wide) / 1000000 * FREQUENCY * 4096);
return analog_value;
}
void loop() {
EtatBtBlanc = digitalRead(BtBlanc);
if (EtatBtBlanc == HIGH) {
pwm.setPWM(0, 0, pulseWidth(90));
}
else {
pwm.setPWM(0, 0, pulseWidth(0));
}
EtatBtJaune = digitalRead(BtJaune);
if (EtatBtJaune == HIGH) {
pwm.setPWM(1, 0, pulseWidth(90));
}
else {
pwm.setPWM(1, 0, pulseWidth(0));
}
EtatBtRouge = digitalRead(BtRouge);
if (EtatBtRouge == HIGH) {
pwm.setPWM(2, 0, pulseWidth(90));
}
else {
pwm.setPWM(2, 0, pulseWidth(0));
}
EtatBtVert = digitalRead(BtVert);
if (EtatBtVert == HIGH) {
pwm.setPWM(3, 0, pulseWidth(90));
}
else {
pwm.setPWM(3, 0, pulseWidth(0));
}
}
+ Infos sur le langage utilisé :
1. Fonctions
2. Variables
3. Stucture
Nombre de vues: 588
WordPress:
J’aime chargement…