#include #include int acquireTime (int); //return the proper value put on the potentiometer by the user to update the time void lightUpLeds(int); //contains the conversion digital to binary and lights up the led void makeDigit(int); //contains movements for writing numbers (sequences of up, down, right and left) void up(void); //up, down, right and left send to makeStep the proper motor to use and the proper direction void down(void); void right(void); void left(void); void makeStep(int, int, int); //contains the instructions (library stepper) to make a step void solenoid(int); //turn on or turn off the solenoid pin const int ledPin0=3; // select the pins for the LEDs const int ledPin1=4; const int ledPin2=5; const int ledPin3=6; const int potentiometer = A0; // select the input pin for the potentiometer const int buttonTime = 2; // the number of the pushbutton pin (ok for time update) const int multiplexer=12; // the number of the multiplexer pin const int sole=7; // the number of the solenoid pin int sensorValue; //used to stock the value of the potentimeter int readingValue; //contains the convertion of the sensorValue int lowMinute=0; int highMinute=0; int lowHour=0; int highHour=0; int Hour; //used for the Time library int Minute; int Min; //used to know when to read the time boolean nextStep=false; //used to validate the value of the time //Initialization for the stepper motor// const int steps = 100; // number of step per instruction (200=360degres) const int speedOfStep=30; // initialize the stepper library on pins 8 through 11: Stepper myStepper(steps, 8,10,9,11); void setup() { pinMode(ledPin0, OUTPUT); // initialize the LEDs pin as outputs pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(ledPin3, OUTPUT); pinMode(buttonTime, INPUT); //initializes the buttonTime as an input pinMode(multiplexer, OUTPUT); //initializes the multiplexer pin as on output pinMode(sole, OUTPUT); //initializes the solenoid pin as on output myStepper.setSpeed(speedOfStep); // set the speed at speedOfStep rpm } void loop(){ lowMinute = acquireTime(1); //Update the lowMinute value highMinute = acquireTime(2); //Update the highMinute value lowHour = acquireTime(3); //Update the lowHour value highHour = acquireTime(4); //Update the highHour value lightUpLeds(0); //switch of all the leds Hour = highHour*10 + lowHour; Minute = highMinute*10 + lowMinute; //initializes the time into the Time library setTime(Hour,Minute,0,1,1,2000); //hour, minute, sec, day, mounth, year while(true){//infinite loop which writes time highHour=hour()/10; lowHour=hour()%10; highMinute=minute()/10; lowMinute=minute()%10; Min=minute(); //whrites highHour makeDigit(highHour); makeStep(0,1,5); //reduction factor of progress : 5 in order to make a space between two numbers //writes lowHour makeDigit(lowHour); makeStep(0,1,5); //reduction factor of progress : 5 //writes highMinute makeDigit(highMinute); makeStep(0,1,5); //reduction factor of progress : 5 //writes lowMinute makeDigit(lowMinute); makeStep(0,1,5); //reduction factor of progress : 5 while(minute() == Min) //waits one minute to write the time again delay(100); } } int acquireTime(int stage){ while (nextStep == false){ sensorValue = analogRead(potentiometer); //return value from 0 to 1023 (0 to 5V) switch (stage){ case 1 : readingValue = sensorValue/105; break; //stage 1 : update the lowMinute value. Divides the voltage in 10 possibilities (0 to 9) case 2 : readingValue = sensorValue/175; break; //stage 2 : update the highMinute. Divides the voltage in 6 possibilities (0 to 5) case 3 : readingValue = sensorValue/105; break; //stage 3 : update the lowHour. Divides the voltage in 10 possibilities (0 to 9) } if (stage==4){ //require additional verification for stage 4 : update the highHour if (lowHour<4){ readingValue = sensorValue/345; //divides the voltage in 3 possibilities (0 to 2) } else{ readingValue = sensorValue/512; //divides the voltage in 2 possibilities (0 to 1) } } lightUpLeds(readingValue); //light up the leds with the readingValue nextStep = digitalRead(buttonTime); //exit loop if buttonTime is pressed } lightUpLeds(15);//lights up the 4 leds (15 = 1 1 1 1) delay(2000); //used as an anti rebound nextStep = false; return (readingValue); } void lightUpLeds (int value){ //converts value into binary sequence int firstLed=bitRead(value, 0); int secondLed=bitRead(value, 1); int thirdLed=bitRead(value, 2); int fourthLed=bitRead(value, 3); //lights up the leds digitalWrite(ledPin0, firstLed); digitalWrite(ledPin1, secondLed); digitalWrite(ledPin2, thirdLed); digitalWrite(ledPin3, fourthLed); } void up (){ makeStep (1,1,1); delay(100); } void down (){ makeStep (1,-1,1); delay(100); } void right (){ makeStep (0,1,1); delay(100); } void left (){ makeStep (0,-1,1); delay(100); } void makeDigit(int digit){ switch (digit){ case 0 : solenoid(1); up(); up(); right(); down(); down(); left(); solenoid(0); right(); break; case 1 : right(); solenoid(1); up(); up(); solenoid(0); down(); down(); break; case 2 : solenoid(1); up(); right(); up(); left();solenoid(0); down(); down(); solenoid(1); right(); solenoid(0); break; case 3 : solenoid(1); right(); up(); up(); left();solenoid(0); down(); solenoid(1); right(); solenoid(0); down(); break; case 4 : up(); solenoid(1); up(); solenoid(0); down(); solenoid(1); right(); up(); solenoid(0); down(); solenoid(1); down(); solenoid(0); break; case 5 : solenoid(1); right(); up(); left(); up(); right(); solenoid(0); down(); down(); break; case 6 : solenoid(1); right(); up(); left(); up(); solenoid(0); down(); solenoid(1); down(); solenoid(0); right(); break; case 7 : up(); up(); solenoid(1); right(); down(); left(); solenoid(0); right(); solenoid(1); down(); solenoid(0); break; case 8 : solenoid(1); up(); up(); right(); down(); left(); solenoid(0); right(); solenoid(1); down(); left(); solenoid(0); right(); break; case 9 : up(); solenoid(1); up(); right(); down(); left(); solenoid(0); right(); solenoid(1); down(); solenoid(0); break; } } void solenoid (int state){ digitalWrite(sole, state); //Value of state : 1 for the pen on, 0 for the pen off delay(500); } void makeStep (int motorUsed, int sense, int coeffOfStep){ digitalWrite(multiplexer, motorUsed); //Value of motorUsed : 0 moves the table, 1 moves the pen delay(100); if (sense==1){ //WARNING : have to put the real value. NOW : closkwise (sense=1) for up/right, counterclockwise (sense=-1) for down/left myStepper.step(steps/coeffOfStep); } else if (sense==-1){ myStepper.step(-steps/coeffOfStep); } }