1

I'm a beginner making a simple arduino for a school project with an arduino uno. After the press of a button I activate, after a small delay, an external circuit obtained by disassembling another product, and it deactivates automatically after a delay of some seconds. At first it works but the problem is when I try to press the button again, the function that activates the external circuit is called but it looks like the digitalWrite() functions are not working. After the circuit deactivates if I press the button for a third time it will work fine but it won't activate the fourth time but it will work the fifth time and so on...

If I do the same on the led on pin 13 of the arduino board everything works fine everytime I try it.

If I reset everything with the button on the board it works but I can't do it on the final project. I tried resetting it by code but with no success.

I suspect the external circuit activates only half of the time because it has a button in it that originally was intended to activate the circuit but now it interferes somehow.

Here's the code:

int mistState = LOW;         // the current state of the output pin
int buttonState ;             // the current reading from the input pin
int lastButtonState = HIGH;   // the previous reading from the input pin

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {

  Serial.begin(9600);
  pinMode(buttonPin, INPUT_PULLUP);
  digitalWrite(mistPin, LOW);
  pinMode(mistPin, OUTPUT);
  Serial.println("start");
  buttonState = HIGH;
  Serial.println(buttonState);
}

void loop() {


  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);
  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH), and you've waited long enough
  // since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;
      // only toggle the LED if the new button state is HIGH
      if (buttonState == LOW) {
        activateMistMaker(2000, 5000);
      }
    }
  }

  // save the reading. Next time through the loop, it'll be the lastButtonState:
  lastButtonState = reading;
}

//void (* resetFunc ) (void) = 0;

void activateMistMaker(int timeOn, int timeOff)
{
    delay(timeOn);
    digitalWrite(mistPin, HIGH);
    Serial.println("acceso");
    delay(timeOff);
    digitalWrite(mistPin, LOW);
    Serial.println("spento");
    delay(50);
    //resetFunc() ;
}

What should I change to make the external circuit work everytime? I tried lots of different ways but with no success so far. Thanks in advance for your help.

3
  • 1
    Without knowing anything about the "external circuit" it's difficult to provide meaningful input--but if it works every other time maybe just run the Arduino button press code twice (possibly with a delay)? – Dave Newton Jun 5 at 15:27
  • 1
    How exactly have you connected the Arduino to the mist maker? – chrisl Jun 5 at 15:30
  • the external circuit is a simple circuit with a button that originally was to activate the mist maker. It is connected to the arduino at the pin 8 via a pull down resistance (i think it's called like that) and has 2 more cables, one goes to the vin pin and the other to the ground. The arduino button is connected to the ground and to the pin 2 – Danisuper Jun 5 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.