Lesson 10 – Timer Example Code

Lesson 10 Timer Code Example

// 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]