Adding another IF -Prog. executing stops

Hi!

My project is a dog feeder. Every 12 hour the servo will dispense some food. If additional food is requerd there will be a button to press.

I got the millis() function to work (Thanks to you guys). But now, adding the button makes the program execution halt at 5 sec. A simple thing as addig a button and the program halts. I tried 2 different approachs same results, program simulation just stops at 5 sec. Is it a bug in circuits?

The project in a state where everything still works: https://circuits.io/circuits/3018431-millis-servo

Thanks..

Here's the code that works. Now tell me what's wrong with this other code I didn't show you. See the problem there?

EDIT: Actually, I didn't find any code at that link either. OP, post that stuff here where we can work on it.

Delta_G:
Here’s the code that works. Now tell me what’s wrong with this other code I didn’t show you. See the problem there?

EDIT: Actually, I didn’t find any code at that link either. OP, post that stuff here where we can work on it.

Sorry about that, thought you guys can see the layout and code in the link.

Here is my code so far.
I put an “IF” with button then the program simulation halts at 5 sec. (same as the “interval”).
The button will enable the servo to despense additional food.

#include <Servo.h>
Servo DoserServo;
unsigned long previousMillis = 0; // will store last time updated
const unsigned long interval = 5000; // Change every 12 hours (12UL60UL60UL*1000UL)
const int buttonPin = 5;
const int PowerOnLED = 11;
void setup()
{
** DoserServo.attach(13);**

** pinMode(buttonPin, INPUT);**
** digitalWrite(buttonPin, 0);**

** pinMode(PowerOnLED, OUTPUT);**
** digitalWrite(PowerOnLED, 1);**
}
void loop()
{
** unsigned long currentMillis = millis();**

** if (currentMillis - previousMillis >= interval)**
** {**
** previousMillis += interval;**
** DoserServo.attach(13);**
** DoserServo.write(180);**
** delay(1200);**
** DoserServo.write(0);**
** delay(1200);**
** DoserServo.detach();**
** delay(1200);**
** }**
}

pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, 0);

Use LOW and HIGH, not 0 and 1 for these types of lines. It makes it much easier to read.

SO you're turning off the built-in pull-up resistor. Do you have an external one? Is it pull-up or pull-down?

I don't see any if statement in that code that relates to any button. So did you just lie to us saying you put one in when you really didn't? Why don't you try it first before you come here and say it doesn't work. Or did you just not get what I wrote in the last post that we need to see the code that causes the problem not some other piece of code.

Delta_G: Use LOW and HIGH, not 0 and 1 for these types of lines. It makes it much easier to read.

SO you're turning off the built-in pull-up resistor. Do you have an external one? Is it pull-up or pull-down?

I don't see any if statement in that code that relates to any button. So did you just lie to us saying you put one in when you really didn't? Why don't you try it first before you come here and say it doesn't work. Or did you just not get what I wrote in the last post that we need to see the code that causes the problem not some other piece of code.

If you have such keen eyes. Detecting "0" insead of "LOW" Maybe use your keen eyes and read what I´m telling you. I wrote:

istvanuino: The project in a state where everything still works: https://circuits.io/circuits/3018431-millis-servo

You answered:

Delta_G: EDIT: Actually, I didn't find any code at that link either. OP, post that stuff here where we can work on it.

I got your first post, "ANY" code did you write then. That´s why I added the "The project in a state where everything still works:"

Insead of using your energy to bitch around, think two steps ahead and help with a button solution that you know work since I´m asking about a potational bug in the program when I said I´ve alread tried two different solutions to a simple button with the combination of the code above in Circuits. If that´s hard, be quiet insead!

Dude, cool your jets. I can't tell you what's wrong with the code with the button if you won't show the code with the button. When I add a button to some code it works fine for me. So there's no bug in the system. You've done something wrong. Please post the code where you tried to add the button and it didn't work and we can look at it. I don't understand how you think we can diagnose that without seeing it.

Unfortunatley I started the thread home, backed up to last working code then left for work. Now I´m at work, answering from my mobile. But I used first:

const int buttonPin pinMode(buttonPin, INPUT); int buttonVal = digitalRead(buttonPin); if(buttonVal = 0)

//Then int buttonPin= 2 pinMode(2, INPUT) int buttonState = 0 buttonState = digitalRead(buttonPin); if (buttonState == 1)

I´m writing this off-hand so the code inte NOT correct. I put these lines in void setup and loop.. Both solutions stopped at 5 sec. How did you solve this button thing?

Yeah. Neither of those will compile. I’d tell you why they don’t work but I think you know. When you get home put up the actual code and I’ll help you get it right. I promise it is possible to add button handling code to what you have. Damn simple actually. It’s probably something simple you overlooked.

Delta_G:
Yeah. Neither of those will compile. I’d tell you why they don’t work but I think you know. When you get home put up the actual code and I’ll help you get it right. I promise it is possible to add button handling code to what you have. Damn simple actually. It’s probably something simple you overlooked.

Okey, maybe I should quit this Arduino alread…
Rewrote the program and now he is entering the button IF statement every time.

The new code:

#include <Servo.h>
Servo DoserServo;
unsigned long previousMillis = 0; // will store last time updated
const unsigned long interval = 5000; // Change every 12 hours (12UL60UL60UL*1000UL)
const int buttonPin = 5;
const int PowerOnLED = 11;
int buttonState = 0;
void setup()
{
** DoserServo.attach(13);**

** pinMode(buttonPin, INPUT);**
** digitalWrite(buttonPin, 0);**

** pinMode(PowerOnLED, OUTPUT);**
** digitalWrite(PowerOnLED, 1);**
}
void loop()
{
** unsigned long currentMillis = millis();**

** if (currentMillis - previousMillis >= interval)**
** {**
** previousMillis += interval;**
** DoserServo.attach(13);**
** DoserServo.write(180);**
** delay(1200);**
** DoserServo.write(0);**
** delay(1200);**
** DoserServo.detach();**
** delay(1200);**
** }**

** buttonState = digitalRead(buttonPin);**

** if (buttonState == 1)**
** {**
** previousMillis += interval;**
** DoserServo.attach(13);**
** DoserServo.write(180);**
** delay(1200);**
** DoserServo.write(0);**
** delay(1200);**
** DoserServo.detach();**
** delay(1200);**
** }**
}