Question

In: Computer Science

Arduino Uno code, Please explain what does this code do? unsigned long ms_runtime; int one_ms_timer; //define...

Arduino Uno code, Please explain what does this code do?

unsigned long ms_runtime;
int one_ms_timer;
//define all timers as unsigned variables
unsigned long timer1 = 0; // timer1 increments every 100ms = 0.1s
const int USER_LED_OUTPUT = 13;
const int USER_BUTTON_INPUT = 2;
void setup()
{ // initialize the digital pin as an output.
pinMode(USER_LED_OUTPUT, OUTPUT);
pinMode(USER_BUTTON_INPUT, INPUT);
Serial.begin(9600);
}
void loop()
{ // run loop forever
static int state, old_state,counter;
timers();
// logic for state change
if(state == 0)
{ if(digitalRead(USER_BUTTON_INPUT) == 1)
{ if(timer1 > 5)
state = 1;
}
else
timer1 = 0;
}
else if(state == 1)
{ if(digitalRead(USER_BUTTON_INPUT) == 0)
{ if(timer1 > 5)
state = 0;
}
else
timer1 = 0;
}
// execute commands based on state
if (state == 1)
digitalWrite(USER_LED_OUTPUT, HIGH);
else if (state == 0)
digitalWrite(USER_LED_OUTPUT, LOW);
// monitor and print changed of state
if(old_state != state)
{ counter++;
Serial.print("counter = ");
Serial.println(counter);
old_state = state;
}
}
void timers(void)
{
int i;
if(millis() > (ms_runtime + 1))
{ ms_runtime = ms_runtime + 1;
one_ms_timer++;
}
else if( ms_runtime > millis())
ms_runtime = millis();
if(one_ms_timer > 99)
{ timer1++;
one_ms_timer = 0;
}
}

Solutions

Expert Solution

The code is implemented as follows:

the timers() function returns the is basically for marking time in 100 ms.

in the setup() function we basically write all the defined pin modes like USER_LED_OUTPUT is for output and  USER_BUTTON_INPUT is for input .

void loop is the part of the code where main functionality occurs. If the state is zero and user gives high input in the pin then if the timer is greater than zero i.e time passed is greater than 500 ms then value of state is set to 1 and the code gets into else if condition , but if input was not passed by the user then the timer1 is retained as zero.

In the elseif case the similar condition occurs , if user doesn't pass the input as low , then timer1 is  retained to zero else state is made zero.

Then if the state is 1 then output in led is HIGH else led shows LOW.

Then finally the output is printed on the screen on the basis of the value of state. If value of state differs from that of the initial value , then counter is increased and its value is being printed and now old state is made equal to new state. It shows the number of time value of state changes in our experiment.


Related Solutions

What does each line in the code?? Please unsigned Long timer1; unsigned Long button_dbnc_tmr = 0;...
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;...
We need to code an LCD with a temperature and humidity sensor for the arduino UNO...
We need to code an LCD with a temperature and humidity sensor for the arduino UNO board. The LCD should simply display the temperature and humidity reading from the DHT11 sensor. We must do this without the use of arduino or lcd libraries, and only use raw c code. For example we need to use PORTD |= (1<<5); to set the pin to 5. The LCD is connected to pins A0 and A1, and it is using I2C. The temperature...
this is code to Temperature and Humidity Monitoring over ThingSpeak using Arduino UNO and ESP8266 i...
this is code to Temperature and Humidity Monitoring over ThingSpeak using Arduino UNO and ESP8266 i need someone explain the steps for me #include <stdlib.h> #include <DHT.h> #define DHTPIN 5         // DHT data pin connected to Arduino pin 5 #define DHTTYPE DHT11     // DHT11 (DHT Sensor Type ) DHT dht(DHTPIN, DHTTYPE); // Initialize the DHT sensor #define SSID "WiFi Name"     // "WiFi Name" #define PASS "WiFi Password"       // "Password" #define IP "184.106.153.149"// thingspeak.com ip String msg = "GET /update?key=Your API...
I need an Arduino uno code to measure high low levels with hcsr04 of a tank...
I need an Arduino uno code to measure high low levels with hcsr04 of a tank and display it on i2c 20x4 lcd.
Please explain this code line by line void printperm(int *A,int n,int rem,int j) {    if(n==1)...
Please explain this code line by line void printperm(int *A,int n,int rem,int j) {    if(n==1)       {        for(int k=0;k<j;k++)        cout<<A[k]<<" + ";        cout<<rem<<"\n";        return;       }     for(int i=0;i<=rem;i++)    {          if(i<=rem)          A[j]=i;          printperm(A,n-1,rem-i,j+1);    } }
Please do this code with python. Thank you! struct Node {     int data;     struct...
Please do this code with python. Thank you! struct Node {     int data;     struct Node* left;     struct Node* right; }; // Node creation struct Node* newNode(int data) {     struct Node* nn         = new Node;     nn->data = data;     nn->left = NULL;     nn->right = NULL;     return nn; } // Function to insert data in BST struct Node* insert(struct Node* root, int data) {   if (root == NULL)         return newNode(data);     else {...
Explain the following code. What language? What does it do? What is the result?                 <!DOCTYPE...
Explain the following code. What language? What does it do? What is the result?                 <!DOCTYPE teo[                                 <ELEMENT teo((stations| databases)+)>                                 <ELEMENT stations(stationName stationLocation sensors*)> <ELEMENT databases(databaseName databaseType )> <ELEMENT stationName(#PCDATA)> <ELEMENT stationLocation(#PCDATA)> <ELEMENT sensors (sensorName phenomenon)+> <ELEMENT sensorName(#PCDATA)> <ELEMENT phenomenon(#PCDATA)> <ELEMENT databaseName(#PCDATA)> <ELEMENT databaseType(#PCDATA)> <!ATTLIST stations boundingBox CDATA #required> ]>
All QUESTIONS, PLEASE 5.    What is the output of the following code: int product = 1,...
All QUESTIONS, PLEASE 5.    What is the output of the following code: int product = 1, i = 6; while (i < 9) {       product = product * i;       i++; } cout << “i is : ” << i << endl; cout << “product is : ” << product << endl; 6.    What is the output of the following code: int product = 1, i = 6;      do {       product = product * i;       i++;...
I have this mystery code. I dont know what the code does. Can somone please explain...
I have this mystery code. I dont know what the code does. Can somone please explain with examples. (python 3.xx) from typing import Dict, TextIO, Tuple, List def exam(d1: Dict[str, List[int]], d2: Dict[int, int]) -> None: """ *Mystery code* """ for key in d1: value = d1[key] for i in range(len(value)): value[i] = d2[value[i]]   
1)What is the output of the following code? struct someType { int a; int b; };...
1)What is the output of the following code? struct someType { int a; int b; }; void f1(someType &s) { s.a = 1; s.b = 2; } someType f2(someType s) { someType t; t = s; s.a = 3; return t; } int main() { someType s1, s2; f1(s1); s2 = f2(s1); cout << s1.a << '-' << s1.b << '-' << s2.a << '-' << s2.b << '-' << endl; return 0; } ------------------------------------------------------------------------------------------------------------------ 2) struct dateType { int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT