Lesson 10 Timer Code Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
// By Roee Bloch TIMER EXAMPLE //All right Reserved // Copyright (c) 2015 All Right Reserved, http://www.electronics-freak.com // // This source is subject to the Roee Bloch License. // Please see the License.txt file for more information. // All other rights reserved. // // THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A // PARTICULAR PURPOSE. // // </copyright> // <author>Roee Bloch</author> // <email>roeebloch@walla.co.il</email> // <date>June 2015</date> #define analog_in 0 // define analog input pin in use #define Seconds 2 // this pin will be seconds #define Minutes 3 // this pin will be Minutes #define start_timer 4 // start timer command pin #define LED 13 // on board LED #define timer_out 7 // timer output command char function = 0; // 0 ->will be for after time -> ON, 1 -> after time off int my_analog; int remap; char opt_chosen; // for seconds 1, for minutes 2 char light = 0, start; char state = 0; // for on board LED to blink void setup() { // put your setup code here, to run once: pinMode(Seconds, INPUT); digitalWrite(Seconds, HIGH); // week pullup on this pin pinMode(Minutes, INPUT); digitalWrite(Minutes, HIGH); // week pullup on this pin pinMode(start_timer, INPUT); digitalWrite(start_timer, HIGH); // week pullup on this pin Serial.begin(9600); // Serial monitor pinMode(timer_out, OUTPUT); pinMode(LED, OUTPUT); if (function == 1) { digitalWrite(timer_out, 1); } else { digitalWrite(timer_out, 0); } // Serial.println("state = "); // Serial.println(function); // Serial.println("now state = "); // Serial.println(function); // state = function; // start command state } void loop() { // put your main code here, to run repeatedly: my_analog = analogRead(analog_in); // Serial.println (my_analog); //for debug only Serial.println (remap); // also for debug if (digitalRead(Seconds) == 0) { opt_chosen = 1; } if (digitalRead(Minutes) == 0) { opt_chosen = 2; } remap = map (my_analog, 0, 1024, 0, 100); // remapping to switch case switch (opt_chosen) { case 2: Serial.println("Minutes"); break; case 1: Serial.println("Seconds"); break; default: Serial.println("Nothing was chosen"); } start = digitalRead(start_timer); if (start == 0) { Serial.println("Timer started"); Run_timer(opt_chosen, remap); } delay(200); // 200mS sedlay } void Run_timer(char opt_chosen, int mytime) { if ( opt_chosen == 1) //seconds chosen { for (int x = 0; x < mytime ; x++) { delay(1000); Serial.println(x); digitalWrite(LED, state); state = ~ state; // change LED state } // of for Serial.println("Timer Finished!"); Serial.println(function); if (function == 1) { digitalWrite(timer_out, LOW); } else { digitalWrite(timer_out, HIGH); } loop1: goto loop1; return; } // of if if ( opt_chosen == 2) //seconds chosen { for (int x = 0; x < mytime ; x++) for (int y = 0; y < 60; y++) { { delay(1000); Serial.print("Minutes="); Serial.println(x); Serial.print("Seconds="); Serial.println(y); digitalWrite(LED, state); state = ~ state; // change LED state } // of for } Serial.println("Timer Finished!"); Serial.println(function); if (function == 1) { digitalWrite(timer_out, LOW); } else { digitalWrite(timer_out, HIGH); } loop2: goto loop2; return; } // of if } |
Video Based on this Code: lesson-10-arduino-timer-multi-functions
[adsenseyu6] |