Hello everyone. I wanted to build a program that was already on the forum with 2 additional functions ... why doesn't it work?
witam wszystkich , chciałem rozbudować program który był juz na forum o 2 dodatkowe funkcje... czemu nie działa?
int pomiarcisnienia;
void setup()
{ Serial.begin(9600); // prędkość portu COM do odczytu
pinMode(7, OUTPUT); //wskazanie pinu dla 1 przekaźnika przy butli 1
pinMode(8, OUTPUT); //wskazanie pinu dla 2 przekaźnika przy butli 2
digitalWrite(7, LOW);//wyłączenie 1 przekaźnika przy butli 1
digitalWrite(8, LOW);//wyłączenie 2 przekaźnika przy butli 2
}
void loop() {
pomiarcisnienia = analogRead(A0); // wskazanie pinu czytającego dane z czujnika cisnienia
Serial.print("Pomiar cisnienia = "); //napis dla monitora portu
Serial.println(pomiarcisnienia);
if (pomiarcisnienia > 143) { //przy jakim pomiarze ma nastąpić reakcja? 1024 to 5volt 240=1,17v 230=1,12v 225=1,1v 184=0,9v 143=0,7v
digitalWrite(7, HIGH);//włączenie przekaźnika przy butli 1
digitalWrite(8, HIGH); // włączenie przekaźnika przy butli 2
delay(200); // przekaźnik włączony prez 0,2 sekundy
digitalWrite(8, LOW); // wyłączenie przekaxnika 2 butli
delay(2000); // czas wyłaczenia przekaźnika 2 butli
if (pomiarcisnienia > 184) { //przy jakim pomiarze ma nastąpić reakcja?
digitalWrite(7, HIGH);//włączenie przekaźnika przy butli 1
digitalWrite(8, HIGH); // włączenie przekaźnika przy butli 2
delay(400); // przekaźnik włączony prez 0,4 sekundy
digitalWrite(8, LOW); // wyłączenie przekaxnika 2 butli
delay(2000); // czas wyłaczenia przekaźnika 2 butli
if (pomiarcisnienia > 225) { //przy jakim pomiarze ma nastaspić reakcja?
digitalWrite(7, HIGH);//włączenie przekaźnika przy butli 1
digitalWrite(8, HIGH);//włączenie przekaźnika przy butli 2
}
}
else {
digitalWrite(7, LOW);//wyłączenie przekaźnika przy butli 1
delay( 100);// opoxnienie wyłączzenia przekaźniaka nr 2
digitalWrite(8, LOW);//wyłączenie przekaźnika przy butli 2
}
delay(100); //co ile sekund ma być odczyt z czujnika?
}
ok the program should, depending on the voltage value from the analog sensor, change the valve opening time and the second valve closing time. The program now that reads the first voltage condition from the analog sensor. Disregards other conditions. condition 2, condition 3 - does not implement. valve 1 is to be open while the program is running. if below the first threshold it must be closed. If I remove the first condition - the program executes the second one without the third ... etc.
condition 1 :
if (pomiarcisnienia > 143)
Condition 2:
I'm on a small window and can't look closer, but it seems like you still have { braces } that are not in correct places.
Please post your latest code, and use the </> tool in the message composition window and paste your (auto-formatted) code where it stays to paste your code.
Alternately, you can use the IDE tool called "Copy for Forum" and just paste here.
so all your code is treated well and we can read it
and changing as little as possible, at least the logic. I did change the analog read target values.
Use the slide pot and see the logic of your program fail. Also see it succeed.
I haven't yet and may never draw a flowchart of your current sketch. I recommend you do that, the error will pop out.
If what you mean is having (#1), (#1 and #2) or ( #1 and #2 and #3) code blocks execute, you are close.
// https://forum.arduino.cc/t/extension-of-the-program-with-additional-2-parameters-does-not-work/1049873/8
int pressureMeasurement;
void setup ()
{Serial.begin (9600); // speed of the COM port to read
pinMode (7, OUTPUT); // pin indication for relay 1 at cylinder 1
pinMode (8, OUTPUT); // pin indication for relay 2 at cylinder 2
digitalWrite (7, LOW); // turn off 1 relay at cylinder 1
digitalWrite (8, LOW); // turn off relay 2 at cylinder 2
}
void loop () {
static unsigned int loopCounter;
delay (333); // slow this down crudely
pressureMeasurement = analogRead (A0); // indication of the pin reading the data from the pressure sensor
Serial.println();
Serial.print(loopCounter); loopCounter++;
Serial.print(" pressure = "); // string for the port monitor
Serial.println(pressureMeasurement);
if (pressureMeasurement> 128) {// what measurement should the reaction take place at? 1024 is 5volt 240 = 1.17v 230 = 1.12v 225 = 1.1v 184 = 0.9v 143 = 0.7v
Serial.println ("block 1");
digitalWrite (7, HIGH); // turn on the relay at cylinder 1
digitalWrite (8, HIGH); // turn on the relay at cylinder 2
delay (200); // the relay is turned on for 0.2 seconds
digitalWrite (8, LOW); // turn off the relay for 2 cylinders
delay (2000); // time when the relay for 2 cylinders is turned off
if (pressureMeasurement > 256) {// what measurement should the reaction take place at?
Serial.println ("block 2");
digitalWrite (7, HIGH); // turn on the relay at cylinder 1
digitalWrite (8, HIGH); // turn on the relay at cylinder 2
delay (400); // the relay is turned on for 0.4 seconds
digitalWrite (8, LOW); // turn off the relay for 2 cylinders
delay (2000); // time when the relay for 2 cylinders is turned off
if (pressureMeasurement > 768) {// at what measurement is to be reacted?
Serial.println ("block 3");
digitalWrite (7, HIGH); // turn on the relay at cylinder 1
digitalWrite (8, HIGH); // turn on the relay at cylinder 2
}
}
else {
Serial.println ("block X, analog below 128.");
digitalWrite (7, LOW); // turn off the relay at cylinder 1
delay (100); // delay for switching off the relay no.2
digitalWrite (8, LOW); // turn off the relay at cylinder 2
}
delay (100); // every how many seconds should the sensor read?
}
}
alto777.
Interesting concept, but unfortunately that was not the point. The program must execute blocks (# 1) or (# 2) or (# 3) - NOT (# 1), (# 1 and # 2) or (# 1 and # 2 and # 3) depending on the value of AnalogRead (A0 ). In your scetch - there is a serious problem - the program does not turn off the valve after a sudden drop in analogRead below "128" - the basic condition for turning off all valves. Do you have any idea?
I made some changes - at the moment the only thing the program does wrong - turns off valve 1 ignoring the AnalogRead value above the value set on block 1 and block 2
int pressureMeasurement;
void setup ()
{
Serial.begin (9600); // speed of the COM port to read
pinMode (7, OUTPUT); // pin indication for relay 1 at cylinder 1
pinMode (8, OUTPUT); // pin indication for relay 2 at cylinder 2
digitalWrite (7, LOW); // turn off 1 relay at cylinder 1
digitalWrite (8, LOW); // turn off relay 2 at cylinder 2
}
void loop () {
static unsigned int loopCounter;
delay (250); // slow this down crudely
pressureMeasurement = analogRead (A0); // indication of the pin reading the data from the pressure sensor
Serial.println();
Serial.print(loopCounter); loopCounter++;
Serial.print(" pressure = "); // string for the port monitor
Serial.println(pressureMeasurement);
if (pressureMeasurement > 143 && pressureMeasurement <= 184) { // what measurement should the reaction take place at? 1024 is 5volt 240 = 1.17v 230 = 1.12v 225 = 1.1v 184 = 0.9v 143 = 0.7v
Serial.println ("block 1");
digitalWrite (7, HIGH); // turn on the relay at cylinder 1
digitalWrite (8, HIGH); // turn on the relay at cylinder 2
delay (100); // the relay is turned on for 0.2 seconds
digitalWrite (8, LOW); // turn off the relay for 2 cylinders
delay (2000); // time when the relay for 2 cylinders is turned off
}
if (pressureMeasurement > 184 && pressureMeasurement <= 225) { // what measurement should the reaction take place at?
Serial.println ("block 2");
digitalWrite (7, HIGH); // turn on the relay at cylinder 1
digitalWrite (8, HIGH); // turn on the relay at cylinder 2
delay (400); // the relay is turned on for 0.4 seconds
digitalWrite (8, LOW); // turn off the relay for 2 cylinders
delay (2000); // time when the relay for 2 cylinders is turned off
}
if (pressureMeasurement > 225) {// at what measurement is to be reacted?
Serial.println ("block 3");
digitalWrite (7, HIGH); // turn on the relay at cylinder 1
digitalWrite (8, HIGH); // turn on the relay at cylinder 2
}
else {
Serial.println ("block X, analog below 143.");
digitalWrite (7, LOW); // turn off the relay at cylinder 1
delay (100); // delay for switching off the relay no.2
digitalWrite (8, LOW); // turn off the relay at cylinder 2
}
}
It's your sketch. It does what you wrote. I had no clue as to what you wanted it to actually do, and was not going to fix it for you to do what you want in any case, even if I could have plausibly guessed at your intention.
Use you finger to trace the code. It does exactly what you don't want.
1 ....Or.… 1 and 2 ....Or.… 1 and 2 and 3
It also has a flaw in that just above the low threshold it still arrives at the code that turns everything off.
So my recommendation is for you to learn more about how if/else statements work, and fix the structural issues (badly placed { braces }) and logical issues (if X > 143, it might also be > 287…) that make the code work the way it does.
Draw yourself a flowchart of what is happening in now in the wokwi by studying the code.
Draw yourself a flowchart of how you want your sketch to work.
Sry, I ignored overlooked your improved code, which uses logical operators to get you closer. Blame it on the tiny window and beach beverages.
I see this:
if (pressureMeasurement > 768) {
so the OFF part runs if the value is <= 768.
Change the else clause that does that into its own if tested section.
else {
Serial.println ("block X, analog below 143.");
digitalWrite (7, LOW); // turn off the relay at cylinder 1
delay (100); // delay for switching off the relay no.2
digitalWrite (8, LOW); // turn off the relay at cylinder 2
}
to keep that from happening:
if (pressureMeasurement < 143) {
Serial.println ("block X, analog below 143.");
digitalWrite (7, LOW); // turn off the relay at cylinder 1
delay (100); // delay for switching off the relay no.2
digitalWrite (8, LOW); // turn off the relay at cylinder 2
}
Also, I did crudely throttle the loop, as you had done with delay(100) at the bottom, just to make using the sketch more pleasant.
Use this model to make your loop run exactly at a certain freuency:
// delay (100); // slow this down crudely NO!
// slow this down using millis() timer model
# define THR 100 // run every 100 ms (if there is no blocking)
static unsigned long lastLoop;
if (millis() - lastLoop < THR)
return; // not time to run yet
lastLoop = millis();
Just stick that into loop() as the first instructions to be eecuted and the rest of the loop will run at 10 Hz. Change the time constant, change the frequency.
Of course your control code uses delay(), so I guess it is not terribly important to slow it down any special way. But if you are to go further, the control sections should be re-done as non-blocking code, which would return for your use a full speed loop and 99.44 percent of the processor resource which is time.