Skip to content

Instantly share code, notes, and snippets.

@yuasatakayuki
Created September 2, 2018 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuasatakayuki/4e12d32a1d9f59acb8427a82d15db527 to your computer and use it in GitHub Desktop.
Save yuasatakayuki/4e12d32a1d9f59acb8427a82d15db527 to your computer and use it in GitHub Desktop.
#include <SoftwareSerial.h>
// Serial.print() outputs to the USB-UART (i.e. eventually to the PC).
//
uint8_t PIN_ORANGE_LED = PD7;
uint8_t PIN_BUZZER = PD6;
uint8_t PIN_SOFT_UART_RX = PD4;
const uint8_t BUFFER_SIZE = 16;
uint8_t rxBuffer[BUFFER_SIZE];
SoftwareSerial softSerial(PIN_SOFT_UART_RX, PC2); // rx, tx
enum { LED_OFF = 0, LED_ON = 1 };
uint8_t ledState = LED_OFF;
void onLED() {
digitalWrite(PIN_ORANGE_LED, HIGH);
ledState = LED_ON;
}
void offLED() {
digitalWrite(PIN_ORANGE_LED, LOW);
ledState = LED_OFF;
}
void toggleLED() {
if (ledState == LED_ON) {
offLED();
} else {
onLED();
}
}
void beep() {
digitalWrite(PIN_BUZZER, HIGH);
delay(80);
digitalWrite(PIN_BUZZER, LOW);
delay(80);
digitalWrite(PIN_BUZZER, HIGH);
delay(80);
digitalWrite(PIN_BUZZER, LOW);
}
uint8_t readSoftSerial() {
uint8_t numBytes = 0;
softSerial.listen();
while (numBytes < BUFFER_SIZE && softSerial.available()) {
rxBuffer[numBytes] = softSerial.read();
Serial.print(rxBuffer[numBytes], HEX);
++numBytes;
delay(50);
}
return numBytes;
}
void consumeAllSoftSerial() {
uint8_t numReceivedBytes = 1;
while (numReceivedBytes > 0) {
numReceivedBytes = readSoftSerial();
}
}
void setup() {
// Orange LED
pinMode(PIN_ORANGE_LED, OUTPUT);
onLED();
// Buzzer
pinMode(PIN_BUZZER, OUTPUT);
digitalWrite(PIN_BUZZER, LOW);
// Soft Serial (receive data from the IR tranceiver module)
pinMode(PIN_SOFT_UART_RX, INPUT);
// Serial Ports
Serial.begin(9600);
softSerial.begin(9600);
}
void loop() {
const uint8_t numReceivedBytes = readSoftSerial();
if (numReceivedBytes > 0) {
beep();
toggleLED();
consumeAllSoftSerial();
}
delay(500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment