So i am quite new to the programming language, and i am trying to create this simple project. The project contains 2 LEDs, 2 buttons, and a potentiometer. The 2 LEDs should react to both the buttons and the potentiometer. If one button is pressed, both LEDs goes to HIGH. If the other is pressed, the start blinking, switching from high to low. Now what i can't get working is the potentiometer part. I want to adjust the brightness of the LEDs when using the potentiometer. When using the potentiometer, and changing the value to 0-255. The LEDS turns to LOW if the potentiometer is under 127 (HALF) and is HIGH if above. Any ideas what could be wrong?
CODE
const byte potPin = A5;
const byte greenPin = 5;
const byte redPin = 6;
byte potvalue;
int onoffval;
int onoffPin = 9;
int counter;
int BlinkPin = 13;
int Blinkval;
int Bcounter;
void setup() {
// put your setup code here, to run once:
pinMode(greenPin, OUTPUT);
pinMode(redPin,OUTPUT);
pinMode(onoffPin, INPUT);
pinMode(BlinkPin, INPUT);
pinMode(potPin, INPUT);
digitalWrite(greenPin, LOW);
digitalWrite(redPin, LOW);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
potvalue = analogRead(A5);
Blinkval = digitalRead(BlinkPin);
if (Blinkval == LOW)
{
Bcounter++;
delay(200);
}
if (Bcounter > 1)
{
Bcounter = 0;
}
if(Bcounter == 0)
{
digitalWrite(greenPin,LOW);
digitalWrite(redPin,LOW);
}
if (Bcounter == 1)
{
digitalWrite(greenPin,HIGH);
delay(500);
digitalWrite(redPin,LOW);
digitalWrite(greenPin,HIGH);
delay(500);
digitalWrite(redPin,LOW);
}
onoffval = digitalRead(onoffPin);
if (onoffval == LOW)
{
counter++;
delay(200);
}
if (counter > 1)
{
counter = 0;
}
if (counter > 0)
{
digitalWrite(greenPin, HIGH);
digitalWrite(redPin, HIGH);
}
if (counter == 0)
{
digitalWrite(greenPin, LOW);
digitalWrite(redPin, LOW);
}
if(Bcounter && counter >= 1)
{
Bcounter = 0;
counter = 0;
}
analogWrite(greenPin, potvalue);
analogWrite(redPin, potvalue);
Serial.println(potvalue);
delay(15);
}
Please edit you post to include code tags. See How to use the forum.
And you can only dim led’s which are connected to PWm pin. 8 and 12 are not
Also some tips:
Use variables (const byte) for pins as well. That’s a lot more readable then digitalWrite(12, LOW); etc.
delay() will block EVERYTHING. Also reading the buttons. So if you want responsive scrap the delay() (and never ever use it again and study Blink without delay
septillion:
Please edit you post to include code tags. See How to use the forum.
And you can only dim led's which are connected to PWm pin. 8 and 12 are not
Also some tips:
Use variables (const byte) for pins as well. That's a lot more readable then digitalWrite(12, LOW); etc.
delay() will block EVERYTHING. Also reading the buttons. So if you want responsive scrap the delay() (and never ever use it again and study Blink without delay
Oh yeah, i totally forgot about the PWm pins.
I am using delay, because the button could react multiple times otherwise (Bouncing).
How would you set up the const byte?
Anyway thanks for the quick respones, i'll try to make some few changes.
PaulS:
If you want the potentiometer value to control the brightness of the LED(s), why are you always using digitalWrite()?
Do you know which pins analogWrite() works on, if you hadn't commented it out?
I forgot about which pins analogWrite worked on. What could be used instead of digitalWrite? Another person mentioned const byte, but i don't have the big idea of how to set that up.
UKHeliBob: analogWrite() but as you already seem to know about it I don't understand the question.
Declare variables with sensible names for the pins as const byte rather than int.
const because the value will never change and byte because the value is between 0 and 255 so will fit into 1 byte
Okay, didn't know you could use analogWrite when "talking" to the LEDs, thought it had to be digital.
I'll also try to edit to const byte.
Meanwhile.
I changed the pins to 5 and 6, and now i can adjust the brightness with the potentiometer. Now that the potentiometer is working, ofcourse the buttons doesn't work as intended. Now the lights are on HIGH all the time, unless the potentiometer is turned down, or for a split second when pressing one button. And i can't change the brightness when they are in blink mode.
Say for example you want to change it to pin 11 instead all you need to edit is:
const byte LedPin = 13;
instead of sifting through the code to change 13 into 11 in all places
And about buttons, debounce is fine. But there are other ways to do timing in a non blocking way. The most practical on an Arduino is millis(). And you also use delay() for the blink and with 200ms delay an Arduino Uno could have done 3,2 million calculations
And for the button, if you want easy, grab a library like Bounce2 which does all the heavy lifting for you
No it's not the easiest way to do it and yes, it uses delay. It's just to show it in a simple matter.
const byte LedPin = 13;In the context of this thread I have often thought it a shame that the built in LED was not connected to a pin capable of providing PWM output.