Afternoon, it's been a long time since I programmed anything and took me a lot of ****ing around to get a working setup but now I have got everything working as intended. I work on a power plant and took on a project to create a test unit for a crane spreader (the part that picks up containers) which includes a touchscreen. Long story short it lets you toggle relays and displays feedback in the form of LEDs. Wanted to see if anyone could give me criticism for my code to make it function better or for it to perform better.
All that being said I have learnt a lot about the components involved and specifically the genieArduino library so if anyone is trying to get anything similar working or struggling to get something like communication between display and Arduino I'd be happy to try and help.
Code:
/* Control Sketch 2 This sketch is the first draft of the complete control and feedback When connected together this should allow full control of the relays and display of feedback */ #include <genieArduino.h> Genie genie; #define RESETLINE 4 // Declare all initial relay states bool relayStates[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // Set pins int relayPins[] = {22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37}; int feedbackPins[] = {40, 41, 42, 43, 44, 45, 46, 47}; // Variables for timing unsigned int buttonTimes[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; int index; void setup() { // put your setup code here, to run once: checkRelayBools(); // running this first so that the outputs are set to HIGH immediately to avoid sending any signals to spreader accidently // this is because when the outputs are set to output they will be LOW as default which would close all the relays for (int i = 0; i < 16; i++) { pinMode(relayPins[i], OUTPUT); // set all relay pins as OUTPUT //Serial.print(relayPins[i]); // use for debugging to see if for loop is correct size for array } for (int i = 0; i < 8; i++) { pinMode(feedbackPins[i], INPUT); // set all feedback pins as INPUT } Serial.begin(9600); // start communication @115200 baud rate on Serial0 (9600 BAUD WHILE CANNOT EDIT 4D PROJECT) genie.Begin(Serial); // tell genie instance to communicate on Serial0 (RX0 and TX0) genie.AttachEventHandler(genieEventHandler); // attach event handler pinMode(RESETLINE, OUTPUT); // Set D4 on Arduino to Output (4D Arduino Adaptor V2 - Display Reset) digitalWrite(RESETLINE, 1); // Reset the Display via D4 delay(100); digitalWrite(RESETLINE, 0); // unReset the Display via D4 // Increase to 4500 or 5000 if you have sync problems as your project gets larger. Can depent on microSD init speed. delay (3500); //let the display start up after the reset (This is important) } void loop() { // put your main code here, to run repeatedly: static unsigned int waitPeriod = millis(); genie.DoEvents(); // This calls the library each loop to process the queued responses from the display if (millis() >= waitPeriod) { //Serial.println("=== PERFORMING CHECKS ==="); checkRelayBools(); // check to see if relays need closing as a result of inputs // marked this out as it doesn't seem neccessary to call this every frame rather it seems better to call it when relay states are changed // put back in until a better solution to timing is found as otherwise relays will not self toggle checkFeedbackInputs(); waitPeriod = millis() + 200; // don't rerun code for 200ms just to save processing power } } void genieEventHandler(void) { genieFrame Event; genie.DequeueEvent(&Event); // Remove the next queued event from the buffer, and process it below if (Event.reportObject.cmd == GENIE_REPORT_EVENT) { //Serial.println("EVENT REPORTED"); if (Event.reportObject.object == GENIE_OBJ_4DBUTTON) { if (Event.reportObject.index < 14 && Event.reportObject.index >= 0) // FOR PRESS BUTTONS INDEXES 0-13 { index = Event.reportObject.index; // store received index locally //Serial.print("BUTTON INDEX "); //Serial.println(index); relayStates[index] = 1; // toggle boolean (toggle relay) buttonTimes[index] = millis(); } if (Event.reportObject.index == 14) // HOLD BUTTON { relayStates[14] = !relayStates[14]; } if (Event.reportObject.index == 15) // HOLD BUTTON { relayStates[15] = !relayStates[15]; } } } } void checkRelayBools() { for (int i = 0; i < 16; i++) { if (relayStates[i] == 1) { digitalWrite(relayPins[i], 0); if (millis() > buttonTimes[i] + 1000) // wait for a second then toggle the relay again { relayStates[i] = 0; } /*Serial.print("RELAY "); Serial.print(relayPins[i]); // DEBUGGING :: USES ALL BANDWITDH AND PREVENTS PROPER USE OF PROGRAM AT 9600 BAUD RATE Serial.println(" CLOSED");*/ } else { digitalWrite(relayPins[i], 1); /*Serial.print("RELAY "); Serial.print(relayPins[i]); // DEBUGGING :: USES ALL BANDWITDH AND PREVENTS PROPER USE OF PROGRAM AT 9600 BAUD RATE :/ Serial.println(" OPENED");*/ } } } void checkFeedbackInputs() { for (int i = 0; i < 8; i++) { if (digitalRead(feedbackPins[i]) == 1) { genie.WriteObject(GENIE_OBJ_LED, i, 1); //Serial.print("INPUT ON PIN "); //Serial.println(feedbackPins[i]); } else { genie.WriteObject(GENIE_OBJ_LED, i, 0); } } }
Comment