I want to use the PUSH BUTTON in order start up my servo code which is written below.
#include <Servo.h>
Servo myservo1; // create servo object to control a servo
// twelve servo objects can be created on most boards
Servo myservo2; // create servo object to control a servo
Servo myservo3; // create servo object to control a servo
int pos = 0; // variable to store the servo position
void setup() {
myservo1.attach(10); // attaches the servo on pin 10 to the servo object
myservo2.attach(9); // attaches the servo on pin 9 to the servo object
myservo3.attach(8); // attaches the servo on pin 8 to the servo object
}
void loop() {
int randNumber = random(1, 4);
Serial.println(randNumber);
int del = random(1000,16000);
Serial.println(randNumber);
delay (del);
if (randNumber == 1){
// in steps of 1 degree
myservo1.write(90);
}
if (randNumber == 2){
// in steps of 1 degree
myservo2.write(90);
}
if (randNumber == 3){
// in steps of 1 degree
myservo3.write(90);
}
delay(2000);
myservo1.write(pos); // tell servo to go to position in variable 'pos'
myservo2.write(pos);
myservo3.write(pos);
// waits 15ms for the servo to reach the position
}
See the silly smiley face with sunglasses? You have posted code without using code tags. This creates certain problems and obstacles for other forum members. The code tags make the code look
like this
when posting source code files. It makes it easier to read, and can be copied with a single mouse click.
If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the [code] and [/code] metatags.
aarg:
See the silly smiley face with sunglasses? You have posted code without using code tags. This creates certain problems and obstacles for other forum members. The code tags make the code look
like this
when posting source code files. It makes it easier to read, and can be copied with a single mouse click.
If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the [code] and [/code] metatags.
You should connect one terminal of the switch to ground, the other to an input pin. Configure it to use the input pullup.
Then use this before the servo code:
while (digitalRead(buttonPin) == HIGH) { }
It continually reads the pin and does nothing until the pin goes LOW (which the switch closure will cause).
aarg:
You should connect one terminal of the switch to ground, the other to an input pin. Configure it to use the input pullup.
Then use this before the servo code:
while (digitalRead(buttonPin) == HIGH) { }
It continually reads the pin and does nothing until the pin goes LOW (which the switch closure will cause).
What part of the tutorial, was not helpful?
So i just need to put that line at the top of my servo code?
Benny_Leonard:
So i just need to put that line at the top of my servo code?
You can do - but a better plan is to understand that line and then put it at the top of your servo code.
The problem with doing it that way is that it will "block" - you may find that once you have put this into the Arduino that you can't upload a new sketch without resetting the board first.
Try this:
void loop() {
if(digitalRead(buttonPin) == HIGH) {
move_servo();
}
}
void move_servo() {
int randNumber = random(1, 4);
Serial.println(randNumber);
int del = random(1000,16000);
Serial.println(randNumber);
delay (del);
if (randNumber == 1){
// in steps of 1 degree
myservo1.write(90);
}
if (randNumber == 2){
// in steps of 1 degree
myservo2.write(90);
}
if (randNumber == 3){
// in steps of 1 degree
myservo3.write(90);
}
delay(2000);
myservo1.write(pos); // tell servo to go to position in variable 'pos'
myservo2.write(pos);
myservo3.write(pos);
// waits 15ms for the servo to reach the position
}
The loop just exits immediately if there is no button press happening. So it goes back outside to the underlying Arduino framework code to check if there is another sketch coming down the serial pipe.
Maybe I am wrong, but i didn't see any button code in your codes. the easiest way is to use a button library written by someone else. and try to avoid delay because delay freezes your board, making it hard to read a real time button when it's pressed
aarg:
See the silly smiley face with sunglasses? You have posted code without using code tags. This creates certain problems and obstacles for other forum members. The code tags make the code look
like this
when posting source code files. It makes it easier to read, and can be copied with a single mouse click.
If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the [code] and [/code] metatags.
This poor guy is asking for help for a simple button code, yet he is being seriously lectured by a girl about how to use a code tag in the forum instead of really helping him out? Maybe he is a pretty girl, and you are a handsome guy. who knows
arduinomagbit:
This poor guy is asking for help for a simple button code, yet he is being seriously lectured by a girl about how to use a code tag in the forum instead of really helping him out? Maybe he is a pretty girl, and you are a handsome guy. who knows
What you are saying is completely sexist. Also pay attention to reply #5.
PaulMurrayCbr:
You can do - but a better plan is to understand that line and then put it at the top of your servo code.
The problem with doing it that way is that it will "block" - you may find that once you have put this into the Arduino that you can't upload a new sketch without resetting the board first.
void move_servo() {
int randNumber = random(1, 4);
Serial.println(randNumber);
int del = random(1000,16000);
Serial.println(randNumber);
delay (del);
if (randNumber == 1){
// in steps of 1 degree
myservo1.write(90);
}
if (randNumber == 2){
// in steps of 1 degree
myservo2.write(90);
}
if (randNumber == 3){
// in steps of 1 degree
myservo3.write(90);
}
delay(2000);
myservo1.write(pos); // tell servo to go to position in variable 'pos'
myservo2.write(pos);
myservo3.write(pos);
// waits 15ms for the servo to reach the position
}
The loop just exits immediately if there is no button press happening. So it goes back outside to the underlying Arduino framework code to check if there is another sketch coming down the serial pipe.
So is this it.. (code below)
#include <Servo.h>
Servo myservo1; // create servo object to control a servo
// twelve servo objects can be created on most boards
Servo myservo2; // create servo object to control a servo
Servo myservo3; // create servo object to control a servo
int pos = 0; // variable to store the servo position
void setup() {
myservo1.attach(10); // attaches the servo on pin 10 to the servo object
myservo2.attach(9); // attaches the servo on pin 9 to the servo object
myservo3.attach(8); // attaches the servo on pin 8 to the servo object
}
void loop() {
if(digitalRead(buttonPin) == HIGH) {
move_servo();
}
}
void move_servo() {
int randNumber = random(1, 4);
Serial.println(randNumber);
int del = random(1000,16000);
Serial.println(randNumber);
delay (del);
if (randNumber == 1){
// in steps of 1 degree
myservo1.write(90);
}
if (randNumber == 2){
// in steps of 1 degree
myservo2.write(90);
}
if (randNumber == 3){
// in steps of 1 degree
myservo3.write(90);
}
delay(2000);
myservo1.write(pos);
myservo2.write(pos);
myservo3.write(pos);
}
It looks okay, but the sense of the switch is backwards. You have an active LOW switch. Thus you should be testing for LOW, not HIGH, to run the servo code. You also failed to configure the switch input with a pinMode() call in setup().
AWOL:
I can't see where you defined buttonPin, or set the pin's mode.
int inPin = 7; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
void setup() {
pinMode(inPin, INPUT); // declare pushbutton as input
}
#include <Servo.h>
Servo myservo1; // create servo object to control a servo
// twelve servo objects can be created on most boards
Servo myservo2; // create servo object to control a servo
Servo myservo3; // create servo object to control a servo
int pos = 0; // variable to store the servo position
void setup() {
myservo1.attach(10); // attaches the servo on pin 10 to the servo object
myservo2.attach(9); // attaches the servo on pin 9 to the servo object
myservo3.attach(8); // attaches the servo on pin 8 to the servo object
}
void loop() {
if(digitalRead(buttonPin) == LOW) {
move_servo();
}
}
void move_servo() {
int randNumber = random(1, 4);
Serial.println(randNumber);
int del = random(1000,16000);
Serial.println(randNumber);
delay (del);
if (randNumber == 1){
// in steps of 1 degree
myservo1.write(90);
}
if (randNumber == 2){
// in steps of 1 degree
myservo2.write(90);
}
if (randNumber == 3){
// in steps of 1 degree
myservo3.write(90);
}
delay(2000);
myservo1.write(pos);
myservo2.write(pos);
myservo3.write(pos);
}
aarg:
No. You now have two setup() functions. You are only allowed to have one.
Also, what are your plans for the variable "val"? You don't use it.
Thanks man.. Think that I have finally worked it out.. Is this it?
#include <Servo.h>
int inPin = 7; // choose the input pin (for a pushbutton)
Servo myservo1; // create servo object to control a servo
// twelve servo objects can be created on most boards
Servo myservo2; // create servo object to control a servo
Servo myservo3; // create servo object to control a servo
int pos = 0; // variable to store the servo position
void setup() {
pinMode(inPin, INPUT); // declare pushbutton as input
myservo1.attach(10); // attaches the servo on pin 10 to the servo object
myservo2.attach(9); // attaches the servo on pin 9 to the servo object
myservo3.attach(8); // attaches the servo on pin 8 to the servo object
}
void loop() {
if(digitalRead(buttonPin) == LOW) {
move_servo();
}
}
void move_servo() {
int randNumber = random(1, 4);
Serial.println(randNumber);
int del = random(1000,16000);
Serial.println(randNumber);
delay (del);
if (randNumber == 1){
// in steps of 1 degree
myservo1.write(90);
}
if (randNumber == 2){
// in steps of 1 degree
myservo2.write(90);
}
if (randNumber == 3){
// in steps of 1 degree
myservo3.write(90);
}
delay(2000);
myservo1.write(pos);
myservo2.write(pos);
myservo3.write(pos);
}