System: Arduino Nano
How can i get my arduino Nano to:
Push the push button (lock) and then move my servo from 0 degrees to 60 degrees? And when I rel the pushbutton (unlock) it should go back to 0 degrees.
System: Arduino Nano
How can i get my arduino Nano to:
Push the push button (lock) and then move my servo from 0 degrees to 60 degrees? And when I rel the pushbutton (unlock) it should go back to 0 degrees.
What have you tried?
You mention servos. A great place to start would be to load up the servo sweep tutorial. This will let you confirm that you can push a sketch to your arduino and that you have the servo wired up properly.
There is also a button tutorial. It lets you exercise your skill with a button.
Then you can try to combine the two to achieve your goals.
If you get stuck along the way, let us know. Post your code (using code tags). Tell us what you expected your sketch to do, what is actually does and how those two differ.
Its not working
Here is my code:
#include <Servo.h>
Servo myservo;
const byte button = 10;
void setup()
{
myservo.attach(3);
pinMode(button, INPUT);
}
void loop()
{
if (digitalRead(button) == HIGH) {
myservo.write(0);
}
else {
myservo.write(60);
}
}
Did you try the sweep example? did it work?
You may have a floating input, change your pinMode to INPUT_PULLUP.
How is your servo powered? From the Arduino is not usually a good answer.
sweep example working
wildbill:
change your pinMode to INPUT_PULLUP.
But make sure the switch goes from pin to ground, if it's not already.
not_a_noob:
But make sure the switch goes from pin to ground, if it's not already.
Like this?
Knutarn:
Like this?
That link is a whole page pf pics.
You want it like S3 in larryd's pic, reproduced below:
not_a_noob:
That link is a whole page pf pics.You want it like S3 in larryd's pic, reproduced below:
Dont need any resistor?
just D4 to gnd?
Well it needn't be D4, any pin.
If you look at that pic you will see the resistor inside the chip; it's turned on by the INPUT_PULLUP as wildbill said, in order to save messing around with an external resistor.
Also be aware that using a pullup means the pin is normally high, and the pin becomes low when the button is pressed.
not_a_noob:
Well it needn't be D4, any pin.If you look at that pic you will see the resistor inside the chip; it's turned on by the INPUT_PULLUP as wildbill said, in order to save messing around with an external resistor.
Also be aware that using a pullup means the pin is normally high, and the pin becomes low when the button is pressed.
Hmm I put wire from D4 to GND and not working ( I tried on my arduino mega)
Do I need too change som in the code too?
Did you remember to change "button" from 10 to 4, do the INPUT_PULLUP?
Post your code. I can test it, got a servo lurking here somewhere.
Here is my code now
#include <Servo.h>
Servo myservo;
const byte button = 4;
void setup()
{
myservo.attach(10);
pinMode(button, INPUT);
}
void loop()
{
if (digitalRead(button) == HIGH) {
myservo.write(0);
}
else {
myservo.write(90);
}
}
You didn't change to INPUT_PULLUP.
This works for me. Note that with INPUT_PULLUP, pressing the button is a LOW.
#include <Servo.h>
Servo myservo;
const byte button = 4;
void setup()
{
myservo.attach(3);
pinMode(button, INPUT_PULLUP); //button to ground
}
void loop()
{
if (digitalRead(button) == LOW) //LOW is pressed due to INPUT_PULLUP
{
myservo.write(0);
}
else
{
myservo.write(90);
}
}
not_a_noob:
You didn't change to INPUT_PULLUP.This works for me. Note that with INPUT_PULLUP, pressing the button is a LOW.
#include <Servo.h>
Servo myservo;
const byte button = 4;
void setup()
{
myservo.attach(3);
pinMode(button, INPUT_PULLUP); //button to ground
}
void loop()
{
if (digitalRead(button) == LOW) //LOW is pressed due to INPUT_PULLUP
{
myservo.write(0);
}
else
{
myservo.write(90);
}
}
hmm tryet now, and not working :o
Mega arduino
D4 to GND
GND to servo
5v to servo
D12 to servo
mybe the bord is broken?
The servo sweep kode working
Knutarn:
D12 to servo
Your last posted code attach()-ed the servo to 10, previous code used 3.
Double check your connections match the code.
not_a_noob:
Your last posted code attach()-ed the servo to 10, previous code used 3.Double check your connections match the code.
Yes, I just changed to test it
Mybe I broke the bord when I connect D4 to GND and not update the code
Knutarn:
Yes, I just changed to test itMybe I broke the bord when I connect D4 to GND and not update the code
'
Now is working!
Is bad connector on the cabel!
Going too add relay.
Is this code ok?
#include <Servo.h>
Servo myservo;
const byte button = 12;
#define CH3 6 // Connect Digital Pin 7 on Arduino to CH3 on Relay Module
void setup()
{
myservo.attach(3);
pinMode(button, INPUT_PULLUP); //button to ground
pinMode(CH3, OUTPUT);
//Turn OFF any power to the Relay channels
digitalWrite(CH3,LOW);
delay(2000); //Wait 2 seconds before starting sequence
}
void loop()
{
if (digitalRead(button) == LOW) //LOW is pressed due to INPUT_PULLUP
{
myservo.write(0);
digitalWrite(CH3,LOW); //Relay 3 switches to NC
}
else
{
myservo.write(160);
delay(1000);
digitalWrite(CH3, HIGH); //Relay 3 switches to NO
}
}
Knutarn:
Going too add relay.Is this code ok?
Nobody's going to answer that one without details of the relay and your wiring and your intentions.