You're resetting count at the start of the loop. Try changing it to static.
#include <Servo.h>
Servo myservo;
const int MaxNum = 3;
const int LEDPIN = 6; // LED Pin
const int SENSORPIN = 4; // Servo Pin
const int Time = 100; // delay time for LED
const int Stime = 500; // delay time for Servo
// variables will change:
int sensorState = 0; // variable for reading the pushbutton status
void setup() {
// Set the Servo pin
myservo.attach(2);
// initialize the LED pin as an output:
pinMode(LEDPIN, OUTPUT);
// initialize the sensor pin as an input:
pinMode(SENSORPIN, INPUT);
digitalWrite(SENSORPIN, HIGH); // turn on the pullup
}
void loop(){
// read the state of the pushbutton value:
sensorState = digitalRead(SENSORPIN);
int count = 0;
if (count < MaxNum) {
// check if the sensor beam is broken
if (sensorState == LOW) { // if it is, the sensorState is LOW
int X = 0; // loop to blink LED
do {
// turn LED on:
digitalWrite(LEDPIN, HIGH);
delay(Time);
digitalWrite(LEDPIN, LOW);
delay(Time);
X +=1;
} while (X <=5);
myservo.write(0); // Move Servo to angle 0 degree
delay(Stime);
myservo.write(30);
count++;
} else { // if beam is detected
// turn LED off:
digitalWrite(LEDPIN, LOW);
myservo.write(45); // Move Servo to angle 30 degree
}
} else {
digitalWrite(LEDPIN, LOW);
myservo.write(45); // Move Servo to angle 30 degree
}
}
if (sensorState == LOW) { // if it is, the sensorState is LOW
Does that comment tell us anything we didn't already know?
int X = 0; // loop to blink LED
do {
// turn LED on:
digitalWrite(LEDPIN, HIGH);
delay(Time);
digitalWrite(LEDPIN, LOW);
delay(Time);
X +=1;
} while (X <=5);
A for loop would make a whole lot more sense. You REALLY should forget that do/while exists. It is almost never the right solution. I can count on one thumb the number of times I have used do/while in 35 years as a professional programmer. And, still have a thumb to spare.
myservo.write(45); // Move Servo to angle 30 degree
If you are going to have useless comments, you look really stupid when they don't match the code.
I'm making a dog treat dispenser, where my dog breaks an IR beam, and then it runs a servo motor.
I have a limit on it (so he doesnt eat too many haha) and i want it to reset after says 1 hour.
thanks
here is the section of code in program
int count = 0;
void loop(){
sensorState = digitalRead(SENSORPIN); // reads the state of Sensor:
if (count < MaxNum) { // check if the sensor beam is broken
digitalWrite(LEDPIN2, LOW);
if (sensorState == LOW) {
for (int x = 0; x <= 5; x ++) { // loop for LED.
digitalWrite(LEDPIN, HIGH);
delay(RTime);
digitalWrite(LEDPIN, LOW);
delay(RTime);
}
// runs Servo motor.
myservo.write(0);
delay(Stime);
myservo.write(30);
count++;
}
else { // if beam is detected
myservo.write(30); // Move Servo to angle 45 degree
}
}else if (count == MaxNum) { // if the max count has been reached
digitalWrite(LEDPIN2, HIGH);
myservo.write(30);
}
}
Always show us your complete code.
Use the </> icon to bring up code tags where you can paste your sketch.
I am not sure if your code works but this is a trivial task for the Arduino.
You almost never use delay(timeAmount); as it freezes code execution for this amount of time.
Use the BWD BlinkWithoutDelay technique (as talked about in the links) to do things at a particular time.
PaulS:
A for loop would make a whole lot more sense. You REALLY should forget that do/while exists. It is almost never the right solution. I can count on one thumb the number of times I have used do/while in 35 years as a professional programmer. And, still have a thumb to spare.
Geez I hope you're kidding but maybe you never did need a loop that depended on a condition other than count or perhaps for those cases you inserted a conditional that led to a break command.
A while loop with an index can be faster than a for-loop to do the same thing.
If you had chosen to tell the OP not to use int where byte would do, like for pin numbers, I would have agreed.
i read the link and get the idea behind it but im still a little unsure about it and i have NO clue how to implement that in my program.
here is my entire program
#include <Servo.h>
Servo myservo;
const int MaxNum = 3; // number times beam can be tripped
const int LEDPIN = 6; // Red LED Pin
const int LEDPIN2 = 8; // White LED Pin
const int SENSORPIN = 4; // Servo Pin
const int RTime = 150; // delay time for Red LED
const int WTime = 150; // delay time for White LED
const int Stime = 500; // delay time for Servo
int sensorState = 0; // variable for reading the pushbutton status
void setup() {
myservo.attach(2);
pinMode(LEDPIN2, OUTPUT);
pinMode(LEDPIN, OUTPUT);
pinMode(SENSORPIN, INPUT);
digitalWrite(SENSORPIN, HIGH);
}
int count = 0;
void loop(){
sensorState = digitalRead(SENSORPIN); // reads the state of Sensor:
if (count < MaxNum) { // check if the sensor beam is broken
digitalWrite(LEDPIN2, LOW);
if (sensorState == LOW) {
for (int x = 0; x <= 5; x ++) { // loop for LED.
digitalWrite(LEDPIN, HIGH);
delay(RTime);
digitalWrite(LEDPIN, LOW);
delay(RTime);
}
// runs Servo motor.
myservo.write(0);
delay(Stime);
myservo.write(30);
count++;
}
else { // if beam is detected
myservo.write(30); // Move Servo to angle 45 degree
}
}else if (count == MaxNum) { // if the max count has been reached
digitalWrite(LEDPIN2, HIGH);
myservo.write(30);
}
}
Geez I hope you're kidding but maybe you never did need a loop that depended on a condition other than count or perhaps for those cases you inserted a conditional that led to a break command.
I DO use while loops, often, when I don't know how many times something needs to happen. But, a while statement checks the condition, and then executes the body of the statement if the condition is true.
A do/while statement executes the body and then checks to see whether it should have. I have not yet found a case where that is the right thing to do.
PaulS:
A do/while statement executes the body and then checks to see whether it should have. I have not yet found a case where that is the right thing to do.
I like to think of it as doing something then checking if it needs to keep doing it.
It is a matter of how one arranges the flow of execution, you may never have a better-fit situation for do-while.
In the OP's case, a for-loop is the obvious choice.