Skip to content

Instantly share code, notes, and snippets.

@Abhilekhgautam
Created February 6, 2024 09:35
Show Gist options
  • Save Abhilekhgautam/104d2b035437349b3296856bfdb00d6d to your computer and use it in GitHub Desktop.
Save Abhilekhgautam/104d2b035437349b3296856bfdb00d6d to your computer and use it in GitHub Desktop.
Solution to Firmware Engineering Internship Assesment
// 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);
}
// 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');
}
@lomassubedi
Copy link

How can you make sure that the string will have newline character in here?

@Abhilekhgautam
Copy link
Author

Pressing an Enter Key after inputting the command would work.

@lomassubedi
Copy link

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