Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- #define DIR_PIN 2
- #define STEP_PIN 3
- const int button = 48; // un bouton sur la broche 48
- const int led = 46;
- Servo monServomoteur;
- float degTot=0;
- void setup() {
- monServomoteur.attach(8);
- pinMode(button, INPUT_PULLUP);
- pinMode(led, OUTPUT);
- pinMode(DIR_PIN, OUTPUT);
- pinMode(STEP_PIN, OUTPUT);
- digitalWrite(led, HIGH);
- while(digitalRead(button) == LOW){
- rotateDeg(-1, .2);
- delay(50);
- }
- degTot = 0;
- }
- void rotate(int steps, float speed){
- int dir = (steps > 0)? HIGH:LOW;
- steps = abs(steps);
- digitalWrite(DIR_PIN,dir);
- float usDelay = (1/speed) * 70;
- for(int i=0; i < steps; i++){
- degTot++;
- if(degTot == 3200){
- degTot = 0;
- delay(500);
- }
- digitalWrite(STEP_PIN, HIGH);
- delayMicroseconds(usDelay);
- digitalWrite(STEP_PIN, LOW);
- delayMicroseconds(usDelay);
- }
- }
- void rotateDeg(float deg, float speed){
- int dir = (deg > 0)? HIGH:LOW;
- digitalWrite(DIR_PIN,dir);
- int steps = abs(deg)*(2/0.225);
- float usDelay = (1/speed) * 70;
- for(int i=0; i < steps; i++){
- degTot++;
- if(degTot == 3200){
- degTot = 0;
- rotate(-3200, speed);
- digitalWrite(DIR_PIN,dir);
- delay(500);
- }
- digitalWrite(STEP_PIN, HIGH);
- delayMicroseconds(usDelay);
- digitalWrite(STEP_PIN, LOW);
- delayMicroseconds(usDelay);
- }
- }
- int position;
- void rotateServo(float angleBase, float angleFin){
- if(angleBase <= angleFin){
- for (position = angleBase; position <= angleFin; position++){
- monServomoteur.write(position);
- }
- }
- if(angleBase > angleFin){
- for (position = angleBase; position <= angleFin; position--){
- monServomoteur.write(position);
- }
- }
- }
- void loop(){
- // Fait bouger le bras de 0° à 180°
- //rotateServo(0,180);
- //for(int position = 0; position < 180; position++){
- // monServomoteur.write(position);
- // }
- if(digitalRead(button) == HIGH) // test si le bouton a un niveau logique HAUT
- {
- digitalWrite(led, LOW); //le bouton est relâché, la LED est allumée
- }
- else // test si le bouton a un niveau logique différent de HAUT (donc BAS)
- {
- digitalWrite(led, HIGH); //la LED reste éteinte
- }
- rotateDeg(40, .2);
- delay(500);
- rotateDeg(-30, .2);
- delay(500);
- degTot=0;
- //rotateDeg(400, .2);
- //delay(1000);
- //rotateDeg(-180, .2);
- //delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement