/* SevSeg Counter Example
Copyright 2017 Dean Reading
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
This example demonstrates a very simple use of the SevSeg library with a 4
digit display. It displays a counter that counts up, showing deci-seconds.
*/
#define RED A0
#define ORANGE A1
#define GREEN A2
#define SWITCH_BUTTON 4
#include "SevSeg.h"
SevSeg sevseg; //Instantiate a seven segment controller object
typedef enum
{
POWER_OFF,
POWER_ON,
} TRAFFIC_LIGHT_POWER_STATE_E;
typedef enum
{
RED_LED_ON,
ORANGE_LED_ON,
GREEN_LED_ON
} LED_STATE_E;
//static TRAFFIC_LIGHT_POWER_STATE_E TRAFFIC_LIGHT_POWER_STATE_re;
static LED_STATE_E LED_SELECT_state_re;
// static LED_STATE_E ORANGE_state_re;
// static LED_STATE_E GREEN_state_re;
static unsigned char Count_10msec=0;
static unsigned char Count_40msec=0;
static unsigned char Count_100msec=0;
static signed char deciSeconds=10;
static unsigned char second =0;
static unsigned char SLIDE_BUTTON=0;
/*READ SLIDE BUTTON EVERY 10MSec */
void Read_Button()
{
SLIDE_BUTTON=digitalRead(SWITCH_BUTTON);
}
void Traffic_Light_POWER_CNTRL(void)
{
switch(SLIDE_BUTTON)
{
case POWER_OFF:
Traffic_Light_Power_Off_state_Cntrl();
break;
case POWER_ON:
Serial.println("1");
Traffic_Light_Power_On_state_Cntrl();
break;
}
}
void Power_Off_state_entry_fun(void)
{
T_LED_OFF();
deciSeconds=10;
sevseg.setNumber(deciSeconds, 1);
//LED_SELECT_state_re = RED_LED_ON;
}
void Traffic_Light_Power_Off_state_Cntrl(void)
{
if(SLIDE_BUTTON==HIGH)
{
LED_SELECT_state_re = RED_LED_ON;
}
else
{
T_LED_OFF();
}
}
void Traffic_Light_Power_On_state_Cntrl(void)
{
if(SLIDE_BUTTON==LOW)
{
// TRAFFIC_LIGHT_POWER_STATE_re =POWER_OFF;
Power_Off_state_entry_fun();
}
else{
//Serial.println("2");
//sevseg.setNumber(deciSeconds, 1);
Traffic_Light_Control_fun();
}
}
void RED_LED_entry_fun(void)
{
LED_SELECT_state_re =RED_LED_ON;
deciSeconds=10;
sevseg.setNumber(deciSeconds, 1);
}
void Orange_LED_entry_fun(void)
{
LED_SELECT_state_re =ORANGE_LED_ON;
deciSeconds=5;
sevseg.setNumber(deciSeconds, 1);
}
void Green_LED_entry_fun(void)
{
LED_SELECT_state_re =GREEN_LED_ON;
deciSeconds=10;
sevseg.setNumber(deciSeconds, 1);
}
void Traffic_Light_Control_fun()
{
switch(LED_SELECT_state_re)
{
case RED_LED_ON:
//deciSeconds=60;
RED_LED_du_fun();
//Fan_Power_Off_state_Cntrl();
break;
case ORANGE_LED_ON:
ORANGE_LED_du_fun();
//Serial.println("orange led on");
break;
case GREEN_LED_ON:
GREEN_LED_du_fun();
break;
}
}
void RED_LED_du_fun(void)
{
//Serial.println("3");
if(deciSeconds>=0)
{
Serial.println("RED");
digitalWrite(RED,HIGH);
Counter_Display();
second++;
}
else
{
digitalWrite(RED,LOW);
Orange_LED_entry_fun();
//set_deci_05();
}
}
void ORANGE_LED_du_fun()
{
Serial.println("4");
if(deciSeconds>=0)
{
digitalWrite(ORANGE,HIGH);
Counter_Display();
second++;
}
else
{
digitalWrite(ORANGE,LOW);
Green_LED_entry_fun();
}
}
void GREEN_LED_du_fun()
{
Serial.println("5");
if(deciSeconds>=0)
{
digitalWrite(GREEN,HIGH);
Counter_Display();
second++;
}
else
{
digitalWrite(GREEN,LOW);
RED_LED_entry_fun();
}
}
void T_LED_OFF()
{
digitalWrite(RED,LOW);
digitalWrite(ORANGE,LOW);
digitalWrite(GREEN,LOW);
RED_LED_entry_fun();
}
/* Counter initilization */
void Counter_INIT(void)
{
byte numDigits = 2;
byte digitPins[] = {2, 3};
byte segmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13};
bool resistorsOnSegments = false; // 'false' means resistors are on digit pins
byte hardwareConfig = COMMON_ANODE; // See README.md for options
bool updateWithDelays = true; // Default 'false' is Recommended
bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros
bool disableDecPoint = true; // Use 'true' if your decimal point doesn't exist or isn't connected
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
updateWithDelays, leadingZeros, disableDecPoint);
sevseg.setBrightness(90);
}
void pin_INTI(void)
{
pinMode(RED, OUTPUT); //Set the pin to be OUTPUT
pinMode(GREEN, OUTPUT);
pinMode(ORANGE,OUTPUT);
pinMode(SLIDE_BUTTON,INPUT);
}
void Counter_Display(void)
{
if(second==0)
{
deciSeconds--;
sevseg.setNumber(deciSeconds, 1);
//deciSeconds--;
}
if(second>10)
{
deciSeconds--;
sevseg.setNumber(deciSeconds, 1);
second=0;
}
// Must run repeatedly
sevseg.refreshDisplay();
}
/* Timer Initilization */
void Init_Timer(void)
{
cli(); //stop interrupts for till we make the settings
/*1. First we reset the control register to amke sure we start with everything disabled.*/
TCCR1A = 0; // Reset entire TCCR1A to 0
TCCR1B = 0; // Reset entire TCCR1B to 0
/*2. We set the prescalar to the desired value by changing the CS10 CS12 and CS12 bits. */
TCCR1B |= B00000011; //Set CS12 to 011 so we get prescalar 64
/*3. We enable compare match mode on register A*/
TIMSK1 |= B00000010; //Set OCIE1A to 1 so we enable compare match A
/*4. Set the value of register A to 31250*/
OCR1A = 250; //Finally we set compare register A to this value /**/
sei(); //Enable back the interrupts
}
void TASK_10msec()
{
Read_Button();
}
void TASK_40msec()
{
}
void TASK_100msec()
{
Traffic_Light_POWER_CNTRL();
}
void setup()
{
Serial.begin(115200);
pin_INTI();
Counter_INIT();
Init_Timer();
//LED_INIT();
}
void loop() {
// put your main code here, to run repeatedly:
if(Count_10msec>=10) /* 10msec*/
{
Count_10msec = 0;
TASK_10msec();
}
else
{
/*do nothing*/
}
if(Count_40msec>= 40) /* 40 msec*/
{
Count_40msec = 0;
TASK_40msec ();
}
else
{
/*do nothing */
}
/* 100msec*/
if(Count_100msec>= 100)
{
Count_100msec = 0;
TASK_100msec () ;
}
else
{
/*do nothing*/
}
}
/**************do not change***********/
/*===========================================*/
//With the settings above, this IRS will trigger each 500ms.
ISR(TIMER1_COMPA_vect)
{
TCNT1 = 0; //First, set the timer back to 0 so it resets for next interrupt
Count_10msec++;
Count_40msec++;
Count_100msec++;
}
/// END ///