I've started a project and I need a little help. Ultimately my goal is to setup a way to count bottles per minute on our bottle line at work. I'm very new to Arduino. I've done little more than the very basics so far. I've found this link that is getting me started. http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1262466908/3
I'm trying a slightly different approach. I setup an IR emmiter and detector for my first test. Thinking when the beam is broken, that will count one.
I can get it to flash the LED when the beam is broken. I get into trouble when I use the modified code from the link above to add the counting. I get an error on the line... if (val <> valold)
error: expected primary-expression before '>' token
Seems like it doesn't like the <> but I don't know enough to fix it.
I hope I am clear enough. I pasted below what is working for me so far.
Thanks in advance for any assistance.
int ledPin = 13; // LED connected to digital pin 13
int irOut = 6;
int photoPin = 2; //
int val = 0;
unsigned int count = 0;
void setup() // run once, when the sketch starts
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
pinMode(irOut, OUTPUT);
pinMode(photoPin, INPUT);
}
void loop() // run over and over again
{
digitalWrite(irOut, HIGH);
val = digitalRead(photoPin);
if (val==LOW)
{
digitalWrite(ledPin, LOW);
}
else
{
digitalWrite(ledPin, HIGH);
count ++;
}
}
Thanks for the quick response. That fixed the error.
New problem. After a minute, the LED goes out and I get all 0's in the serial window. If I close the window, the let acts normal again. This is what I have.
int ledPin = 13; // LED connected to digital pin 13
int irOut = 6;
int photoPin = 2; //
int val = 0;
int valold = 0;
int start;
unsigned int count = 0;
void setup() // run once, when the sketch starts
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
pinMode(irOut, OUTPUT);
pinMode(photoPin, INPUT);
valold = digitalRead(photoPin);
Serial.begin(9600);
}
void loop() // run over and over again
{
digitalWrite(irOut, HIGH);
start = millis(); // take the start time
count = 0; // reset counter
// do the following loop for 60000ms = 1min
while (millis()-start < 60000)
{
// check for overflow of millis()
if (start > millis()) {
start = millis();
count = 0;
}
val = digitalRead(photoPin);
if (val==LOW)
{
digitalWrite(ledPin, LOW);
}
else
{
digitalWrite(ledPin, HIGH);
if (val != valold) {
count ++;
valold = val;
}
}
}
// 1 minute over. print counts
Serial.println(count,DEC);
}
Each time you open or close the serial monitor, the Arduino gets reset. Which is why you see things start to work again. The "works for about a minute" is a huge clue.
this line: "start = millis(); // take the start time" is the problem. Well, part of the problem.
Look at the reference page for millis(). Notice what kind of data type it returns and what kind of data type you are using for the variable "start."