Arduino programming

I would like to add to this program that when the green light is on or when a pressure of 6 bars is detected, I can press the push button on pin 12. When I press the push button, the relay on pin 13 should only start working 3 seconds later and remain active for 6 seconds. Can someone help me?

const int pressureInput = A0; //select the analog input pin for the pressure transducer
const int pressureZero = 102.4; //analog reading of pressure transducer at 0psi
const int pressureMax = 921.6; //analog reading of pressure transducer at 200psi
const float pressuretransducermaxBAR = 13.79; //bar value of transducer being used
const int digitalOutput = 13; //select the digital output pin for activating the output
const int redLedPin = 4; //select the digital output pin for activating the red LED
const int greenLedPin = 2; //select the digital output pin for activating the green LED
const int orangeLedPin = 3;
const int baudRate = 9600; //constant integer to set the baud rate for serial monitor
const int sensorreadDelay = 250; //constant integer to set the sensor read delay in milliseconds

float previousPressureValue = 0; //variable to store the previous value coming from the pressure transducer
float pressureValue = 0; //variable to store the value coming from the pressure transducer

void setup() //setup routine, runs once when system turned on or reset
{
  Serial.begin(baudRate); //initializes serial communication at set baud rate bits per second
  pinMode(digitalOutput, OUTPUT); //set the digital output pin as an output
  pinMode(redLedPin, OUTPUT); //set the red LED pin as an output
  pinMode(greenLedPin, OUTPUT); //set the green LED pin as an output
}

void loop() //loop routine runs over and over again forever
{
  previousPressureValue = pressureValue; //save the current pressure value to previous pressure value 
  pressureValue = analogRead(pressureInput); //reads value from input pin and assigns to variable
  pressureValue = ((pressureValue-pressureZero)*pressuretransducermaxBAR)/(pressureMax-pressureZero); //conversion equation to convert analog reading to bar
  Serial.print(pressureValue, 2); //prints value from previous line to serial with 2 decimal places
  Serial.println("bar"); //prints label to serial
  
  if(pressureValue >= 6.0) {
   
    digitalWrite(greenLedPin, HIGH); // zet de groene LED aan
    digitalWrite(redLedPin, LOW); // zet de rode LED uit
  } else if(pressureValue <= 0.5) {
    
    digitalWrite(greenLedPin, LOW); // zet de groene LED uit
    digitalWrite(redLedPin, HIGH); // zet de rode LED aan
    } else if(pressureValue != previousPressureValue) {
      digitalWrite(orangeLedPin, HIGH); // Orange LED on
      delay(100);
      digitalWrite(orangeLedPin, LOW);
  } else {
    
    digitalWrite(greenLedPin, HIGH); // zet de groene LED aan
    digitalWrite(redLedPin, LOW); // zet de rode LED uit
  }
  
  delay(sensorreadDelay); //delay in milliseconds between read values

What have you tried so far ?

i tried this

 int buttonState = digitalRead(buttonPin);

  // Als de drukknop is ingedrukt, schakel het relais in
  if (buttonState == LOW) {
    startTimer = true;  // Set the timer to start
    delay(3000);        // Wait for 3 seconds
  }

  if (startTimer) {
    digitalWrite(relayPin, HIGH);  // Sets the uitvoerPin to a high state (turns on the output)
    delay(5000);                     // Pauses the program execution for 5 seconds
    digitalWrite(relayPin, LOW);   // Sets the uitvoerPin to a low state (turns off the output)
    startTimer = false;               // Reset the timer
  }
  else {
    digitalWrite(relayPin, LOW);
    Serial.println("Relais is uitgeschakeld");
  }

You should never compare floats with == or !=.
Chances for == are very small. Instead use:

const float epsilon = 0.0001;
if (abs(value-targetvalue) > epsilon) {
   Bla
}

You might need to include math.h to use abs().

If you want to monitor a pressure in time (and you use a compressor controlled by your arduino) you should not use delay(). Instead you need non blocking code. Good examples can be found here on the forum.
You cannot store a decimal in a int variable...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.