I am new to coding an arduino. Ive tried to learn the coding and wiring but i wont get it to work.
I will try to provide you with all the info.
Im using arduino UNO. Its connected to my laptop.
Im using a sg 90 micro servo 9g and a WL potentiometer.
The program should work like this:
I am trying to make a program that lets a servo sweep between start and end position.
By default the start position is 0 degrees and the end position 180 degrees.
When you press the setting button the program stops and the servo goes to the position that the potentiometer supplies. With 2 separate buttons you can save a new position to either start or end position.
then when you release the button the sweep starts again but then with the new values(if changed).
The servo moves like it should when i use just the potentiometer code, or the sweep code individually. Yet when i want to set the values with the buttons and potmeter it wont work and only the sweep keeps looping.
Can anyone explain (in an understandable way) what i am doing wrong?
It would really help me out. Thank you in advance.
#include <Servo.h> // add servo library
#include <Button.h> // add button library
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
const byte startpospin = 10; //pin for button to set start position
const byte endpospin = 11; //pin for button to set end position
const byte settingspin = 12; //pin for button to go into setting mode
int btnstatestart = 0;
int btnstateend = 0;
int endpos = 180; //sets temperatry value for end position
int startpos = 0; //sets temperary value for start position
int led = 13; // led pin to test button activity
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(startpospin, INPUT);
pinMode(endpospin, INPUT);
pinMode(settingspin, INPUT);
pinMode(led, INPUT);
}
void loop()
{
if (settingspin == HIGH) // enter mode wehre you can save positions
{
digitalWrite(led, HIGH); // checking button
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
if (btnstatestart == HIGH)
{ // if button is pressed save following value to startpos
startpos = analogRead(potpin);
startpos = map(startpos, 0, 1023, 0, 180); //changing pot readings to angles from 0 to 180 degrees
}
if (btnstateend == HIGH)
{ // if button is pressed save following value to endpos
endpos = analogRead(potpin);
endpos = map(endpos, 0, 1023, 0, 180); //changing pot readings to angles from 0 to 180 degrees
}
}
else
{
myservo.write(startpos); // normal sweep for servo, go to start pos
delay(1000); // 1 sec delay
myservo.write(endpos); // normal sweep for servo, go to end pos
delay(1000); // 1 sec delay
}
}
Thank you for the fast reply. I see the problem and i somewhat fixed it. I changed it to "if (digitalRead(x) == HIGH)."
Now when i press the setting button it stops the servo and allows me to manually change the servos position. Yet when i press the save end- or startposition button the servo just stops and nothing happends from there. Also when i release the setting button while positioning the servo it saves the position even though i didnt hit the save button. The settingbutton doesnt always work either.
If you could help me with this i would be really gratefull.
this is the current code:
#include <Servo.h> // add servo library
#include <Button.h> // add button library
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
const byte startpospin = 10; //pin for button to set start position
const byte endpospin = 11; //pin for button to set end position
const byte settingspin = 12; //pin for button to go into setting mode
int btnstatestart = 0;
int btnstateend = 0;
int endpos = 180; //sets temperatry value for end position
int startpos = 0; //sets temperary value for start position
int led = 13; // led pin to test button activity
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(startpospin, INPUT);
pinMode(endpospin, INPUT);
pinMode(settingspin, INPUT);
pinMode(led, INPUT);
}
void loop()
{
if (digitalRead(12) == HIGH) // enter mode wehre you can save positions
{
digitalWrite(led, HIGH); // checking button
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
if (digitalRead(11) == HIGH){ // if button is pressed save following value to startpos
startpos = analogRead(potpin);
startpos = map(startpos, 0, 1023, 0, 180); //changing pot readings to angles from 0 to 180 degrees
}
if (digitalRead(10) == HIGH){ // if button is pressed save following value to endpos
endpos = analogRead(potpin);
endpos = map(endpos, 0, 1023, 0, 180); //changing pot readings to angles from 0 to 180 degrees
}
}
else{
myservo.write(startpos); // normal sweep for servo, go to start pos
delay(3000); // 1 sec delay
myservo.write(endpos); // normal sweep for servo, go to end pos
delay(3000); // 1 sec delay
}
}
Do you have pulldown resistors in place or are the inputs floating at an unknown, possible HIGH, value when the buttons are not pressed ?
im sorry but i dont exactly know in what catogory my wiring falls. I would guess a pulldown resistor.
It is exactly wired as shown in the picture above. Im using a Tact switch button if that helps.
The servos should not be powered from the Arduino 5V pin as they could take too much current but it is very unlikely that you can power the servos from 9V. You need to check their specification.
If the 9V battery is the type that is used in smoke detectors (PP3) then it will not last very long even powering the Arduino alone. Consider using 4AA batteries in series to power the servos and connect a common GND to both the Arduino and the batteries
teun-lll:
im sorry but i dont exactly know in what catogory my wiring falls. I would guess a pulldown resistor.
It is exactly wired as shown in the picture above. Im using a Tact switch button if that helps.
The picture shows pulldown resistors. Learn about the Arduino internal pullups and save yourself a lot of wiring.
I am certain you did not wire it that way. It shows two wires in one breadboard hole in 2 places.
The orientation of the tact switches is critical for that wiring diagram to work. It should be difficult to put them in the wrong way, but easier than putting two wires in one hole. The smartest way to wire tact switches is only use two pins and only a diagonal pair. Then orientation does not matter.
The code has errors but nothing stands out as the cause of the incorrect behaviors you identified. (Don't write to an input pin.)
UKHeliBob:
The servos should not be powered from the Arduino 5V pin as they could take too much current but it is very unlikely that you can power the servos from 9V. You need to check their specification.
If the 9V battery is the type that is used in smoke detectors (PP3) then it will not last very long even powering the Arduino alone. Consider using 4AA batteries in series to power the servos and connect a common GND to both the Arduino and the batteries
thanks alot for an answer. I am able to power the servo with the arduino for now though.
I found the solution and im actually retarded:
i didnt know i had to connect the left side of the breadboard to the right side, plus the pushbuttons where connected wrong and not completely pushed in the breadboard, so it gave random highs all the time...
well the more you know i guess..