Skip to content

Commit e9a388b

Browse files
authored
feat: add commands to set the color to the rgb LED3 and LED4 (#18)
* feat: add LED control functionality and update documentation * feat: implement RGB to digital conversion for LED control and enhance setup process * refactor: remove debug print statement from modulino button handler * fix: update WebSocket server URL to use dynamic protocol and hostname * fix: update default HEX color value from red to blue in ArduinoBasics extension
1 parent f1785b1 commit e9a388b

File tree

4 files changed

+97
-6
lines changed

4 files changed

+97
-6
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Scratch for Arduino Uno Q
1+
# Scratch for Arduino Uno Q
22

33
## Installation
44

@@ -13,5 +13,6 @@ curl -sSL https://raw.githubusercontent.com/dido18/scratch-arduino-app/main/inst
1313
### Local development
1414
- `task scratch:init`
1515
- `task scratch:local:start`
16-
- change the `const wsServerURL = `<YOUR_IP>:7000`;` in the `index.js`
17-
- Open local scratch on http://localhost:8601/
16+
- `ŧask board:upload`
17+
- change the `const wsServerURL = `ws://<YOUR_IP>:7000`;` in the `index.js`
18+
- Open local scratch on http://localhost:8601/

python/main.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,30 @@ def on_matrix_draw(_, data):
2626
print(f"Transformed frame to draw on 8x13 matrix: {frame_8x13}")
2727
Bridge.call("matrix_draw", frame_8x13)
2828

29+
def rgb_to_digital(value, threshold=128) -> bool:
30+
"""Convert RGB value (0-255) to digital HIGH(1) or LOW(0)"""
31+
return value >= threshold
32+
33+
def on_set_led_rgb(_, data):
34+
led = data.get("led")
35+
r = data.get("r")
36+
g = data.get("g")
37+
b = data.get("b")
38+
39+
# Convert RGB values (0-255) to digital HIGH/LOW
40+
r_digital = rgb_to_digital(r)
41+
g_digital = rgb_to_digital(g)
42+
b_digital = rgb_to_digital(b)
43+
44+
print(f"Setting LED {led} to color: RGB({r},{g},{b}) -> Digital({r_digital},{g_digital},{b_digital})")
45+
Bridge.call("set_led_rgb", led, r_digital, g_digital, b_digital)
46+
2947
ui.on_message("matrix_draw", on_matrix_draw)
48+
ui.on_message("set_led_rgb", on_set_led_rgb)
3049

3150
def on_modulino_button_pressed(btn):
3251
ui.send_message('modulino_buttons_pressed', {"btn": btn})
3352

3453
Bridge.provide("modulino_button_pressed", on_modulino_button_pressed)
3554

36-
App.run()
55+
App.run()

scratch-arduino-extensions/packages/scratch-vm/src/extensions/arduino_basics/index.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,30 @@ ArduinoBasics.prototype.getInfo = function () {
4949
defaultValue: '0101010101100010101000100'
5050
}
5151
}
52+
},
53+
{
54+
opcode: 'setLed3',
55+
blockType: BlockType.COMMAND,
56+
text: 'set LED 3 to [HEX]',
57+
func: 'setLed3',
58+
arguments: {
59+
HEX: {
60+
type: ArgumentType.COLOR,
61+
defaultValue: '#ff0000'
62+
}
63+
}
64+
},
65+
{
66+
opcode: 'setLed4',
67+
blockType: BlockType.COMMAND,
68+
text: 'set LED 4 to [HEX]',
69+
func: 'setLed4',
70+
arguments: {
71+
HEX: {
72+
type: ArgumentType.COLOR,
73+
defaultValue: '#0000ff'
74+
}
75+
}
5276
}
5377
]
5478
};
@@ -59,4 +83,26 @@ ArduinoBasics.prototype.matrixDraw = function (args) {
5983
this.io.emit("matrix_draw", { frame: args.FRAME });
6084
};
6185

86+
ArduinoBasics.prototype.setLed3 = function (args) {
87+
const hexColor = args.HEX;
88+
const rgb = this.hexToRgb(hexColor);
89+
console.log(`Setting led 3 to: r:${rgb.r}, g:${rgb.g}, b:${rgb.b} (HEX: ${hexColor})`);
90+
this.io.emit("set_led_rgb", {led: "LED3", r: rgb.r, g: rgb.g,b: rgb.b});
91+
};
92+
93+
ArduinoBasics.prototype.setLed4 = function (args) {
94+
const hexColor = args.HEX;
95+
const rgb = this.hexToRgb(hexColor);
96+
console.log(`Setting led 4 to: r:${rgb.r}, g:${rgb.g}, b:${rgb.b} (HEX: ${hexColor})`);
97+
this.io.emit("set_led_rgb", {led: "LED4", r: rgb.r, g: rgb.g,b: rgb.b});
98+
};
99+
100+
ArduinoBasics.prototype.hexToRgb = function (hex) {
101+
hex = hex.replace('#', '');
102+
const r = parseInt(hex.substring(0, 2), 16);
103+
const g = parseInt(hex.substring(2, 4), 16);
104+
const b = parseInt(hex.substring(4, 6), 16);
105+
return { r, g, b };
106+
};
107+
62108
module.exports = ArduinoBasics;

sketch/sketch.ino

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,24 @@ void setup() {
1010
Bridge.begin();
1111
Modulino.begin(Wire1);
1212
// show led indication if buttons cannot be initilized
13-
buttons.begin();
13+
buttons.begin();
14+
pinMode(LED_BUILTIN, OUTPUT);
15+
pinMode(LED_BUILTIN + 1, OUTPUT);
16+
pinMode(LED_BUILTIN + 2, OUTPUT);
17+
pinMode(LED_BUILTIN + 3, OUTPUT);
18+
pinMode(LED_BUILTIN + 4, OUTPUT);
19+
pinMode(LED_BUILTIN + 5, OUTPUT);
20+
21+
digitalWrite(LED_BUILTIN, HIGH);
22+
digitalWrite(LED_BUILTIN + 1, HIGH);
23+
digitalWrite(LED_BUILTIN + 2, HIGH);
24+
digitalWrite(LED_BUILTIN + 3, HIGH);
25+
digitalWrite(LED_BUILTIN + 4, HIGH);
26+
digitalWrite(LED_BUILTIN + 5, HIGH);
27+
1428
buttons.setLeds(true, true, true);
1529
Bridge.provide("matrix_draw", matrix_draw);
30+
Bridge.provide("set_led_rgb", set_led_rgb);
1631
}
1732

1833
void loop() {
@@ -47,4 +62,14 @@ void matrix_draw(String frame){
4762
matrix.draw(shades);
4863
}
4964

50-
65+
void set_led_rgb(String pin, uint8_t r, uint8_t g, uint8_t b) {
66+
if (pin == "LED3") {
67+
digitalWrite(LED_BUILTIN, r ? LOW : HIGH );
68+
digitalWrite(LED_BUILTIN + 1, g ? LOW : HIGH );
69+
digitalWrite(LED_BUILTIN + 2, b ? LOW: HIGH );
70+
} else if (pin == "LED4") {
71+
digitalWrite(LED_BUILTIN + 3, r ? LOW : HIGH );
72+
digitalWrite(LED_BUILTIN + 4, g ? LOW : HIGH );
73+
digitalWrite(LED_BUILTIN + 5, b ? LOW : HIGH );
74+
}
75+
}

0 commit comments

Comments
 (0)