So im new to ESP32 and its programming but very used to PLC control..
My question is how do you make multiple programs work on a single device or do you have to have separate ESP32s running in tandem? and if so what and how do you use to communicate with the screen.
I also want to reflect some control sin use just as simple green / amber circles or dots onto a screen using some axillary relays powered by something else, but maybe show motor running no control from the ESP32 just recognising its running and multiples of these like running stopped failed..
I tried the Button command but can only seem t make 1 button do something or to be more hohnest not raise a failure in the program.. am i using the wrong command..
I would like to read a weight from a device using load cells and then display say 6 motor status messages..possibly with temperature using a DHT22 included..
The temperature i have sorted but then trying to tye them together is not easy or so far impossible, the input from the GPIO i have no clue tried a few things but nothing so far
by typically invoking them ronud-robin from loop().
presumably each of these sub-functions is just monitoring some input and possibly updating some output. A timer can be used to do thing periodically without blocking other sub-functions
what is the "button command"? A button is typically just a switch wired between a pin and ground that pulls a pin LOW that is configured with an internal pull-up. Debouncing is typically required
things like weight and temperature don't change rapidly and can be monitored infrequently
start with one thing, written as a sub-function called from loop() and add more
The most important thing is to avoid delays and long loops. Than you can do as @gcjr suggested: call the functions one after the other in loop.
If you show one of your attempts we can tell you what's wrong.
the button thing is just a normally open contact derived from the pump control and the same would be for the stopped and failed, in my mind these are just normally open or normally closed devices depending on how you want the to react, I tried various methods then stumbled across button which in my mind are the same but reading through it also with these depends on how long they are pressed.. and im not really bothered about time in this instance, but how do you configure them i have had zero success and most examples show a button or contact being pressed then lighting an LED.. But expanding this to be possibly 6-8 GPIO inputs how is this done?? i have not seen an example of this as yet i can scale up.. Im a bit happier i can get the system to look at different times as you say so this is a bit better, any places i can look appreciated as examples
as mentioned, the pins can be configured with internal pullup resistors, pulling the pin HIGH. wiring the button between the pin and ground makes the pin LOW when the button is pressed
// check multiple buttons and toggle LEDs
enum { Off = HIGH, On = LOW };
byte pinsLed [] = { 10, 11, 12 };
byte pinsBut [] = { A1, A2, A3 };
#define N_BUT sizeof(pinsBut)
byte butState [N_BUT];
// -----------------------------------------------------------------------------
int
chkButtons ()
{
for (unsigned n = 0; n < sizeof(pinsBut); n++) {
byte but = digitalRead (pinsBut [n]);
if (butState [n] != but) {
butState [n] = but;
delay (10); // debounce
if (On == but)
return n;
}
}
return -1;
}
// -----------------------------------------------------------------------------
void
loop ()
{
switch (chkButtons ()) {
case 2:
digitalWrite (pinsLed [2], ! digitalRead (pinsLed [2]));
break;
case 1:
digitalWrite (pinsLed [1], ! digitalRead (pinsLed [1]));
break;
case 0:
digitalWrite (pinsLed [0], ! digitalRead (pinsLed [0]));
break;
}
}
// -----------------------------------------------------------------------------
void
setup ()
{
Serial.begin (9600);
for (unsigned n = 0; n < sizeof(pinsBut); n++) {
pinMode (pinsBut [n], INPUT_PULLUP);
butState [n] = digitalRead (pinsBut [n]);
}
for (unsigned n = 0; n < sizeof(pinsLed); n++) {
digitalWrite (pinsLed [n], Off);
pinMode (pinsLed [n], OUTPUT);
}
}
This looks simple enough let me take a look with these as always there is always more to it and its trying to capture that balance of i can do it 25 mins with a PLC but not cheaply, i will take a look at the wording and see what else i might need with this but thanks again for taking time to post
I guess i should have said i dont really need an LED to light the final piece would be for a green spot to change colour on a screen to give me the detail that is just feeding back the state.. thanks for taking a look
using digitalRead() in a loop it is possible to miss button presses in complex control systems
you could use interrupts, e.g.
// ESP32 external interrupt test pin 5 rising edge
#define RXPIN 5
volatile bool interrupt = false; // true on interrupt
// interrupt handler
void IRAM_ATTR handleInterrupt() {
interrupt = true;
}
void setup() {
Serial.begin(115200);
delay(3000);
Serial.println("\n\nESP32 external interrupt test on pin 5");
pinMode(RXPIN, INPUT_PULLUP);
attachInterrupt(digitalPinToGPIONumber(RXPIN), handleInterrupt, RISING);
}
void loop() {
// if had inperrupt on pin 5 display message
if (interrupt) {
interrupt = false;
Serial.println("interrupt on pin 5");
}
}
keep interrupt routines short avoiding complex code (in particular serial IO)
for complex operations set a flag (e.g. volatile bool interrupt in above code) in the interrupt routine which can be monitored in the main control loop
use hall effect switches to avoid the switch bounce associated with mechanical switches