Environment:Arduino App Lab (Python + MCU sketch in one app)
Goal:
I want the LED to turn on when the switch is pressed and turn off when the switch is released. I also want to read the data from pin D8, where my switch is connected, and send that value to the Linux side using a Bridge function so I can print it in the Python console.
MCU (C++ / sketch.ino)
Reads D8 with INPUT_PULLUP
Turns the LED ON while the switch is pressed (and OFF when released)
Exposes a read_d8() function via the Router Bridge so Python can fetch the value
void set_led_state(bool state) {
// LOW state means LED is ON
digitalWrite(LED_BUILTIN, state ? LOW : HIGH);
}`
main.py
`from arduino.app_utils import *
import time import sleep
led_state = False
last = None
def loop():
global last, led_state
v = Bridge.call("read_d8")
if v is not None and v != last:
print("D8 =", v)
last = v
led_state = not led_state
Bridge.call("set_led_state", led_state)
sleep(1)
Have you tried your app of post #1? Is it working? Is it the LED_BUILTIN are you talikng? And always use code tags <code/> when posting codes:
Sketch.ino
#include "Arduino_RouterBridge.h"
const uint8_t SWITCH_PIN = 8;
int read_d8();
void set_led_state(bool state);
void setup() {
pinMode(SWITCH_PIN, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
Bridge.begin();
Bridge.provide("read_d8", read_d8);
Bridge.provide("set_led_state", set_led_state);
}
void loop() {
}
int read_d8() {
return digitalRead(SWITCH_PIN);
}
void set_led_state(bool state) {
// LOW state means LED is ON
digitalWrite(LED_BUILTIN, state ? LOW : HIGH);
}
main.py
from arduino.app_utils import *
import time import sleep
led_state = False
last = None
def loop():
global last, led_state
v = Bridge.call("read_d8")
if v is not None and v != last:
print("D8 =", v)
last = v
led_state = not led_state
Bridge.call("set_led_state", led_state)
sleep(1)
App.run(user_loop=loop)
Notes: 1. In Python, indentation and spacing are part of the language syntax. Please ensure that the above main.py script follows correct indentation and spacing, then re-submit it. Remember that a colon (:) marks the start of a block, and the lines belonging to that block must be indented consistently on the following lines.
2.
From your python script, it appears that --
You press switch at the MCU side
The information is known to MPU via Bridge
MPU sends "turn On" signal to MCU via Bridge
If I am corect, then the above quoted line shoud be modified as: Turns the LED ON while the switch is pressed (and OFF when released) and let it be known to MPU which sends the "turn On/Off" value to MCU.
3. Add codes with python script so that when MCU driven LED_BUILTIN is On, the MPU driven LED1_R is also On.