Created
May 13, 2018 12:47
-
-
Save alexcode/63570dd5aaa0ef226b347e1f41cfb5bc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Ultrasonic.h> | |
#include <Servo.h> | |
#define DISTANCE_PIN 6 | |
#define SERVO_PIN 7 | |
#define SERVO2_PIN 10 | |
#define PIN_MOVE 11 | |
Ultrasonic ultrasonic(DISTANCE_PIN); | |
Servo focusServo; // create servo object to control a servo | |
Servo flapServo; | |
int focusPosition = 0; // variable to store the servo position | |
int flapPosition = 0; | |
const unsigned long MOVIE_TIME_SEC = 10000; | |
int delayLength = 30; | |
int stepnumber1 = 0; | |
bool stepperReset = false; | |
bool capteurReset = false; | |
int currentVideo = 0; | |
void setup() { | |
Serial.begin(9600); | |
focusServo.attach(SERVO_PIN); | |
flapServo.attach(SERVO2_PIN); | |
pinMode(PIN_MOVE, INPUT); | |
pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards??? | |
pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards??? | |
//establish motor brake pins | |
pinMode(9, OUTPUT); //brake (disable) CH A | |
pinMode(8, OUTPUT); //brake (disable) CH B | |
} | |
void loop() { | |
/* | |
if(!stepperReset) { | |
// stepper aller a zero | |
while(!capteurReset) { | |
for (int steps = 0; capteurReset == true || steps < 25; steps +=1 ) { | |
stepperRight(); | |
} | |
for (int steps = 0; capteurReset == true || steps < 50; steps +=1 ) { | |
stepperLeft(); | |
} | |
} | |
stepperReset = true; | |
} | |
*/ | |
if(currentVideo == 0 /*&& digitalRead(PIN_MOVE) == HIGH*/) { | |
Serial.println("//lecture capteur mouvement//"); | |
currentVideo = 1; | |
Serial.println("fin etape 1"); | |
} | |
if(currentVideo == 1) { | |
Serial.println("Etape 2 debut"); | |
playVideo(2); | |
Serial.println("Etape 2 fin"); | |
} | |
if(currentVideo == 2) { | |
Serial.println("E3 debut"); | |
Serial.println("moteur pas a pas bouge de 90 degrés"); | |
while(stepnumber1 < 13) { | |
stepperRight(); | |
stepnumber1 +=1; | |
} | |
Serial.println("joue la deuxieme video"); | |
playVideo(0); | |
Serial.println("remet step reset en false pour remettre m.p.a.p en position 0"); | |
stepperReset = false; | |
Serial.println("E3 fin"); | |
} | |
delay(10); | |
} | |
void playVideo(int next) { | |
Serial.println("attente du clap"); | |
int clap = analogRead(A5); | |
if(clap >= 100) { //si le niveau sonore est élevé | |
Serial.println(clap); | |
Serial.println("fait la mise au point"); | |
int angle = static_cast<int>(getServoAngle(ultrasonic.distanceRead())); | |
rotateServo(focusPosition, angle, focusServo); | |
Serial.println("ouvre le cache"); | |
openFlap(); | |
Serial.println("joue la video"); | |
delay(MOVIE_TIME_SEC); | |
Serial.println("ferme le clap"); | |
closeFlap(); | |
currentVideo = next; | |
Serial.println("passe a l'etape suivante"); | |
} | |
} | |
float getAngle(float distance) { | |
return 13.74312 + (51.59477 - 13.74312)/(1 + pow(distance/142.6213, 4.404875)); | |
} | |
float getServoAngle(float distance) { | |
return getAngle(distance) * 1.8; | |
} | |
void rotateServo(int servoPosition, int angle, Servo myservo) { | |
while(servoPosition != angle) { | |
if(servoPosition < angle) { | |
servoPosition += 1; | |
} else { | |
servoPosition -= 1; | |
} | |
myservo.write(servoPosition); | |
delay(15); | |
} | |
} | |
void closeFlap() { | |
rotateServo(flapPosition, 0, flapServo); | |
} | |
void openFlap() { | |
rotateServo(flapPosition, 90, flapServo); | |
} | |
void stepperRight() { | |
digitalWrite(9, HIGH); //DISABLE CH A | |
digitalWrite(8, LOW); //ENABLE CH B | |
digitalWrite(13, LOW); //Sets direction of CH B | |
analogWrite(11, 255); //Moves CH B | |
delay(delayLength); | |
digitalWrite(9, LOW); //ENABLE CH A | |
digitalWrite(8, HIGH); //DISABLE CH B | |
digitalWrite(12, HIGH); //Sets direction of CH A | |
analogWrite(3, 255); //Moves CH A | |
delay(delayLength); | |
digitalWrite(9, HIGH); //DISABLE CH A | |
digitalWrite(8, LOW); //ENABLE CH B | |
digitalWrite(13, HIGH); //Sets direction of CH B | |
analogWrite(11, 255); //Moves CH B | |
delay(delayLength); | |
digitalWrite(9, LOW); //ENABLE CH A | |
digitalWrite(8, HIGH); //DISABLE CH B | |
digitalWrite(12, LOW); //Sets direction of CH A | |
analogWrite(3, 255); //Moves CH A | |
delay(delayLength); | |
} | |
void stepperLeft() { | |
digitalWrite(9, LOW); //ENABLE CH A | |
digitalWrite(8, HIGH); //DISABLE CH B | |
digitalWrite(12, HIGH); //Sets direction of CH A | |
analogWrite(3, 255); //Moves CH A | |
delay(delayLength); | |
digitalWrite(9, HIGH); //DISABLE CH A | |
digitalWrite(8, LOW); //ENABLE CH B | |
digitalWrite(13, LOW); //Sets direction of CH B | |
analogWrite(11, 255); //Moves CH B | |
delay(delayLength); | |
digitalWrite(9, LOW); //ENABLE CH A | |
digitalWrite(8, HIGH); //DISABLE CH B | |
digitalWrite(12, LOW); //Sets direction of CH A | |
analogWrite(3, 255); //Moves CH A | |
delay(delayLength); | |
digitalWrite(9, HIGH); //DISABLE CH A | |
digitalWrite(8, LOW); //ENABLE CH B | |
digitalWrite(13, HIGH); //Sets direction of CH B | |
analogWrite(11, 255); //Moves CH B | |
delay(delayLength); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment