Skip to content

Instantly share code, notes, and snippets.

@johnwargo
Created November 1, 2023 11:59
Show Gist options
  • Save johnwargo/416b6ea38d8843a75ac7a5839386862c to your computer and use it in GitHub Desktop.
Save johnwargo/416b6ea38d8843a75ac7a5839386862c to your computer and use it in GitHub Desktop.
A single sketch that covers both sides of reading an Arduino pin state from another Arduino device/sketch
/**********************************************************
* Arduino to Arduino Pin Status
* Demonstration sketch
***********************************************************/
// =============================================================
// comment out the following line to build the receiver version
// of this sketch.
// =============================================================
#define SENDER
// =============================================================
#ifdef SENDER
#define PIN A0
// configure the HIGH/LOW delay time (all at once)
#define DELAY_VAL 2000
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println();
Serial.println("Configuring PIN for OUTPUT");
pinMode(PIN, OUTPUT);
}
void loop() {
Serial.println("Setting PIN state to HIGH");
digitalWrite(PIN, HIGH);
delay(DELAY_VAL);
Serial.println("Setting PIN state to LOW");
digitalWrite(PIN, LOW);
delay(DELAY_VAL);
}
#else
#define PIN A0
uint8_t prevState = LOW;
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println();
Serial.println("Configuring PIN for INPUT");
pinMode(PIN, INPUT);
}
void loop() {
uint8_t state = digitalRead(PIN);
if (prevState != state) {
prevState = state;
Serial.print("PIN status: ");
if (state == HIGH) {
Serial.println("HIGH");
} else {
Serial.println("LOW");
}
}
delay(25);
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment