const int ledPin = 12;
const int buttonPin = 2;
int buttonState = 0;
void setup() {
Serial.begin (9600);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
// designate pin 13 as an OUTPUT pin for the LED
pinMode (13, OUTPUT);
// designate pin A0 as an Analog INPUT for the potentiometer
pinMode (A0, INPUT);
Serial.begin(9600);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
Serial.println("LED ON +++++++++++");
}
else{
digitalWrite(ledPin, LOW);
Serial.println("LED OFF ---------");
}
Serial.println (A0);
int potValue = analogRead (A0);
digitalWrite (13, HIGH);
delay (potValue);
digitalWrite (13, LOW);
delay (potValue);
}
Im new to this!
So I want one LED to be controlled via a potentiometer, which works well.
The other LED is to be turned on/off with a button. However, it's behaving strangely - sometimes it turns on for a while, sometimes it doesn't respond to button being pressed. I know it has something to do with my code but i have no idea how to solve it.
PaulRB:
Hi and welcome to the forum. +1 Karma for using code tags the first time you posted code.
How do you know that? You seem very certain. My guess is not the code.
Thank you for the welcome!
I am not certain it is my code...
I have the both LED's and buttons on a breadboard wired to the same GND/5V.
How exactly is the button wired? Do you have a pull-up or pull-down resistor on the pin it’s connected to or is it just left floating when the button isn’t pressed?
slipstick:
How exactly is the button wired? Do you have a pull-up or pull-down resistor on the pin it's connected to or is it just left floating when the button isn't pressed?
Steve
Button is wired with a 10k resistor to the GND side.
dougp:
Is this behavior affected in any way by the pot setting?
So yes the LED2(connected to button) responds well when pot is blinking LED1 at high rate. When the pot is set lower so LED1 is blinking slowly LED2 is responsive only when LED1 is on.
PaulRB:
5V and GND are not the same, which is it?So the button and 10K are both wired to GND? You could be clearer this.
Button is connected on one side to 5V and other side to GND. Button and 10k both wired to ground yes.
lovehouseparadise:
So yes the LED2(connected to button) responds well when pot is blinking LED1 at high rate. When the pot is set lower so LED1 is blinking slowly LED2 is responsive only when LED1 is on.
Ahh, right. I think you'll find LED2 changes only when LED1 changes. If LED1 blinks slowly, LED2 seems unresponsive. It only responds at the instant LED1 switches off. You need to learn & understand the "blink without delay" principle.
lovehouseparadise:
Button is connected on one side to 5V and other side to GND. Button and 10k both wired to ground yes.
Really? That will cause a damaging short circuit when you press the button... My point here is, every time you try to explain how you wired everything, you make a another mistake. This isn't because you are not good at English. It's because English is bad at explaining circuits.
//potentiometer
int potentiometer = A0;
int potRead = 0;
int redLED = 13;
int isOn = 1;
long now = 0;
//button
byte led2 = 10;
byte button2 = 7;
void setup() {
// set the digital pin as output:
//potentiometer
pinMode(redLED,OUTPUT);
pinMode(potentiometer,INPUT);
digitalWrite(redLED,HIGH);
// button
pinMode (led2, OUTPUT); // pin to LED anode, LED cathode to resistor, resistor to Gnd
pinMode (button2, INPUT_PULLUP); // button connects pin to Gnd when pressed
} // end setup
void loop()
{
// blink the LED.
potRead = analogRead(potentiometer);
if(millis() > now + potRead){
digitalWrite(redLED,isOn);
isOn = 1 - isOn;
now = millis();
if (digitalRead(button2) == LOW) {
digitalWrite (led2, HIGH);
}
else {
digitalWrite (led2, LOW);
}
}
} // end loop
If you use Auto-Format, this will fix the indentation of your code. That doesn't change how the code works at all, but makes it easier to spot this kind of mistake.
PaulRB:
Yup, because you got a } in the wrong place.
If you use Auto-Format, this will fix the indentation of your code. That doesn't change how the code works at all, but makes it easier to spot this kind of mistake.
So apologies for my noob understanding. However, now the led only blinks super fast at the highest potentiometer level. When turned slightly down it just switches on with no blinking... (sometimes even just turns off completely..)
//potentiometer
int potentiometer = A0;
int potRead = 0;
int redLED = 13;
int isOn = 1;
long now = 0;
//button
byte led2 = 10;
byte button2 = 7;
void setup() {
// set the digital pin as output:
//potentiometer
pinMode(redLED, OUTPUT);
pinMode(potentiometer, INPUT);
digitalWrite(redLED, HIGH);
// button
pinMode (led2, OUTPUT); // pin to LED anode, LED cathode to resistor, resistor to Gnd
pinMode (button2, INPUT_PULLUP); // button connects pin to Gnd when pressed
} // end setup
void loop()
{
// blink the LED.
potRead = analogRead(potentiometer);
if (millis() > now + potRead)
digitalWrite(redLED, isOn);
isOn = 1 - isOn;
now = millis();
if (digitalRead(button2) == LOW) {
digitalWrite (led2, HIGH);
}
else {
digitalWrite (led2, LOW);
}
}