I've been working on a laser to count parts at the factory I work at. The laser counter needs to count every 3 parts as one on the serial. The code pasted below works, but it counts only one part as one. It needs to count 3 as one on the serial. I appreciate any help and I'll admit I'm still relatively new to arduino.
I used this code from a youtube video so ir_pin was suppose to be an inferred sensor. but it fits my purpose.
int ir_pin = 7;
int counter = 0;
int hitObject = false;
void setup() {
Serial.begin(9600);
pinMode(ir_pin,INPUT);
}
void loop() {
int val = digitalRead(ir_pin);
if( (val == 0) && (hitObject == false) ){
counter++;
hitObject = true;
Serial.print("Counter = ");
Serial.println( counter);
}else if( (val ==1) && (hitObject == true) ) {
hitObject = false;
}
Add a second counter.
When you increment "counter", compare it to 3. If equal, add 1 to your new counter and zero "counter".
Where you print "counter", change to print your new counter name.
Paul
Paul_KD7HB:
Add a second counter.
When you increment "counter", compare it to 3. If equal, add 1 to your new counter and zero "counter".
Where you print "counter", change to print your new counter name.
Paul
I'm not sure what you mean. I attempted to do as you asked but only 0 returns now. Sorry I'm new to alot of this.
Thank you!
pstaley:
I'm not sure what you mean. I attempted to do as you asked but only 0 returns now. Sorry I'm new to alot of this.
Thank you!
And the new code is where?
Paul_KD7HB:
And the new code is where?
sorry. It's right here. If tried a few things. so this may not look exactly what you originally told me.
thanks.
int ir_pin = 7;
int counter = 0;
int new_counter=0;
int hitObject = false;
void setup() {
Serial.begin(9600);
pinMode(ir_pin,INPUT);
}
void loop() {
int val = digitalRead(ir_pin);
if((val == 0)&&(hitObject == false) ){
if(counter++ == 3);
hitObject = true;
Serial.print("Counter = ");
Serial.println( new_counter);
}else if( (val ==1) && (hitObject == true) ) {
hitObject = false;
}
}
pstaley:
sorry. It's right here. If tried a few things. so this may not look exactly what you originally told me.
thanks.
int ir_pin = 7;
int counter = 0;
int new_counter=0;
int hitObject = false;
void setup() {
Serial.begin(9600);
pinMode(ir_pin,INPUT);
}
void loop() {
int val = digitalRead(ir_pin);
if((val == 0)&&(hitObject == false) ){
if(counter++ == 3);
hitObject = true;
Serial.print("Counter = ");
Serial.println( new_counter);
}else if( (val ==1) && (hitObject == true) ) {
hitObject = false;
}
}
Change to:
if((val == 0)&&(hitObject == false) ){
if(counter++ == 3)
{
counter = 0;
++new_counter // this will be 1/3 the old count
};
hitObject = true;
Serial.print("Counter = ");
Serial.println( new_counter);
Paul_KD7HB:
Thank you! that seem to have worked. Atleast good enough for now. My next step will be connection it to a 7 segment display.. But thats for another day.
I really appreciate your help!