Some new stuff
  1. Arduino Park Control
  2. Arduino USB2serial programming the bare bone
  3. Arduino Projects Videos added
  4. Arduino Xbee projects
  5. Arduino dB Meter 
  6. Trying to get the IOIO going. 
  7. IOIO is going....bluetooth
  8. Attiny 2313 bluetooth bee RC car
  9. Compass and temperature added
  10. AVR programmer
  11. USB2Serial and Ethernet POE
  12. Arduino LPG Sensor
  13. Program the Arduino Mini 05
  14. Lipo Rider Pro in action
  15. EZ Robot Builder kit arrived 10 Nov 2012
  16. Arduino VA Meter!
  17. 05 Nov 2012 Project Sentry Gun.
  18. Attiny85 PIR Sensor 10 Dec 2012 
  19. 0-30VDC3A and 2-28VDC10A bench PSU's
  20. 19 Dec 2012 Arduino Voltmeter LCD
  21. 29 Dec 2012 Arduino Tachometer&Speedometer LCD
  22. 03 Jan 2013 Arduino AC Phase Control. 
  23. 18 Jan 2013 Arduino Distance Meter
  24. 22 Jan 2013 Arduino Digital Clock and Date
  25. 31 Jan 2013 Arduino Digital Clock and Date 8x2 LCD
  26. Feb 2013 LCD and Backpack
  27. Feb 2013 7 Segment Serial Clock Sparkfun. Distance Sensor with "newping" library
  28. Feb 2013 Lelo Remote
  29. 01/03/2013 Arduino Adafruit 7 Seg Digital Clock with RTC and backpack 
  30. Arduino Frequency Meter 26/03/2013
  31. Another RC  Car controlled by Arduino 06/04/2013
  32. My quadcopter project update 25 April 2013
  33. My Aqua Quad Copter Flying 26 April 2013enlightened
  34. Another monster RC truck;strong one! 11July2013cool
  35. X-Frame Quad Copter 31 July 2013cool
  36. Arduino GSM Sheild added.28/8/2013
  37. 1.8inch TFT screen 13/09/2013cool
  38. 04/11/2014 added PID Soldering Iron Control
  39. Added bluetooth servo control 04/11/2014
  40. ESP8266 first try.16/03/2015
  41. Arduino Uno Quadcopter 19 May 2015
  42. IOT WemosD1 Amp Meter

 

 



Arduino Adjustable Timer 

The code is from BANJO'S website.

The timing is set with a potentiometer.

I changed the code to accommodate adafruit backpack LCD16x2.

 

unsigned long ONE_SECOND = 1000;  //1 second is 1000 microSeconds
unsigned long ONE_MINUTE = ONE_SECOND * 60;
int MAX_ADC_COUNTS = 1024; //ADC
unsigned long SENSOR_READ_DELAY_TIME = ONE_SECOND;

unsigned long MAX_CYCLE_TIME =  3 * ONE_MINUTE; // This controls the total amount of time for a period (both 1 one and 1 off time)
unsigned long IGNORE_MINIMUM =  1 * ONE_SECOND;  // In case you are controlling something that you don't want cycled too fast.
int ALARM_COUNTS = 250;  //blink rate.  Each is on for this amount of time.
#include "Wire.h"
#include "LiquidCrystal.h"
// A4 = SDA,A5 = SCL
LiquidCrystal lcd(0);
//pin assignments 
int UNO_LED_PIN = 13;  //LED on the uno board, so no external connections
int ON_LED_PIN = 9;  //LED if using an external state LED
int OFF_LED_PIN = 10;   //LED timer off 
int TIMER_OUTPUT_PIN = 8;        //This controls the onboard relay that controls the timer output contacts.
int TIMER_RATIO_INPUT_PIN = A0;  //this is the onboard potetiometer that controls the ratio of on to off
int TIMER_ON = HIGH;  //used because may use sink current at some point
int TIMER_OFF = LOW;  //used because may use sink current at some point
void setup() {                
  Serial.begin(9600);
  lcd.begin(16,2);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("V=");
  lcd.setCursor(5,0);
  lcd.print("/ON=");
  lcd.setCursor(0,1);
  lcd.print("%");
  lcd.setCursor(5,1);
  lcd.print("/OF=");
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(UNO_LED_PIN, OUTPUT);
  pinMode(ON_LED_PIN, OUTPUT);
  pinMode(OFF_LED_PIN, OUTPUT);
  pinMode(TIMER_OUTPUT_PIN, OUTPUT);
  Serial.println("setup()...Entry & Exit"); 
}
void loop() {
  Serial.println("loop()...Entry"); 
  turnTimerOff();  //de-energize the relay
  turnTimerOn();  //energize the releay
  Serial.println("loop()...Exit"); 
}
float getSetpointRatio() {
  //rather than counts of 0 - 1023 (range of the onboard ADC), return the 0% - 100% of range
  float ratio = 0.0;
  int sensorValue = 0;
  sensorValue = analogRead(TIMER_RATIO_INPUT_PIN);
  ratio = float(sensorValue) / float(MAX_ADC_COUNTS);
  Serial.print("getSetpointRatio()....sensorValue: "); Serial.println(sensorValue);
  lcd.setCursor(2,0);
  lcd.print(sensorValue); 
  Serial.print("getSetpointRatio()....ratio: "); Serial.println(ratio);
  lcd.setCursor(1,1);
  lcd.print(ratio);
  return ratio;
 }
//----------------------------------------------------------------------------------------------------------------------------------------
void turnTimerOn () {
  //time controlled, not temperature controlled
  unsigned long cumulativeOnTime = 0;
  unsigned long timerOnTime = 0;
  unsigned long timerOffTime = 0;  //special case; keep on if no off time
  float ratio;
  Serial.println("turnTimerOn()...time controlled...entry..............................");
  do {
    Serial.println("turnTimerOn()...do loop top.");  
    ratio = getSetpointRatio();
    timerOnTime = calcOnTime(ratio);
    Serial.print("turnTimerOn()...timerOnTime: "); Serial.println(timerOnTime);
    Serial.print("turnHeatherOn()...IGNORE_MINIMUM: "); Serial.println(IGNORE_MINIMUM);
    if(timerOnTime < IGNORE_MINIMUM) {  //need to refactor this
      Serial.println("turnTimerOn()...less than IGNORE_MINIMUM...exiting.");
      return;
    }
    timerOn(); //energize the relay
    delay(SENSOR_READ_DELAY_TIME); //simple way to be able to provide approx on time.
    cumulativeOnTime += SENSOR_READ_DELAY_TIME;
    Serial.print("turnTimerOnUsingTimer()...cumulativeOnTime: ");Serial.println(cumulativeOnTime);
    Serial.print("turnTimerOnUsingTimer()...timerOnTime: ");Serial.println(timerOnTime);
    }   while(cumulativeOnTime < timerOnTime);
    Serial.println("turnTimerOnUsingTimer()...Exit.");
}
void turnTimerOff() {
  unsigned long cumulativeOffTime = 0;
  unsigned long timerOffTime = 0;
  float ratio = 0.0;
  Serial.println("turnTimerOff()...time controlled...entry.............................");
  do {
    Serial.println("turnTimerOffUsingTimer()...do loop top.");  
    ratio = getSetpointRatio();
    timerOffTime = calcOffTime(ratio);
    Serial.print("turnTimerOffUsingTimer()...timerOffTime: "); Serial.println(timerOffTime);
    Serial.print("turnHeatherOffUsingTimer()...IGNORE_MINIMUM: "); Serial.println(IGNORE_MINIMUM);
    if(timerOffTime < IGNORE_MINIMUM) {  //need to refactor this
      Serial.println("turnTimerOffUsingTimer()...less than IGNORE_MINIMUM...exiting.");
      return;
       }
       timerOff();  //de-energize the relay
    delay(SENSOR_READ_DELAY_TIME); //simple way to be able to provide approx off time.
    cumulativeOffTime += SENSOR_READ_DELAY_TIME;
    Serial.print("turnTimerOffUsingTimer()...cumulativeOffTime: ");Serial.println(cumulativeOffTime);
    Serial.print("turnTimerOffUsingTimer()...timerOffTime: ");Serial.println(timerOffTime);
  
  }   while(cumulativeOffTime < timerOffTime);
  Serial.println("turnTimerOffUsingTimer()...Exit.");
}
unsigned long calcOnTime(float ratio) {
  //time is a function of MAX_CYCLE_TIME; if this is seconds, then return seconds; if mins, then return mins
  unsigned long onTime = 0;
  Serial.println("calcOnTime()....Entry");
  onTime = ratio * MAX_CYCLE_TIME;
  Serial.print("calcOntime()....onTime: "); Serial.println(onTime);
  lcd.setCursor(9,0);
  lcd.print(onTime);
  Serial.println("calcOnTime()....Exit");
  return onTime;
  }
unsigned long calcOffTime(float ratio) {
  //time is a function of MAX_CYCLE_TIME; if this is seconds, then return seconds; if mins, then return mins
  unsigned long offTime = 0;
  float offRatio = 0.0;
  Serial.println("calcOffTime()....Entry");
  //use calcOnTime for brevity
  offTime = calcOnTime(ratio);
  offTime = MAX_CYCLE_TIME - offTime;
  Serial.print("calcOffTime()....offTime: "); Serial.println(offTime);
  lcd.setCursor(9,1);
  lcd.print(offTime);
  Serial.println("calcOffTime()....Exit");
  return offTime;
 
}
void timerOn() {
  Serial.println("timerOn() ... entry.");
  digitalWrite(TIMER_OUTPUT_PIN, TIMER_ON);   // sink current
  digitalWrite(OFF_LED_PIN, LOW);
  digitalWrite(UNO_LED_PIN, HIGH);   // set the LED on
  digitalWrite(ON_LED_PIN, HIGH);
  Serial.println("timerOn() ... exit.");
}
void timerOff() {
  Serial.println("timerOff() ... entry.");
  digitalWrite(TIMER_OUTPUT_PIN, TIMER_OFF);     
  digitalWrite(UNO_LED_PIN, LOW);   // set the LED on
  digitalWrite(ON_LED_PIN, LOW);
  digitalWrite(OFF_LED_PIN, HIGH);
  Serial.println("timerOff() ... exit.");
}  
void alarm(int sensorValueCounts) {  //needs to be refactored to use timerOff()
  //NOTE: alarm function turns relay coil off.  This is left here as a convenience for
  //  code you may develop.  It is used in a later implementation of this software,
  //  where I check for gas pilot light on.
  //
  //blink both on and off LEDs
  //
  Serial.println("alarm()....Entry...will not exit!");
  //TURN OFF!
  digitalWrite(TIMER_OUTPUT_PIN, TIMER_OFF);   
  digitalWrite(OFF_LED_PIN, LOW);
  
  Serial.print("alarm()....sensor low. sensorValueCounts: "); Serial.println(sensorValueCounts);
  while(1) {
    //keep turning timer off!
    digitalWrite(TIMER_OUTPUT_PIN, TIMER_OFF);
    
    //show it by alternating blinking both LEDs
    digitalWrite(UNO_LED_PIN, HIGH);
    digitalWrite(ON_LED_PIN, HIGH);
    digitalWrite(OFF_LED_PIN, LOW);
    delay(ALARM_COUNTS);

    digitalWrite(UNO_LED_PIN,LOW);
    digitalWrite(ON_LED_PIN, LOW);
    digitalWrite(OFF_LED_PIN, HIGH);
    delay(ALARM_COUNTS);
  }
}

Back Back to top