Created
February 6, 2024 09:35
-
-
Save Abhilekhgautam/104d2b035437349b3296856bfdb00d6d to your computer and use it in GitHub Desktop.
Solution to Firmware Engineering Internship Assesment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// q no 1 | |
const int LED_ONE = 7; | |
const int LED_TWO = 12; | |
int prev_milli_one = 0; | |
int prev_milli_two = 0; | |
const int INTERVAL_ONE = 1000; | |
const int INTERVAL_TWO = 2000; | |
int led_state_one = LOW; | |
int led_state_two = LOW; | |
void setup() { | |
pinMode(LED_ONE, OUTPUT); | |
pinMode(LED_TWO, OUTPUT); | |
} | |
void loop() { | |
unsigned long currentMillis = millis(); | |
if (currentMillis - prev_milli_one >= INTERVAL_ONE) { | |
prev_milli_one = currentMillis; | |
// Toggle LED 1 state | |
if (led_state_one == LOW) { | |
led_state_one = HIGH; | |
} else { | |
led_state_one = LOW; | |
} | |
digitalWrite(LED_ONE, led_state_one); | |
} | |
// Toggle LED 2 every 2 seconds | |
if (currentMillis - prev_milli_two >= INTERVAL_TWO) { | |
prev_milli_two = currentMillis; | |
if (led_state_two == LOW) { | |
led_state_two = HIGH; | |
} else { | |
led_state_two = LOW; | |
} | |
digitalWrite(LED_TWO, led_state_two); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// q no 4 | |
#include <Arduino_FreeRTOS.h> | |
const int LED_ONE = 7; | |
const int LED_TWO = 12; | |
void Blink_One_Sec( void *pvParameters ); | |
void Blink_Two_Sec( void *pvParameters ); | |
void setup() { | |
xTaskCreate( | |
Blink_One_Sec, | |
"Blink One Sec" , | |
128, | |
NULL, | |
1, | |
NULL | |
); | |
xTaskCreate( | |
Blink_Two_Sec, | |
"Blink Two Sec", | |
128, | |
NULL, | |
1, | |
NULL | |
); | |
} | |
void loop(){} | |
void Blink_One_Sec(void *pvParameters){ | |
pinMode(LED_ONE, OUTPUT); | |
for (;;) { | |
digitalWrite(LED_ONE, HIGH); | |
vTaskDelay( 1000 / portTICK_PERIOD_MS ); | |
digitalWrite(LED_ONE, LOW); | |
vTaskDelay( 1000 / portTICK_PERIOD_MS ); | |
} | |
} | |
void Blink_Two_Sec(void *pvParameters){ | |
pinMode(LED_TWO, OUTPUT); | |
for (;;){ | |
digitalWrite(LED_TWO, HIGH); | |
vTaskDelay( 2000 / portTICK_PERIOD_MS ); | |
digitalWrite(LED_TWO, LOW); | |
vTaskDelay( 2000 / portTICK_PERIOD_MS ); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// q no 2 | |
const int PUSH_BTN = 2; | |
const int LED_ONE = 12; | |
volatile bool Btn_ON = false; | |
void setup() | |
{ | |
pinMode(LED_ONE, OUTPUT); | |
pinMode(PUSH_BTN, INPUT_PULLUP); | |
attachInterrupt(digitalPinToInterrupt(PUSH_BTN), CheckStatusAndToggle, FALLING); | |
} | |
void CheckStatusAndToggle(){ | |
if(digitalRead(PUSH_BTN) == LOW){ | |
Btn_ON = !Btn_ON; | |
digitalWrite(LED_ONE, Btn_ON); | |
} | |
} | |
void loop() | |
{ | |
// delay doesn't matter. | |
delay(500); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// q no 3 | |
const int LED_PIN = 13; | |
volatile bool ledState = LOW; | |
void setup() { | |
pinMode(LED_PIN, OUTPUT); | |
Serial.begin(9600); | |
// Set up Timer 1 for interrupt every 1 second | |
TCCR1A = 0; | |
TCCR1B = 0; | |
TCNT1 = 0; | |
OCR1A = 15624; | |
TCCR1B |= (1 << WGM12); | |
TCCR1B |= (1 << CS12) | (1 << CS10); // 1024 prescalar | |
TIMSK1 |= (1 << OCIE1A); | |
} | |
void loop() { | |
// Check for commands received through UART | |
if (Serial.available() > 0) { | |
String command = Serial.readStringUntil('\n'); | |
if (command.indexOf("{\"command\":\"on\",\"gpio\":18}") != -1) { | |
digitalWrite(LED_PIN, HIGH); | |
} else if (command.indexOf("{\"command\":\"off\",\"gpio\":18}") != -1) { | |
digitalWrite(LED_PIN, LOW); | |
} | |
} | |
} | |
ISR(TIMER1_COMPA_vect) { | |
// Transmit character 'A' via UART every 1 second | |
Serial.write('A'); | |
} |
Pressing an Enter Key after inputting the command would work.
This can be programmed not to send CR or LF on enter. But anyway the overall solution is considerable. Will be updating further by email. Thank you.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How can you make sure that the string will have newline character in here?