I am trying to make a color sorter for ping pong balls. If the color sensor reads "red" I want the motor to rotate 120 degrees CW and if it reads "green" I want it to rotate 120 degrees CCW.
Using servo1.write(120) won't work though. Because if I drop another red ball the sorter will just remain in the same position. Is it possible to do what I am trying?
Please ask for more questions if confused.
Do you mean "if I'm at 120 degrees, rotate to 0 degrees, and if I'm at 0 degrees, rotate to 120 degrees" ?
To some extent, it depends on your physical ‘ball handling arrangement’ *
But reading between the lines, i’d say,...
Why don’t you restore the servo to the ‘home position’ between balls - then you can choose which direction you want to turn the gate for each ball drop.
Home at 90
Left to 0
Right to 180
- restore to home after 200mS or whatever is needed between drops.
- i never thought i’d write that in a post.
Servos only go to defined positions. If you want anything else then it's up to you to keep track of the position the servo is currently in and do the arithmetic yourself.
But your spec isn't really clear. If you mean what you say e.g. red ball, rotate 120 CW, another red ball rotate another 120 CW, another rotate another 120 CW and so on then it won't work. Servos can't rotate that far.
What kind of motor? Can you post a drawing of your mechanism? And post your code.
I am trying to make a color sorter for ping pong balls. If the color sensor reads “red” I want the motor to rotate 120 degrees CW and if it reads “green” I want it to rotate 120 degrees CCW.
Using servo1.write(120) won’t work though. Because if I drop another red ball the sorter will just remain in the same position. Is it possible to do what I am trying?
I can’t make sense of this.
Are you saying that if the first red ball is detected you want to do servo.write(120) and if the second ball is also red you want do do servo.write(240) ?
The maths for that is trivial but most servos can only move as far as 180°.
Seems to me like there's a centre position as lastchancename surmised, from which it needs to swing 120 left or right depending on the colour, then centre once the ball's been delivered.
But if that's the case then full left to full right is 240 which is beyond common-or-garden servos.