From 6ca042f49e24fcb4aa28586664acd5bd53adc7e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Hozda?= Date: Fri, 3 Jan 2020 16:54:58 +0100 Subject: [PATCH] Reduce block size to 8b --- src/rx.ino | 25 ++++++++----------------- src/tx.ino | 16 ++++++++-------- 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/src/rx.ino b/src/rx.ino index 75424b7..7fb3257 100644 --- a/src/rx.ino +++ b/src/rx.ino @@ -16,25 +16,16 @@ void setup() void loop() { - uint8_t buf[4]; + uint8_t buf[2]; + size_t buf_len = sizeof(buf); // rip - if (driver.recv(buf, &4)){ - int buffer = atoi((char*)buf); - int channel = buffer%10; - int value = buffer/10; + if (driver.recv(buf, &buf_len)){ + uint8_t value = buf[1]; + uint8_t ch = [buf]2; - switch(channel){ - case 0: - channel = 3; - break; - case 1: - channel = 5; - break; - case 2: - channel = 6; - break; - } + // 0 -> 3; 1 -> 5; 2 -> 6 + ch = (ch ? ++ch + 3 : 3); - analogWrite(channel, value); + analogWrite(ch, value); } } diff --git a/src/tx.ino b/src/tx.ino index 299aae4..38e6caa 100644 --- a/src/tx.ino +++ b/src/tx.ino @@ -4,7 +4,7 @@ RH_ASK driver(2000); -int lastval[] = {0,0,0}; +uint8_t lastval[] = {0,0,0}; void setup() { @@ -14,17 +14,17 @@ void setup() void loop() { - - for(int i=0; i < 3; i++){ - int val = analogRead(i)/4; + for(uint8_t i = 0; i < 3; i++) { + uint8_t val = (uint8_t)analogRead(i)/4; if(val >= lastval[i]+3 || val <= lastval[i]-3){ - char msg[4]; - lastval[i] = val; - sprintf(msg, "%d%d", val, i); + uint8_t msg[2] = { val, i }; + Serial.println(msg); - driver.send((uint8_t *)msg, 4); + driver.send((uint8_t *)msg, 2); driver.waitPacketSent(); + + lastval[i] = val; } }