Morning All,
I'm new on the forum, but have been lurking for a while.
I am making a project where where the end goal is to have two arduinos communicate via RS485 half duplex, communicating the status of each others inputs, to operate some function on the other (LEDs, stepper motor, LCD, etc). Each arduino has a 20x4 LCD using I2C.
My own version of a remote IO of sorts. I find the concept of controlling something very far away over serial comms quite fascinating. The end goal being to control another project I'm working on with RS485 full duplex, and potentially over fibre optics.
I've experience with the arduinos before, but only simple functions, but including displaying the status of inputs on the LCD.
I'm looking for some pointers to compact the if statements I'll use to display the status of the digitalReads on the LCD. (see *** if statements to print condition of digital pin ***)
There are three variables:
- dig_in_pins_status[i]
- X position on LCD (0, 5, 10, 15)
- Y position on LCD (1, 2, 3)
Currently the plan is to create 16x if / else if statements to handle the 8x digital inputs, but I'm sure that this isn't the best way, and seems quite wasteful.
My thoughts were that there should be a way using if loops and or arrays to handle this, but it's beyond me at the moment. Any pointers are appreciated, thanks.
kit:
2x Arduino Unos (328Ps)
Thinkpad T480 running Linux mint
Arduino IDE 1.8.19.
//************* LIBS & DEFS *************
#include <LiquidCrystal_I2C.h> //add LCD I2C lib, change line 12 of LiquidCrystal_I2C.cpp from 1 to 0
#include <Wire.h> //add wire lib
LiquidCrystal_I2C lcd(0x27, 4, 20); //initiate LCD via I2C, address is normally 0x27 (or 0x3F), rows, columns
#define delay_time 1000 //set delay variable
const uint8_t dig_in_pins[] = {4,5,6,7,8,9,10,11}; //create digital input array
//keep pins 0 and 1 free for TX and RX
//keep pins 2 and 3 free for DE and RE
uint8_t dig_in_pins_status[sizeof(dig_in_pins)/sizeof(dig_in_pins[0])];
void setup() { //************* START SETUP *************
lcd.init(); //initialize lcd screen
lcd.backlight(); // turn on the backlight
// SET INPUT PINS - ANALOG
// keep pins A4 and A5 free for SDA and SCL
pinMode(A0, INPUT); //set A0 as input pin
// SET INPUT PINS - DIGITAL
//keep pins 0 and 1 free for TX and RX
for(uint8_t i = 0; i <= sizeof(dig_in_pins)/sizeof(dig_in_pins[0]); i++) pinMode(dig_in_pins[i], INPUT_PULLUP); // create DIs inputs
// SET OUTPUT PINS
pinMode(13, OUTPUT); //set pin 13 as output
digitalWrite(13, LOW);
} //************* END SETUP *************
void loop() { //************* START LOOP *************
digitalWrite(13, HIGH); //set pin 13 high
uint16_t pot_A = analogRead(A0); //read pin A0
for(uint8_t i = 0; i <= sizeof(dig_in_pins)/sizeof(dig_in_pins[0]); i++) {
dig_in_pins_status[i] = {digitalRead(dig_in_pins[i])}; // digitalread array
}
// LCD write
lcd.clear(); //clear lcd screen
// write pot value
lcd.setCursor(0,0); //set curser (x, y)
lcd.print("POT VAL="); //print text
lcd.setCursor(9,0); //set curser (x, y)
lcd.print(pot_A); //print pot value, 0-1023
// *** if statements to print condition of digital pin ***
if(dig_in_pins_status[0] == HIGH) {
lcd.setCursor(0,1);
lcd.print("1-ON ");
}
else if(dig_in_pins_status[0] == LOW){
lcd.setCursor(0,1);
lcd.print("1-OFF");
} //(close if loop 1.2)
// remaining if statements for dig_in_pins_status[1] through dig_in_pins_status[8] to go here
// Flash LED and delay
digitalWrite(13, LOW); //set pin 13 low
delay(delay_time); //delay
} //************* END LOOP *************
//************* END *************```



