I am working with an Uno Starter Kit, nothing extra, and I am trying to create a starting gate for a Rube Goldberg project. I would like to press a button, have three LEDs go from Red>yellow>green, and then turn my servo motor 90 degrees. This is a copy of the code I have going right now. With the delay at the end of original loop the light stays red and servo doesn't move. Without it, it cycles through what I want it to do without stopping. Each time I press the switch(button) the board goes blank, so I stopped pressing the button. I have the switch hooked up to a digital pin through (-) side with 100uf capacitor. Any help would be greatly appreciated...
Thank you,
zane
It's hard to see exactly how you have the wires connected by the picture. Are you shorting the 5V to ground when you close the switch? This may explain the board is going blank. I suggest looking at the Button example to learn how to properly use a button. If you open the Arduino IDE, click on File, Examples, 2.Digital, you will find the example button. In the notes you will find a link to the web page.
Your servo looks to have a black, red, and white wire. The black would be ground, the red 5V, and the white should go to your servo pin (pin 5). You may have this right, but it's hard to follow the wires under the capacitor.
GunnerCAF:
Are you shorting the 5V to ground when you close the switch?
It looks like that to me.
I suggest removing the wire from the +ve rail to the switch/capacitor +ve, remove the wire from pin 11 to the switch/capacitor -ve, and connect a wire from pin 11 to the switch/capacitor +ve. Then change your sketch to enable the internal pull-up resistor on pin 11, and to treat HIGH as switch open and LOW as switch closed when reading pin 11.
Make sure that the wire from pin 5 isn't connected to that yellow link to the +ve power rail - it looks as if they may be connected to me, and if they are you'd blow pin 5 as soon as it is configured as an output.
I gather from your photograph, that you have a large capacitor paralleled across the supply and ground of the servo. This is eminently sensible though it could just as well have been plugged across the power rails on the side of the breadboard and would make the photo easier to interpret; the distance between one position and the other is in this case quite inconsequential compared to the length of the sires to the servos.
It is difficult to discern exactly how you have the pushbutton wired, but it does seem that you have some confusion about the Vcc wire. It would appear that you have the pushbutton connected directly across the power supply so that pressing the button shorts out the power. The consequence of that would be reasonably obvious.
The real curiousity is why you have another capacitor there? I suspect under the misapprehension that this will - if connected correctly - provide some sort of "debounce" to the pushbutton. I would suggest that you study up on software debounce coding and do not attempt to "fudge" it with large (or indeed, any) capacitors.
There should be no need to have anything to do with the button connecting to the supply rail (Note that the tutorial I cite there does use the less common and inadvisable approach of connecting the button to the supply rail with a pull-down resistor. It is much preferable to have the button referenced to ground and use the internal pull-up in the MCU, though I cannot see that you have enabled that.)
pinMode(pin, INPUT); // set pin to input
digitalWrite(pin, HIGH); // turn on pullup resistors
Thank you for the support guys, I figured it out though. I was attempting to combine 3 of the projects in the project book that comes with. I had connected the capacitor because the "moodindicator" called for it on the potentionator, but it is not needed when using a button, instead what was needed was a 10k resistor connected to ground, along with jumper from pin to ground; and a small jumper from +5v. If you guys still care I have posted pics and code of successful design. I have taken LEDs and servo off to emphasize wire structure. I am very new to this and can't say how much I appreciate this community.
Thank you all
I have studied your post but in all honesty, most of it goes over my noobish head, lol. I figured out this wiring prior to your post, if you could maybe describe what you mean in lay-terms it would be greatly appreciated. Any improvement or criticism on the efficiency of my design is very much welcomed. Thank you for your interest/concern.
-Zane
zmoze:
If you guys still care I have posted pics and code of successful design.
I can see what was wrong with the original circuit but I can't follow your description of how you fixed it. The image you posted is unfortunately not clear enough for me to make out how the switch is connected in the new scheme.
zmoze:
If you could maybe describe what you mean in lay-terms it would be greatly appreciated. Any improvement or criticism on the efficiency of my design is very much welcomed.
OK, fairly simple.
Wire the pushbutton between your switch pin - I gather it is now digital port 2 and was port 11 - and ground - no external resistor, no connection to Vcc.
insert the line:
digitalWrite(switchPin, HIGH); // turn on pullup resistors
after the line:
pinMode(switchPin, INPUT);
and change
if(switchVal == LOW) {
to
if(switchVal == HIGH) {
It would not be the only concern in your code; debouncing could be better, but the second instance of: