// Controlling a servo position by a time controlled counter // and display it on a scale by pointer moved by servo. // Setting the start time with a variable resistor (Poti)and start it with pushbutton start/stop // IO 13 indicates that timer runs. Pressing start/stop again runs or stops timer also indicated by IO 13. // Pause is indicated by flashing IO 13. // By pressing pushbotton reset the complete time will be reset. // Stop the alarm with any key by holding it unless the alarm is stoping. // IO 10 is high when alarm is on and low when alarm is off // by Michal J.Franz #include // Funktions from Arduino System #include "pitches.h" // Funktion userdefined copy in your Project Folder Servo mypointerservo0; // create servo object to control a servo // set pin numbers: const int buttonStart = 2; // the number of the pushbutton pin for start/stop const int buttonReset = 3; // the number of the pushbutton pin for reset const int LED_TimeCount = 13; // the number of the port to connect LED const int LED_PauseCount = 12; // the number of the port to connect LED const int LED_Alarm = 11; // the number of the port to connect LED const int ANAin0 = 0; // analog pin 0 used to measure a voltage over two cables // the pin is protected over a diode and a resistor. // variables will change: int melody[] = { NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4}; // note durations: 4 = quarter note, 8 = eighth note, etc.: int noteDurations[] = { 4,8,8,4,4,4,4,4 }; long previousMillis = 0; // will store last time LED was updated long interval = 630; // interval for time count int buttonStateStart = 1; // variable for reading the pushbutton status int buttonStateReset = 1; // variable for reading the pushbutton status int countOnOff=1; // Switch time count-down on or off means start or stop timer int ANAval0; // variable to read the value from the analog pin to set value of variable TIMECOUNT0 int TIMECOUNT0; // Time to estimate void setup() // set up { // initialize the LED pin as an output: pinMode(LED_TimeCount, OUTPUT); pinMode(LED_PauseCount, OUTPUT); pinMode(LED_Alarm, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonStart, INPUT); // pinMode(buttonReset, INPUT); mypointerservo0.attach(9); // attaches the servo on pin 9 to the servo object ANAval0=1024; } void loop() // This is the mainprogram { AscTime(); // setting the thime with Poti (variable resistor) // and display the time with a servo on a scale RunTime(); // count down and display the remaining time // asking reset and start/stop button to reset or to stop time coutn down. ALARM(); // give alarm with a melody // asking reset and start/stop button to stop alarm } // End Loop do Loop again // My Routines doing in loop //========================================================================================================================= void AscTime() { // read the state of the pushbutton value: while(buttonStateStart == HIGH){ settime(); // setting the time buttonStateStart = digitalRead(buttonStart); buttonStateReset = digitalRead(buttonReset); // Ask buttons } while(buttonStateStart == LOW){ settime(); buttonStateStart = digitalRead(buttonStart); // debounce start button } } void RunTime() { while ((ANAval0 >1)&(buttonStateReset == HIGH)) { // do until both states are true countdown(); buttonStateStart = digitalRead(buttonStart); buttonStateReset = digitalRead(buttonReset); if (buttonStateStart==LOW) { if (countOnOff==HIGH) { countOnOff=LOW; } else{countOnOff=HIGH;} } while(buttonStateStart == LOW) { buttonStateStart = digitalRead(buttonStart); } } } void ALARM() { while((buttonStateReset == HIGH)&(buttonStateStart == HIGH)) { digitalWrite(LED_Alarm, HIGH); digitalWrite(LED_TimeCount, LOW); digitalWrite(LED_PauseCount, LOW); melodie(); buttonStateReset = digitalRead(buttonReset); buttonStateStart = digitalRead(buttonStart); } digitalWrite(LED_Alarm, LOW); // read the state of the pushbutton value: buttonStateStart = digitalRead(buttonStart); while(buttonStateStart == LOW){ settime(); buttonStateStart = digitalRead(buttonStart); } digitalWrite(LED_Alarm, LOW); digitalWrite(LED_TimeCount, LOW); digitalWrite(LED_PauseCount, LOW); countOnOff=1; //set clock to start } void settime() { ANAval0 = analogRead(ANAin0); // reads the value of the voltage up to 5 V (value between 0 and 1023) TIMECOUNT0 = map(ANAval0, 0, 1023, 179, 0); // scale it to use it with the servo (value between 0 and 180) and this reverse! mypointerservo0.write(TIMECOUNT0); // sets the servo position according to the measured voltage delay(20); // waits for the servo to get there } void countdown() { if (buttonStateReset == HIGH) { if (countOnOff==HIGH) { if (millis() - previousMillis > interval) { // save the last time you count the time previousMillis = millis(); ANAval0=ANAval0-1; } digitalWrite(LED_TimeCount, HIGH); digitalWrite(LED_PauseCount, LOW); } //merker else // indicate pausestate { digitalWrite(LED_TimeCount, LOW); digitalWrite(LED_PauseCount, HIGH); } } TIMECOUNT0 = map(ANAval0, 0, 1023, 179, 0); // scale it to use it with the servo (value between 0 and 180) and this reverse! mypointerservo0.write(TIMECOUNT0); // sets the servo position according to the measured voltage delay(20); // waits for the servo to get there } void melodie() { for (int thisNote = 0; thisNote < 8; thisNote++) { // to calculate the note duration, take one second // divided by the note type. // e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. int noteDuration = 1000/noteDurations[thisNote]; tone(8, melody[thisNote],noteDuration); // to distinguish the notes, set a minimum time between them. // the note's duration + 30% seems to work well: int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); } }