Greetings !
I have this switch i would like to wire it to my arduino to turn on a led when clicked with some programming
Is this possible ? if yes how to connect it
Most often you connect C to the input pin and NO connected to GND (0V).
Use:
pinMode(inputPin, INPUT_PULLUP); //a LOW read on the pin means the switch is down.
NO = Normally Open
NC = Normally Closed
(set the pin as INPUT_PULLUP)
It worked for me when i write the code in a new file and run it..
int buttonState = 0;
void setup() {
pinMode(7, INPUT_PULLUP);
pinMode(12, OUTPUT);
}
void loop() {
buttonState = digitalRead(7);
if (buttonState == HIGH) {
digitalWrite(12, HIGH);
} else if (buttonState == LOW) {
digitalWrite(12, LOW);
}
}
but when i add the same code in my project code it didn't work please can u tell me why
?
#include <Servo.h>
Servo myservo;
int pos = 0;
int buttonState = 0;
void setup() {
myservo.attach(9);
pinMode(12, OUTPUT); //led pin
pinMode(7, INPUT_PULLUP); // switch pin
}
void loop() {
buttonState = digitalRead(7);
if (buttonState == HIGH) {
digitalWrite(12, HIGH);
} else if (buttonState == LOW) {
digitalWrite(12, LOW);
}
for (pos = 0; pos <= 165; pos += 1) { /* goes from 0 degrees to 165 degrees in steps of 1 degree */
myservo.write(pos);
delay(25);
}
delay(3000);
for (pos = 165; pos >= 0; pos -= 1) { /* goes from 165 degrees to 0 degrees */
myservo.write(pos);
delay(25);
}
delay(3000);
}
When debugging code, add Serial print statements at strategic places to print variables to the serial monitor.
This allows you to check these variable values and confirm they are what you think they are.
“ it didn't work please can u tell me why”
Always tell us what didn’t work.
Like this ?
const int switch = 7;
then
pinMode(switch, INPUT_PULLUP);
If not please explain more because i still newbie
I men the switch didn't work with my project code
Still conveys no information that we can use to help you.
What is the code supposed to do? What does the code really do.
BTW
We hardly ever advise new people use delay( ) in their sketches.
I have a code to controll a servo motor this is my main project
i would like to program this switch to control a LED
but the switch didn't work in my main project
this is the code
#include <Servo.h>
Servo myservo;
int pos = 0;
int buttonState = 0;
void setup() {
myservo.attach(9);
pinMode(12, OUTPUT); //led pin
pinMode(7, INPUT_PULLUP); // switch pin
}
void loop() {
buttonState = digitalRead(7);
if (buttonState == HIGH) {
digitalWrite(12, HIGH);
} else if (buttonState == LOW) {
digitalWrite(12, LOW);
}
for (pos = 0; pos <= 165; pos += 1) { /* goes from 0 degrees to 165 degrees in steps of 1 degree */
myservo.write(pos);
delay(25);
}
delay(3000);
for (pos = 165; pos >= 0; pos -= 1) { /* goes from 165 degrees to 0 degrees */
myservo.write(pos);
delay(25);
}
delay(3000);
}
so how can i fix it ?
The code is suck in the delays in the for loops for 11.25 seconds. The switch is not read during that time. Like @LarryD says, do not use delays. Nothing can happen during a delay call. In order to be read as low the switch must be closed during the time between the for loops which is very brief. Hold the switch closed long enough and it will work.
how to do timing with millis():
Several things at a time.
Beginner's guide to millis().
Blink without delay().
@oussy29, your topic has been moved to a more suitable location on the forum as it's unclear how this relates Interfacing w/ Software on the Computer
I tried other code to control servo sweep without delay and a led with a switch but when i click the switch the led turns on only when the servo is rotating.. any help ?
#include <Servo.h>
int buttonState = 0;
class Sweeper
{
Servo servo; // the servo
int pos; // current servo position
int increment; // increment to move for each interval
int updateInterval; // interval between updates
unsigned long lastUpdate; // last update of position
public:
Sweeper(int interval)
{
updateInterval = interval;
increment = 1;
}
void Attach(int pin)
{
servo.attach(pin);
}
void Detach()
{
servo.detach();
}
void Update()
{
if((millis() - lastUpdate) > updateInterval) // time to update
{
lastUpdate = millis();
pos += increment;
servo.write(pos);
Serial.println(pos);
if ((pos >= 180) || (pos <= 0)) // end of sweep
{
delay(5000);
// reverse direction
increment = -increment;
}
}
}
};
Sweeper sweeper1(20);
void setup()
{
Serial.begin(9600);
sweeper1.Attach(9);
pinMode(7, INPUT_PULLUP);
pinMode(12, OUTPUT);
}
void loop()
{
sweeper1.Update();
buttonState = digitalRead(7);
if (buttonState == HIGH) {
digitalWrite(12, HIGH);
} else {
digitalWrite(12, LOW);
}
}
may be because when the servo is not rotating your code is stuck in this delay for 5s
so you are not going back to the loop where you test the button.
➜ get rid of all delays (that are more than a few ms) if you want no perceived lag in user interaction. The loop needs to run fast and never be stopped or blocked somewhere
You need to modify the update() method so that it also knows when to pause for a while in the same way you used millis() and lastUpdate. This could be done by smartly modifying updateInterval for example (but keeping a copy of the original value)
i want to make 4h delay between 0 to180 and 180 ==> 0
and i want to keep the led on during the same delay once the switch is clicked...
i didn't get how to do it.. i just get this code and made some modifications as i know because i sill beginer
I suggest that you look into using a finite state machine. States could be "move CW", "move CCW" and "wait 5 seconds". Each state would have its own millis() timer (like the several things at a time example).
Run the servo state machine in parallel with reading and acting on the switch.
For more info just google "arduino state machine",
This will complicate it more for me because i still beginner
i am thinking about doing it both with millis()
Learning to use millis() for timing and how to do a state machine are among the most important things for a beginner to learn.
opportunity to learn! that's how you'll no longer be a beginner
do u recommend any video from youtube or mp4 course to teach this ?