im making a project for school using my arduino uno. the problem that im having is that im trying to make a two or four censor that involves a train and a railroad gate. when the trains wagons passes thru the censor the gates goes down and counts the amounts of wagons(censors) goes thru and the gate goes from position 90 degrees to 180 degrees and when the wagons(censors) goes thru the next censors the gates goes up from 180 degrees cars go thru. i think i have to use a combination of bounce or debounce programing as well as the servo motor. but when i try running the push button only sends a signal to the servo everytime i push the button and goes from 90 degrees to 180 degrees and stops... please help.
Please post the code that you have (using the code tag) and try to provide a clearer idea of what you are trying to accomplish.
Pictures of the physical setup are also great.
Without you providing more information no one will be able to help much.
thanks.. working on it...lol
Just for future reference, and to make google seaches easier for you, a device which detects things is called a "sensor".
Someone who edits published material to remove sensitive or objectionable material is called a "censor".
Was that a censure?
![]()
No, just an aid to googling.
A censure woulld have mentioned proper use of shift key and punctuation, but that would have been rude.
what i want for the program to recognize is when i press the button/sensor the gate goes from 0 or 180 degrees to 90 degrees and when i let go of the button/sensor the gate goes back to 0 or 180 degrees.
/*
Button_Sweep
Turns on and off a light emitting diode(LED) connected to digital
pin 13, when pressing a pushbutton attached to pin 2.
The circuit:
* LED attached from pin 13 to ground
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground
* Note: on most Arduinos there is already an LED on the board
attached to pin 13.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
// constants won't change. They're used here to
// set pin numbers:
int ledPin = 10; // the number of the LED pin
int buttonPin = 2; // the number of the pushbutton pin
int pos = 0; // variable to store the servo position
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup()
{
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input:
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
buttonState = digitalRead(buttonPin); // read the state of the pushbutton value:
{
if (buttonState == HIGH) // check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
}
for(pos = 0; pos < 90; pos += 150) // goes from 0 degrees to 90 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(2000); // waits 2s for the servo to reach the position
}
digitalWrite(ledPin, HIGH); // turn LED on:
for(pos = 90; pos>=0; pos-=150) // goes from 90 degrees to 0 degrees
{
else {
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(2000); // waits 2s for the servo to reach the position
digitalWrite(ledPin, LOW); // turn LED off:
}
}
dezDuino77:
what i want for the program to recognize is when i press the button/sensor the gate goes from 0 or 180 degrees to 90 degrees and when i let go of the button/sensor the gate goes back to 0 or 180 degrees./*
Button_Sweep
Turns on and off a light emitting diode(LED) connected to digital
pin 13, when pressing a pushbutton attached to pin 2.
The circuit:
-
LED attached from pin 13 to ground
-
pushbutton attached to pin 2 from +5V
-
10K resistor attached to pin 2 from ground
-
Note: on most Arduinos there is already an LED on the board
attached to pin 13.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
// constants won't change. They're used here to
// set pin numbers:
int ledPin = 10; // the number of the LED pin
int buttonPin = 2; // the number of the pushbutton pin
int pos = 0; // variable to store the servo position
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup()
{
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input:
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
buttonState = digitalRead(buttonPin); // read the state of the pushbutton value:
{
if (buttonState == HIGH) // check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
}
for(pos = 0; pos < 90; pos += 150) // goes from 0 degrees to 90 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(2000); // waits 2s for the servo to reach the position
}
digitalWrite(ledPin, HIGH); // turn LED on:
for(pos = 90; pos>=0; pos-=150) // goes from 90 degrees to 0 degrees
{
else {
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(2000); // waits 2s for the servo to reach the position
digitalWrite(ledPin, LOW); // turn LED off:
}
}
some "interesting" code/constructs there
let's take
for (pos=0; pos < 90; pos+=150)
that will set pos to 0 - good start
if it's less than 90, it will add 150
so - how many times do you think you'll go round that loop?
your comment "in steps of 1 degree" implies that you think "lots of times"
nope you'll get one step! (0+150 = 150, which is > 90!!)
you seem to have unecessary { } around this
{
if (buttonState == HIGH)
}
suggest you remove them
once you've fixed your servo positioning to read
for (pos=0; pos < 90; pos+=1)
you might want to take the 2 second delay out of each step!!
and move it to the end of the loop!
try that and see how it goes
and wtf is this a poll?
and wtf is this a poll?
not sure what you meant by that. but thanks for the help i post something if i get any more error codes.
Poll.
Up there, at the top of the page.
Why?
The top RH button when you got to make a new topic is "post new poll". I think its proximity to "new topic" means a few people hit it by mistake, or maybe think "new poll" is a "new question".
http://en.wiktionary.org/wiki/poll
The first definition of poll is "An election or a survey of a particular group. ".
So perhaps posters think by doing a "new poll" they are asking the group (us) a question.
Personally I would move "post new poll" further away so you have to look harder to find it.
and wtf is this a poll?
Obviously a clever way to trap many into a pointless brain loop concerning polls. ![]()
my bad on the poll, is the first time i posted something and i mark those not know what it will do.
so did you try what I suggested way back in post #7?
Okay, so that is quite possibly the strangest code I have ever seen. Does it even compile?
void loop()
{
buttonState = digitalRead(buttonPin); // read the state of the pushbutton value:
if (buttonState == HIGH)
myservo.write(0);
else
myservo.write(90);
delay(2000); // waits 2s for the servo to reach the position
digitalWrite(ledPin, buttonState); // turn LED on/off
}
That should do the same thing, I believe... although from your code I cannot really tell what you are trying to do.
I am also curious about the purpose of the delay() call. Is this to delay the next sensor check or is it supposed to wait for the servo to move. I don't know much about servos, but it doesn't seem the servo would need 2 seconds to change positions.
If you are wanting to slow the servo action down a bit, you could put in your loops (maybe that was the original plan) and make a small delay between each degree call (20ms per iteration should get you about 2 seconds total). The problem with that is you need to maintain your button state between loop calls and only change the servo position when the button state changes. Otherwise the gate will just keep moving up and down... or down and up if you press the button.
I think i would start with a button sketch that turns an LED on/off. I'd then ad the servo library and send the servo to 180 when the LED is turned on and to 90 when the LED is turned off.
Yes in some way.
I'm going to post the new code and where I got stock. Sorry if I still have some of the wrong codes on the same program.
ok guys i did some changings in the code but got a bit complicate. guys i know im a newbie and i do apreciate ur help thats why im taking all the sarcasism please let me know where did i go wrong once again. i also added the code from (KIDMOSEYs sample) and yes kid it does compile, also ZOOMKAT thats what in some way i want the whole sketch.
Button_Sweep
Turns on and off a light emitting diode(LED) connected to digital
pin 13, when pressing a pushbutton attached to pin 2.
The circuit:
* LED attached from pin 13 to ground
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground
* Note: on most Arduinos there is already an LED on the board
attached to pin 13.
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
// constants won't change. They're used here to
// set pin numbers:
int ledPin = 10; // the number of the LED pin
int buttonPin = 2; // the number of the pushbutton pin
int pos = 0; // variable to store the servo position
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup()
{
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input:
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
buttonState = digitalRead(buttonPin); // read the state of the pushbutton value:
if (buttonState == HIGH); // check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
for(pos=90; pos < 180; pos+=0); // goes from 0 degrees to 90 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
}
digitalWrite(ledPin, HIGH); // turn LED on:
if (buttonState == LOW); // check if the pushbutton is depressed.
for(pos=180; pos > 90; pos-=1); // goes from 180 degrees to 2 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(2000); // waits 2s for the servo to reach the position
buttonState = digitalRead(buttonPin); // read the state of the pushbutton value:
if (buttonState == HIGH)
myservo.write(0);
else
myservo.write(90);
delay(2000); // waits 2s for the servo to reach the position
digitalWrite(ledPin, buttonState); // turn LED on/off
}
for(pos=90; pos < 180; pos+=0);
Pointless - lose the semicolon
if (buttonState == LOW); // check if the pushbutton is depressed.
for(pos=180; pos > 90; pos-=1);
Ditto
for(pos=180; pos > 90; pos-=1); // goes from 180 degrees to 2 degrees
When code and comments disagree, the compiler has the final word.