Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Scratch for Arduino Uno Q
# Scratch for Arduino Uno Q

## Installation

Expand All @@ -13,5 +13,6 @@ curl -sSL https://raw.githubusercontent.com/dido18/scratch-arduino-app/main/inst
### Local development
- `task scratch:init`
- `task scratch:local:start`
- change the `const wsServerURL = `<YOUR_IP>:7000`;` in the `index.js`
- Open local scratch on http://localhost:8601/
- `ŧask board:upload`
- change the `const wsServerURL = `ws://<YOUR_IP>:7000`;` in the `index.js`
- Open local scratch on http://localhost:8601/
21 changes: 20 additions & 1 deletion python/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,30 @@ def on_matrix_draw(_, data):
print(f"Transformed frame to draw on 8x13 matrix: {frame_8x13}")
Bridge.call("matrix_draw", frame_8x13)

def rgb_to_digital(value, threshold=128) -> bool:
"""Convert RGB value (0-255) to digital HIGH(1) or LOW(0)"""
return value >= threshold

def on_set_led_rgb(_, data):
led = data.get("led")
r = data.get("r")
g = data.get("g")
b = data.get("b")

# Convert RGB values (0-255) to digital HIGH/LOW
r_digital = rgb_to_digital(r)
g_digital = rgb_to_digital(g)
b_digital = rgb_to_digital(b)

print(f"Setting LED {led} to color: RGB({r},{g},{b}) -> Digital({r_digital},{g_digital},{b_digital})")
Bridge.call("set_led_rgb", led, r_digital, g_digital, b_digital)

ui.on_message("matrix_draw", on_matrix_draw)
ui.on_message("set_led_rgb", on_set_led_rgb)

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

Bridge.provide("modulino_button_pressed", on_modulino_button_pressed)

App.run()
App.run()
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,30 @@ ArduinoBasics.prototype.getInfo = function () {
defaultValue: '0101010101100010101000100'
}
}
},
{
opcode: 'setLed3',
blockType: BlockType.COMMAND,
text: 'set LED 3 to [HEX]',
func: 'setLed3',
arguments: {
HEX: {
type: ArgumentType.COLOR,
defaultValue: '#ff0000'
}
}
},
{
opcode: 'setLed4',
blockType: BlockType.COMMAND,
text: 'set LED 4 to [HEX]',
func: 'setLed4',
arguments: {
HEX: {
type: ArgumentType.COLOR,
defaultValue: '#0000ff'
}
}
}
]
};
Expand All @@ -59,4 +83,26 @@ ArduinoBasics.prototype.matrixDraw = function (args) {
this.io.emit("matrix_draw", { frame: args.FRAME });
};

ArduinoBasics.prototype.setLed3 = function (args) {
const hexColor = args.HEX;
const rgb = this.hexToRgb(hexColor);
console.log(`Setting led 3 to: r:${rgb.r}, g:${rgb.g}, b:${rgb.b} (HEX: ${hexColor})`);
this.io.emit("set_led_rgb", {led: "LED3", r: rgb.r, g: rgb.g,b: rgb.b});
};

ArduinoBasics.prototype.setLed4 = function (args) {
const hexColor = args.HEX;
const rgb = this.hexToRgb(hexColor);
console.log(`Setting led 4 to: r:${rgb.r}, g:${rgb.g}, b:${rgb.b} (HEX: ${hexColor})`);
this.io.emit("set_led_rgb", {led: "LED4", r: rgb.r, g: rgb.g,b: rgb.b});
};

ArduinoBasics.prototype.hexToRgb = function (hex) {
hex = hex.replace('#', '');
const r = parseInt(hex.substring(0, 2), 16);
const g = parseInt(hex.substring(2, 4), 16);
const b = parseInt(hex.substring(4, 6), 16);
return { r, g, b };
};

module.exports = ArduinoBasics;
29 changes: 27 additions & 2 deletions sketch/sketch.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,24 @@ void setup() {
Bridge.begin();
Modulino.begin(Wire1);
// show led indication if buttons cannot be initilized
buttons.begin();
buttons.begin();
pinMode(LED_BUILTIN, OUTPUT);
pinMode(LED_BUILTIN + 1, OUTPUT);
pinMode(LED_BUILTIN + 2, OUTPUT);
pinMode(LED_BUILTIN + 3, OUTPUT);
pinMode(LED_BUILTIN + 4, OUTPUT);
pinMode(LED_BUILTIN + 5, OUTPUT);

digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(LED_BUILTIN + 1, HIGH);
digitalWrite(LED_BUILTIN + 2, HIGH);
digitalWrite(LED_BUILTIN + 3, HIGH);
digitalWrite(LED_BUILTIN + 4, HIGH);
digitalWrite(LED_BUILTIN + 5, HIGH);

buttons.setLeds(true, true, true);
Bridge.provide("matrix_draw", matrix_draw);
Bridge.provide("set_led_rgb", set_led_rgb);
}

void loop() {
Expand Down Expand Up @@ -47,4 +62,14 @@ void matrix_draw(String frame){
matrix.draw(shades);
}


void set_led_rgb(String pin, uint8_t r, uint8_t g, uint8_t b) {
if (pin == "LED3") {
digitalWrite(LED_BUILTIN, r ? LOW : HIGH );
digitalWrite(LED_BUILTIN + 1, g ? LOW : HIGH );
digitalWrite(LED_BUILTIN + 2, b ? LOW: HIGH );
} else if (pin == "LED4") {
digitalWrite(LED_BUILTIN + 3, r ? LOW : HIGH );
digitalWrite(LED_BUILTIN + 4, g ? LOW : HIGH );
digitalWrite(LED_BUILTIN + 5, b ? LOW : HIGH );
}
}
Loading