From 70adc5ff18af6579df9a5f80a08a7a308154e6e4 Mon Sep 17 00:00:00 2001 From: Alexander Gee Date: Wed, 5 Jun 2019 15:59:38 -0500 Subject: [PATCH 1/2] Refactored to reduce length. Refactored slightly to reduce the length and remove a bunch of unneeded temp variables. Hope this is helpful! --- part16/Remote016/Remote016.ino | 385 ++++++++++++++------------------- 1 file changed, 166 insertions(+), 219 deletions(-) diff --git a/part16/Remote016/Remote016.ino b/part16/Remote016/Remote016.ino index a9edb1a..5678bdf 100644 --- a/part16/Remote016/Remote016.ino +++ b/part16/Remote016/Remote016.ino @@ -1,220 +1,167 @@ -#include -#include -#include - -#include // Comes with Arduino IDE -#include - -// Set the pins on the I2C chip used for LCD connections (Some LCD use Address 0x27 and others use 0x3F): -LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display - -unsigned long previousMillis = 0; -const long interval = 20; - -int but1; -int but2; -int but3; -int but4; -int but5; - -int axis1; -int axis2; -int axis3; -int axis4; -int axis5; -int axis6; - -String count; - -struct SEND_DATA_STRUCTURE{ -//struct __attribute__((__packed__)) SEND_DATA_STRUCTURE{ - //put your variable definitions here for the data you want to send - //THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO - - int16_t menuDown; - int16_t Select; - int16_t menuUp; - int16_t toggleBottom; - int16_t toggleTop; - int16_t mode; - int16_t RLR; - int16_t RFB; - int16_t RT; - int16_t LLR; - int16_t LFB; - int16_t LT; -}; - -struct RECEIVE_DATA_STRUCTURE_REMOTE{ - //put your variable definitions here for the data you want to receive - //THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO - int16_t mode; - int16_t count; -}; - -int remoteFlag = 0; - -SEND_DATA_STRUCTURE mydata_send; -RECEIVE_DATA_STRUCTURE_REMOTE mydata_remote; - -RF24 radio(7, 8); // CE, CSN -const byte addresses[][6] = {"00001", "00002"}; - - -void setup() { - - lcd.init(); // initialize the lcd - lcd.backlight(); - - pinMode(38, INPUT_PULLUP); - pinMode(40, INPUT_PULLUP); - pinMode(42, INPUT_PULLUP); - pinMode(44, INPUT_PULLUP); - pinMode(46, INPUT_PULLUP); - - pinMode(A0, INPUT); - pinMode(A1, INPUT); - pinMode(A2, INPUT); - pinMode(A4, INPUT); - pinMode(A5, INPUT); - pinMode(A6, INPUT); - - radio.begin(); - radio.openWritingPipe(addresses[1]); // 00001 - radio.openReadingPipe(1, addresses[0]); // 00002 - radio.setPALevel(RF24_PA_MIN); - - lcd.begin(20,4); // Initialize the lcd for 20 chars 4 lines, turn on backlight - - lcd.setCursor(0,0); - lcd.print("openDog Remote "); - lcd.setCursor(0,1); - lcd.print("XRobots.co.uk "); - -} - -void loop() { - - unsigned long currentMillis = millis(); - if (remoteFlag == 0 && currentMillis - previousMillis >= 5) { - - but1 = digitalRead(38); - but2 = digitalRead(40); - but3 = digitalRead(42); - but4 = digitalRead(44); - but5 = digitalRead(46); - - if (but1 == 0) { - mydata_send.menuDown = 1; - } - else { - mydata_send.menuDown = 0; - } - - if (but2 == 0) { - mydata_send.Select = 1; - } - else { - mydata_send.Select = 0; - } - - if (but3 == 0) { - mydata_send.menuUp = 1; - } - else { - mydata_send.menuUp = 0; - } - - if (but4 == 0) { - mydata_send.toggleBottom = 1; - } - else { - mydata_send.toggleBottom = 0; - } - - if (but5 == 0) { - mydata_send.toggleTop = 1; - } - else { - mydata_send.toggleTop = 0; - } - - axis1 = analogRead(A0); - axis2 = analogRead(A1); - axis3 = analogRead(A2); - axis4 = analogRead(A3); - axis5 = analogRead(A4); - axis6 = analogRead(A5); - - mydata_send.RLR = axis1; - mydata_send.RFB = axis2; - mydata_send.RT = axis3; - mydata_send.LLR = axis4; - mydata_send.LFB = axis5; - mydata_send.LT = axis6; - - //delay(5); - //radio.startListening(); - //radio.read(&mydata_remote, sizeof(RECEIVE_DATA_STRUCTURE_REMOTE)); - - count = String(mydata_remote.count); - - - /* - - lcd.setCursor(0,3); - lcd.print(count); - - if (mydata_remote.mode == 0) { - lcd.setCursor(0,0); - lcd.print("Mode 0 - Safe "); - lcd.setCursor(0,1); - lcd.print(" "); - } - else if (mydata_remote.mode == 1) { - lcd.setCursor(0,0); - lcd.print("Mode 1 - Kin Test "); - lcd.setCursor(0,1); - lcd.print(" "); - } - else if (mydata_remote.mode == 2) { - lcd.setCursor(0,0); - lcd.print("Mode 2 - "); - lcd.setCursor(0,1); - lcd.print(" "); - } - else if (mydata_remote.mode == 3) { - lcd.setCursor(0,0); - lcd.print("Mode 3 - "); - lcd.setCursor(0,1); - lcd.print(" "); - } - else if (mydata_remote.mode == 4) { - lcd.setCursor(0,0); - lcd.print("Mode 4 - "); - lcd.setCursor(0,1); - lcd.print(" "); - } - else if (mydata_remote.mode == 5) { - lcd.setCursor(0,0); - lcd.print("Mode 5 - "); - lcd.setCursor(0,1); - lcd.print(" "); - } - - */ - - radio.stopListening(); - radio.write(&mydata_send, sizeof(SEND_DATA_STRUCTURE)); - - previousMillis = currentMillis; - } - - - - - - - - } // end of main loop +#include +#include +#include +#include // Comes with Arduino IDE +#include + +// Set the pins on the I2C chip used for LCD connections (Some LCD use Address 0x27 and others use 0x3F): +LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display + +unsigned long previousMillis = 0; // The timestamp at which we last sent a radio packet. +const long interval = 20; + +const int BUTTON1_PIN = 38; +const int BUTTON2_PIN = 40; +const int BUTTON3_PIN = 42; +const int BUTTON4_PIN = 44; +const int BUTTON5_PIN = 46; + +const int AXIS1_PIN = A0; +const int AXIS2_PIN = A1; +const int AXIS3_PIN = A2; +const int AXIS4_PIN = A4; +const int AXIS5_PIN = A5; +const int AXIS6_PIN = A6; + +String count; + +struct SEND_DATA_STRUCTURE{ +//struct __attribute__((__packed__)) SEND_DATA_STRUCTURE{ + //put your variable definitions here for the data you want to send + //THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO + + int16_t menuDown; + int16_t Select; + int16_t menuUp; + int16_t toggleBottom; + int16_t toggleTop; + int16_t mode; + int16_t RLR; + int16_t RFB; + int16_t RT; + int16_t LLR; + int16_t LFB; + int16_t LT; +}; + +struct RECEIVE_DATA_STRUCTURE_REMOTE{ + //put your variable definitions here for the data you want to receive + //THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO + int16_t mode; + int16_t count; +}; + +int remoteFlag = 0; + +SEND_DATA_STRUCTURE mydata_send; +RECEIVE_DATA_STRUCTURE_REMOTE mydata_remote; + +RF24 radio(7, 8); // CE, CSN +const byte addresses[][6] = {"00001", "00002"}; + + +void setup() { + lcd.init(); + lcd.backlight(); + + pinMode(BUTTON1_PIN, INPUT_PULLUP); + pinMode(BUTTON2_PIN, INPUT_PULLUP); + pinMode(BUTTON3_PIN, INPUT_PULLUP); + pinMode(BUTTON4_PIN, INPUT_PULLUP); + pinMode(BUTTON5_PIN, INPUT_PULLUP); + + pinMode(AXIS1_PIN, INPUT); + pinMode(AXIS2_PIN, INPUT); + pinMode(AXIS3_PIN, INPUT); + pinMode(AXIS4_PIN, INPUT); + pinMode(AXIS5_PIN, INPUT); + pinMode(AXIS6_PIN, INPUT); + + radio.begin(); + radio.openWritingPipe(addresses[1]); // 00001 + radio.openReadingPipe(1, addresses[0]); // 00002 + radio.setPALevel(RF24_PA_MIN); + + lcd.begin(20,4); // Initialize the lcd for 20 chars 4 lines, turn on backlight + + lcd.setCursor(0,0); + lcd.print("openDog Remote "); + lcd.setCursor(0,1); + lcd.print("XRobots.co.uk "); + +} + +void loop() { + unsigned long currentMillis = millis(); + if (remoteFlag == 0 && currentMillis - previousMillis >= 5) { + + mydata_send.menuDown = !digitalRead(BUTTON1_PIN); + mydata_send.Select = !digitalRead(BUTTON2_PIN); + mydata_send.menuUp = !digitalRead(BUTTON3_PIN); + mydata_send.toggleBottom = !digitalRead(BUTTON4_PIN); + mydata_send.toggleTop = !digitalRead(BUTTON5_PIN); + + mydata_send.RLR = analogRead(AXIS1_PIN); + mydata_send.RFB = analogRead(AXIS2_PIN); + mydata_send.RT = analogRead(AXIS3_PIN); + mydata_send.LLR = analogRead(AXIS4_PIN); + mydata_send.LFB = analogRead(AXIS5_PIN); + mydata_send.LT = analogRead(AXIS6_PIN); + + //delay(5); + //radio.startListening(); + //radio.read(&mydata_remote, sizeof(RECEIVE_DATA_STRUCTURE_REMOTE)); + + count = String(mydata_remote.count); + + /* + + lcd.setCursor(0,3); + lcd.print(count); + + if (mydata_remote.mode == 0) { + lcd.setCursor(0,0); + lcd.print("Mode 0 - Safe "); + lcd.setCursor(0,1); + lcd.print(" "); + } + else if (mydata_remote.mode == 1) { + lcd.setCursor(0,0); + lcd.print("Mode 1 - Kin Test "); + lcd.setCursor(0,1); + lcd.print(" "); + } + else if (mydata_remote.mode == 2) { + lcd.setCursor(0,0); + lcd.print("Mode 2 - "); + lcd.setCursor(0,1); + lcd.print(" "); + } + else if (mydata_remote.mode == 3) { + lcd.setCursor(0,0); + lcd.print("Mode 3 - "); + lcd.setCursor(0,1); + lcd.print(" "); + } + else if (mydata_remote.mode == 4) { + lcd.setCursor(0,0); + lcd.print("Mode 4 - "); + lcd.setCursor(0,1); + lcd.print(" "); + } + else if (mydata_remote.mode == 5) { + lcd.setCursor(0,0); + lcd.print("Mode 5 - "); + lcd.setCursor(0,1); + lcd.print(" "); + } + + */ + + radio.stopListening(); + radio.write(&mydata_send, sizeof(SEND_DATA_STRUCTURE)); + + previousMillis = currentMillis; + } +} // end of main loop From 337289e2e0670574a2ede8984acfffc5c46dcae7 Mon Sep 17 00:00:00 2001 From: Alexander Gee Date: Wed, 5 Jun 2019 16:07:20 -0500 Subject: [PATCH 2/2] Update Remote016.ino --- part16/Remote016/Remote016.ino | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/part16/Remote016/Remote016.ino b/part16/Remote016/Remote016.ino index 5678bdf..2d6bbfa 100644 --- a/part16/Remote016/Remote016.ino +++ b/part16/Remote016/Remote016.ino @@ -20,9 +20,9 @@ const int BUTTON5_PIN = 46; const int AXIS1_PIN = A0; const int AXIS2_PIN = A1; const int AXIS3_PIN = A2; -const int AXIS4_PIN = A4; -const int AXIS5_PIN = A5; -const int AXIS6_PIN = A6; +const int AXIS4_PIN = A3; +const int AXIS5_PIN = A4; +const int AXIS6_PIN = A5; String count;