I'm doing a project using an Arduino to connect a servo with a photointerrupter. The idea is that when a coin blocks the infrared beam, the servo needs to turn a few degrees, wait and turn back to it's origional position. Frankly I'm not very skilled with Arduino and understand just basic code so I was hoping that someone could point out what I am doing wrong.
I did get the photointerrupter to work using pin A0 but the readings are very slow. Almost 3 seconds in between readings. This should be a lot faster.
The problem is that the servo is turning continuously without even triggering the sensor.
I think the program is sort of doing what I intended because when I trigger the sensor the servo seems to reset and complete the loop.
I know there is some code for leds but I'm not using those right now.
I hope someone can help me because the deadline is in two days and I'm oblivious as to what the problem might be.
your if() statement only applies to the first for()
you need to include both in a block { ... } e.g.
if (sensorValue >= 2)
{ // << add
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}// <<< add
why type of signal does the photointerrupter give you ?
also you are comparing sensorValue >= 2
I would have thought you would be picking up noise levels larger than that
I did get the photointerrupter to work using pin A0 but the readings are very slow. Almost 3 seconds in between readings. This should be a lot faster.
For that sort of application it should be possible to use the photo thingy with digital I/O. But you have not provided a link to the datasheet for the device or a circuit diagram. A photo-diode will react more quickly than a Light Dependent Resistor (LDR).
The problem is that the servo is turning continuously without even triggering the sensor.
Your program responds to every analog value greater than 1. That means, all 1022 values out of 1023 - so it is not surprising that it appears to be triggered all the time. You need to figure out the threshold that distinguishes between the beam being interrupted and not interrupted.
I hope someone can help me because the deadline is in two days and I'm oblivious as to what the problem might be.
A common problem - should have started the project in good time.
And does it mean that you are asking us to do your schoolwork? If so I hope our contribution will be mentioned in your project report.
horace, Thank you so much! It's working now. To answer your question. I'm trying to read a voltage signal to trigger the rest of the loop. And you're right, >=2 is a bit low. I think my resistor is a bit to strong so I'm only picking up low voltages. I will probably buy a les potent resistor. Like Robin2 said, I will mention you in my report if you don't mind.
Robin2, I could have used digital input indeed but analog seemed easier to me. The photointerrupter is encased in some sort of a coinshaft but it might still get to much interference from daylight. I will work on that and narrow down the threshold.
I know how it sounds. I was supposed to make a multiway switching circuit but I wanted to put in a little more effort and bought a Arduino. Maybe I was to optimistic and underestimated learning to program from zero. I hope it didn't seem like I wanted others to write my code instead of me.