In: Computer Science
What does each line in the code?? Please
unsigned Long timer1;
unsigned Long button_dbnc_tmr = 0;
const int User_Button = 2;
const int USER_LED_Pin = 13;
bool allow_change = 0;
int counter = 0;
int state;
void read_state_from_memory(void);
void write_state_to_memory(void);
void turnoff(void);
void flash_1s(void);
void flash_2s(void);
void flash_3s(void);
void setup()
{
read_state_from_memory();
pinMode(USER_LED_Pin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
timers();
if(digitalRead(User_Button) == 1)
{
if(allow_change == 1)
{
state++;
Serial.println(counter);
Serial.println(state);
allow_change = 0;
if(state > 3)
state = 0;
write_state_to_memory();
}
}
}
else
{ counter = 0;
allow_change = 1;
button_dbnc_tmr = 0;
}
switch(state_
{
case 0:
turn off();
break;
case 1:
flash_1s();
break;
case 2:
flash_2s();
break;
case 3:
flash_3s();
break;
}
}
void turnoff()
{
//complete by me
}
void flash_1s()
{
//complete by me
}
void flash_2s()
{
//complete by me
}
void flash_3s()
{
//complete by me
}
void timers(void)
{
static unsigned Long ms_runtime;
static int one_ms_timer;
if(millis() > (ms_runtime + 1))
{ ms_runtime = ms_runtime + 1;
one_ms_timer++;
}
else if(ms_runtime > millis())x
ms_runtime = millis();
if(one_ms_timer > 99)
{ one_ms_timer = 0;
button_dbnc_tmr++;
timer1++;
}
}
void read_state_from_memory ()
{
//to be completed by me
}
void write_state_to_memory()
{
//to be completed by me
}
The purpose of each line is added as command line.
// Declare global variable timer1 as unsigned Long (32 bit size)
unsigned Long timer1;
// Declare global variable button_dbnc_tmr as unsigned Long and initialize it to 0
unsigned Long button_dbnc_tmr = 0;
// Declare constant User_Button as integer type and initialize it to 2
const int User_Button = 2;
// Declare constant USER_LED_Pin as integer type and initialize it to 13
const int USER_LED_Pin = 13;
// Declare boolean variable allow_change and initialize it to 0
bool allow_change = 0;
// Declare integer variable counter and initialize it to 0
int counter = 0;
// Declare integer variable state
int state;
// Declare user defined function read_state_from_memory with void parameter and no return values
void read_state_from_memory(void);
// Declare user defined function write_state_to_memory with void parameter and no return values
void write_state_to_memory(void);
// Declare user defined function turnoff with void parameter and no return values
void turnoff(void);
// Declare user defined function flash_1s with void parameter and no return values
void flash_1s(void);
// Declare user defined function flash_2s with void parameter and no return values
void flash_2s(void);
// Declare user defined function flash_3s with void parameter and no return values
void flash_3s(void);
// Define a user defined function setup to setup Arduino
//function setup which is used to setup the Arduino
void setup()
{
//calling the function read_state_from_memory()
read_state_from_memory();
//calling the function pinMode() as Pins configured as OUTPUT mode , We pass USER_LED_Pin which is we declared in the begining
//Here we pass pin as USER_LED_Pin and the mode as OUTPUT
pinMode(USER_LED_Pin, OUTPUT);
//Serial.begin(9600) passes the value 9600 to the speed parameter. This tells the Arduino to get ready to exchange messages with the Serial Monitor at a data rate of 9600 bits per second.
Serial.begin(9600);
}
//// Define a user defined function loop()
void loop()
{
//calling the function timers()
timers();
//digitalRead() function will read the value from digital pin User_Button and return HIGH or LOW
if(digitalRead(User_Button) == 1)
{
//true block
//check the value in the variable allow_change is 1?
if(allow_change == 1)
{
//increase value of state
state++;
//Serial.println() function will print the counter value with a newline character
Serial.println(counter);
//Serial.println() function will print the state value with a newline character
Serial.println(state);
// change the value of allow_change to zero
allow_change = 0;
//change the value of state to zero if the current value of variable state is greater than 3
if(state > 3)
{
state = 0;
//call function write_state_to_memory()
write_state_to_memory();
}
}
}
// else case of first if clause
else
{
//change the values of counter , allow_change,button_dbnc_tmr
counter = 0;
allow_change = 1;
button_dbnc_tmr = 0;
}
//switch block for state variable, the functions turn_off(),flash_1s(),flash_2s() or flash_3s() will be called according to the value in state variable;
switch(state)
{
case 0:
turn_off();
break;
case 1:
flash_1s();
break;
case 2:
flash_2s();
break;
case 3:
flash_3s();
break;
}
}
//defenition of the function turnoff() that might be contain the code to turn off the Arduino
void turnoff()
{
//complete by me
}
//defenition of the function flash_1s that does not return aby value
void flash_1s()
{
//complete by me
}
//defenition of the function flash_2s that does not return aby value
void flash_2s()
{
//complete by me
}
//defenition of the function flash_2s that does not return aby value
void flash_3s()
{
//complete by me
}
// user defined function timers
void timers(void)
{
//declare two static variables Long ms_runtime and one_ms_timer
static unsigned Long ms_runtime;
static int one_ms_timer;
//the function millis() will return the number of milliseconds at the time, the Arduino board begins running the current program
//the time will compared with the value ms_runtime + 1, If the running time is higher than ms_runtime + 1.
if(millis() > (ms_runtime + 1))
{
//true cawse :value of ms_runtime and one_ms_timer will be incrimented
ms_runtime = ms_runtime + 1;
one_ms_timer++;
}
//else check the ms_runtime is greater than the number of milliseconds at the time, the Arduino board begins running the current program
//if it is true the value of ms_runtime is changes to the Arduino running time
else if(ms_runtime > millis())x
ms_runtime = millis();
//check the value one_ms_timer which is updated in the true case of above if condition ,is greater than 100, then it will round to zero
if(one_ms_timer > 99)
{ one_ms_timer = 0;
// the value of button_dbnc_tmr and timer1 is incrimented by 1
button_dbnc_tmr++;
timer1++;
}
}
//defenition of function read_state_from_memory ()
void read_state_from_memory ()
{
//to be completed by me
}
//defenition of function write_state_to_memory()
void write_state_to_memory()
{
//to be completed by me
}