need help with code

I'm not sure how to ask this question so ill try to explain the best i can. i have a pump to run a high pressure spray nozzle. after the pump shuts off i need to activate a relay to open a drain solenoid so i can instantly drop the pressure in the line. When the pump needs to run i set a pin low and when its off a pullup resistor keeps the pin high. When the pump shuts off i need to run another relay but only for a few seconds so it can drain the line. When i hit the button pin goes low pumps on. Then when i let off the button the pins high and drain relay instantly turns on but only for a few seconds. hope you can understand lol thanks

also i am already using millis() as a timer in my code

I think that I understand what you want to do. Basically, when the spray pin becomes HIGH run the drain pump for a few seconds. The important thing here is to be able to detect when the pin becomes HIGH rather than when it is HIGH. To do this you need to note the state of the pin each time through loop() and act when it changes from LOW to HIGH. The StateChangeDetection example in the IDE shows how to do this.

So, having detected the change what do you do ? The smart way to time the drain pump run is to turn it on when you detect the change of state and save the value of millis() at that time. Then, each time through loop(), subtract the start time from millis() and test whether the required number if milliseconds of pumping time has passed. If so, turn off the pump.

i need to note that my "button" is manual meaning its all grounded to the arduino but its manually pulls the relay pins low. the arudino can operate the relay too. im trying to think how the state detection would work with the way the dht sensors readings are setup in my program. im really new at this so im not sure what can be done about all of this

how could i read the sensors values with millis and not delay so that im not holding up the loop for 3 seconds. thanks

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

i would rather just work on the code issue. im not having any issues with the circuit right now.

Hi,

i need to note that my "button" is manual meaning its all grounded to the arduino but its manually pulls the relay pins low. the arudino can operate the relay too. im trying to think how the state detection would work with the way the dht sensors readings are setup in my program. im really new at this so im not sure what can be done about all of this

Can you tell us your electronics, programming, Arduino, hardware experience?

A circuit diagram will help with I/O organisation of your code.

Do you have a transistor or MOSFET driving your relay?

Tom... :slight_smile:

can someone give me a piece of working code as an example.

const byte buttonPin = A1;
const byte ledPin = 13;
unsigned long startTime;
unsigned long currentTime;
unsigned long period = 5000;
byte previousButtonState;
byte currentButtonState;

void setup()
{
  Serial.begin(115200);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  currentTime = millis();
  previousButtonState = currentButtonState;
  currentButtonState = digitalRead(buttonPin);
  
  if (currentButtonState != previousButtonState && currentButtonState == HIGH)
  {
    startTime = millis();
    digitalWrite(ledPin, HIGH);
  }
  
  if (currentTime - startTime >= period)
  {
    digitalWrite(ledPin, LOW);
  }
}

Adjust the HIGH/LOW logic to suit your requirements

relays have 10kohm pullup resistors and when i write the pin low of the arudino it activates the relay. all the io information is right there in the code as far as whats an input and a output. everything works fine except im trying to add this solenoid into the picture. the arduino runs off a 5v power supply do the relays. all of the digital pins are tied to the dht22 sensors. when i made my paintball gun auto circuit i used button state change as mode select. im trying to picture how i would use button state change to activate this relay. i really dont want to use the arduino as the input for the manual on buttons unless i have to.

thankyou UKHeliBob that should get me started. i just wish i understood the syntax better but im pretty new to this and have not done a lot of projects. What do you think about the 300ms delay between all the sensor readings i think they might cause some trouble. is there any way around waiting for all the delays before it gets back to that part of the code

i just wish i understood the syntax better

Here is the program with some comments added

const byte buttonPin = A1;  //const because they will never change when the program runs
const byte ledPin = 13;
unsigned long startTime;    //these are the timing variables
unsigned long currentTime;  //declared as unsigned long to prevent problems when millis() rolls over to zero
unsigned long period = 5000;    //some of them could be declared local to loop() but I chose to make them global
byte previousButtonState;  //variables to hold the state of the button input
byte currentButtonState;

void setup()
{
  Serial.begin(115200);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  currentTime = millis();  //get current millis() value each time through loop()
  previousButtonState = currentButtonState;  //save the previous state of the button input
  currentButtonState = digitalRead(buttonPin);  //get the current state of the button input
  
  if (currentButtonState != previousButtonState && currentButtonState == HIGH)  //if the state has changed and is now HIGH
  {                                                                             //the button has just been pressed
    startTime = millis();  //save the millis() value of when the button became pressed
    digitalWrite(ledPin, HIGH);  //turn on the LED (for example)
  }
  
  if (currentTime - startTime >= period)  //if the period has elepsed
  {                  
    digitalWrite(ledPin, LOW);  //turn off the LED
  }
}

What do you think about the 300ms delay between all the sensor readings

Why are they there ? If you want to slow down the display then print all of them with no delays between them then have one delay. If your pump is going to be on for a few seconds then a small delay() will not cause a problem.

You can eliminate the delay() by using the same technique as running the pump for a short period. Print the values with no delay(), save the millis() value, then check each time through loop() whether the prining period has elapsed and if so do it again.

Incidentally, at some time (but not yet) you should consider using arrays to hold the instances of the sensor and the data returned by them. This will make the code smaller and easier to read.

thankyou for helping me figure this out. the only problem im having right now is i seem to get some kind of interference. everything is fine untill i plug in the 12 power source and the solenoid. the 12vdc power is not connected to the ic circuits. only the relay switches the hotwire to the solenoid. if i plug the 12vdc power and solenoid in it just keeps turning on and off over and over again. its very strange. using a flyback diode did not help. neither did using and ohm pull up resistor. i wonder if the relay board im using has something connected to the circuit thats making things haywire. it seems to only do it a few times if i keep my wires far apart, i even added some debouce with the delay function and that helped a ton. but it still does it sometimes. maybe i need to shield the cables? i dont have too much knowledge in circuitry.

Seeing a circuit diagram would be helpful, even if it is only hand drawn and photographed.

UKHeliBob:
Seeing a circuit diagram would be helpful, even if it is only hand drawn and photographed.

See post #4 and #6
Tom... :slight_smile:

TomGeorge:
See post #4 and #6
Tom... :slight_smile:

I had already seen them but I forgot to turn on the sarcastic font before posting :slight_smile:

It was just interference from the solenoid.