PWM regulation outs...

Hi everyone, at 1st - sorry for a may be stupid question (for most of yours) I am a Arduino beginner without any experiences with programming anyway I have a lot of experiences with HW. Now I looking for a wery simple solution for a home LEDs ligting regulation (ON/Off/Dimming and more in future - of corse :-) ) I was ordered Arduino UNO R3 and now I need to found software solution to control PWM outs. My idea is control PWM out by using two Digital Inputs (UP/DWN buttons)

Practice run: System status - PWM out at 0% Pressing UP button PWM out will smoothly increasing value of PWM level releasing UP button PWM out will stay on last level (for example on 65% ) Pressing DWN button PWM out will smoothly deceasing value of PWM level releasing DWN button PWM out will stay on last level (for example on 25% )

Conditions: No of PWM steps levels: aprox 50 to 250 steps (more is better for smooth dimming - are not vissible dim steps in lowest light levels ) The speed of change output PWM level increasing/decreasing from 0% to 100% - cca 10sec.

If anybody can help me with SW program solution - I will be happy. Many THX for appropriate help and sorry for my english

Nice day

Hi! Here is a basic code based on what you want. You can add more stuff like switch debouncing. Here are some links you can refer as well:

int brightness = 50;    // how bright the LED is

void setup()  { 
  // declare pin 9 to be an output:
  pinMode(9, OUTPUT);
  // declare pin 2(up) & 3(down) to be an output:
  pinMode(2, INPUT);
  pinMode(3, INPUT);
} 

void loop()  { 
  if(brightness<256)
    brightness+=digitalRead(2);    //increase brightness level by 1 if up is pressed else do nothing
  if(brightness>50)
    brightness-=digitalRead(3);    //decrease brightness level by 1 if down is pressed else do nothing

  // set the brightness of pin 9:
  analogWrite(9, brightness);    
}

Antzy:

    if(brightness<256)

brightness+=digitalRead(2);    //increase brightness level by 1 if up is pressed else do nothing

oops… a logical error. its should be:

    if(brightness<255)
    brightness+=digitalRead(2);    //increase brightness level by 1 if up is pressed else do nothing

since digitalRead takes values from 0-255…

Uhááá.... 8) Its look exactly what I needed... many THX gents...

Nice day

since digitalRead takes values from 0-255...

No. digitalRead() returns either HIGH or LOW.

The analogWrite() function takes values from 0 to 255.

another typo... I meant analogWrite :P

uhhh “brightness<255” thats saying LESS THAN 255 so the max value would be 254. <256 the max value would be 255.

sirbow2:
uhhh “brightness<255” thats saying LESS THAN 255 so the max value would be 254. <256 the max value would be 255.

thats what I thought initially but after reading it again, the golden login revealed itself :stuck_out_tongue: :

Antzy:

  if(brightness<256)

brightness+=digitalRead(2);    //increase brightness level by 1 if up is pressed else do nothing

  // set the brightness of pin 9:
  analogWrite(9, brightness);

read the whole thing. Lets look at the original incorrect code…
Assume current value of brightness is 255. It comes to if condition. The condition is evaluated to true since 255<256. It increases brightness to 256 and sets it using analogWrite. So max goes to 256. Thats why we used we use if(brightness<255).

ahh i see, +=1 or 0 depending on the state. didnt see that before.

Many thx for all. As I think about posibilities of Ardunio board - I have an idea to use also other OUTs to drive next two circuits for power LED drivingI. have a finished shield with three power MOSFET transistors driven from OUT 5,6,9 and changing values of PWM are from pins 2,3, 7,8, 12, 13.
I were simple try to modify code from Antzy for aditional managing of Inputs and Outpust by methods of 3 time clone of base sequence and changing of Ins and Outs numbers (see attached code), but result is, then Is pressed any from Input butons are changed values in all Outputs simultaneously. for example - by pressing button on pin 2 it should by a increasing value on output 9, but - in fact are changed values in all outputs and inceasing value is recorded also on pins 5 and 6… in code is probably to use any separation of all thee “circuits” then they work independent
I will like to ask You again for a help to change a microprocessor code - if it is possible.
I hope, than I explained my problem clearly for undrestanding… :slight_smile:

Many thanks for help gents…
Here is my not working code modification:

int brightness = 5; // how bright the LED is

void setup() {

pinMode(9, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(7, INPUT);
pinMode(8, INPUT);
pinMode(12, INPUT);
pinMode(13, INPUT);
}

void loop() {
if(brightness<255)
brightness+=digitalRead(2); //increase brightness level by 1 if up is pressed else do nothing
if(brightness>0)
brightness-=digitalRead(3); //decrease brightness level by 1 if down is pressed else do nothing
analogWrite(9, brightness);

if(brightness<255)
brightness+=digitalRead(7); //increase brightness level by 1 if up is pressed else do nothing
if(brightness>0)
brightness-=digitalRead(8); //decrease brightness level by 1 if down is pressed else do nothing
analogWrite(5, brightness);

if(brightness<255)
brightness+=digitalRead(12); //increase brightness level by 1 if up is pressed else do nothing
if(brightness>0)
brightness-=digitalRead(13); //decrease brightness level by 1 if down is pressed else do nothing
analogWrite(6, brightness);
delay(100); // wait for 100 milliseconds to see the dimming effect
}

Hey, glad my code worked out... :sweat_smile:

The problem you are facing right now is that you are setting the same brightness for all inputs and LED outputs. just go through the code in your head and try to visualize how the brightness value works... Take the case of a single LED... It stores a SINGLE value between 0 to 255 which is increased by 1 on pressing the up button and decreased by 1 on pressing the down button. If it was 50 in the last loop and you are pressing up button, it will become 51 in the next loop. And then you set the brightness of LED according to this value!

Now in the case of 3 LEDs, what your code is doing is changing the SAME brightness variable for all LEDs. Assume you have the up for 1st LED pressed, up for 2nd LED and 3rd LED buttons are untouched. Also say the value of brightness is 50. The code will first check the buttons for 1st LEDs and increase brightness value to 51. Then display LED1 at 51 brightness. It will then again increase the SAME brightness variable by 1, so changing it to 52 and then displaying it for LED2. Then for LED3 buttons, it will not change them and set [u]LED3 to the value of brightness, i.e. 52[/u]! In the next loop, LED1 will be at 53, LED2 at 54 and LED3 also at 54 and so on... So you see, the problem lies in using the same variable for brightness value for all 3 LEDs... If you still have some doubts or facing problems, I can make that code as well if you like. I