# programming help - block separation

Hi all,
I am a beginner with programming and unfortunately I need help with next issue:

1st - explanation of purpose - designed HW is on base Arduino uno and myself made “power shield” - board with resistors and 3 pcs. MOSFET transistos for driving of 50W power LED clusters - it meaning independent dimming (PWM outs) of each one from these 3 clusters (charged by 12V cross MOSFET transistor). Arduino program idea - By two inputs - for example In 2, 3 (in fact will be represented by buttons) it will driven one PWM out - for example Out 9. By pressing button 2 it will cause increasing brightness (by change of character PWM) on Out 9, By pressing button 3 it will cause decreasing brightness (by change of character PWM) on out 9. Well - this now working for 2 inputs and one output and I need it for another two outs - In fact - summary - as I wrote - 3 independent circuits contains 2+2+2 inputs and 1+1+1 PWM out - of course - each circuit must be controlled independent.

2nd - program problem - As I wrote - for one circuit it program working well - Lets see:

int brightness = 10;    // 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);
TCCR1B = TCCR1B & 0b11111000 | 0x03;    //change frequency on 9,10 value 0x05 lower to 0x1 higher
}

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

// set the brightness of pin 9:
analogWrite(9, brightness);
delay(200);    // wait for 200 milliseconds to see the dimming effect

So - because I am an “idealist” and I was suppose, than is enough to simply extend and rename list of in/outs and “loop” core and it will work - ehm… well - in fact - does not work as i like. In real it work little bit confused - Each from all 6 inputs control of all 3 outs. For example - by pressing button for controling input No2 (for increasing falue PWM on Output No 9) it automatically changed value also on other outs No 5,6.
I need somehow separate these three blocks in program to “independent blocks” that they will not take a affect to another one
In fact:

• by Inputs 2,3 I need control only Output 9
• by Inputs 7,8 I need control only Output 5
• by Inputs 12, 13 I need control only Output 6

here is my not working experiment - if you can anybody help me wit that… Many Thx.

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);
delay(100);

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
delay(100);
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
}
// declare pin 2(up) & 3(down) to be an output:
pinMode(2, INPUT);
pinMode(3, INPUT);

Is it the code or the comment that is wrong?

here is my not working experiment - if you can anybody help me wit that

You need three brightness values, not one.

This is the working code - this code was writern only for one circuit. In this version are active only two inputs (2, 3) and one output (9). I was put it on the screen as example - what is realy working - there are nothing wrong - I was tested it on the board and working very well- So, I was modified this code for summary three independent circuits (complete modified code is in code frame below - and there is something wrong and... I will like to know what... ) well - information "You need three brightness values, not one." is prety good, but - I do not know - how can I to do implement it on to the code...

Thx...

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

That's one brightness level for all three LEDs. It isn't exactly rocket science to copy this twice, appending 1, 2, and 3 to the names, for the 3 LEDs.

Yes, but - as I suppose - this is init instruction for initial LED brightness at system start. And of course - this one value have affect to all three LEDs outs, but it does not affect in "void loop" part where I need independent change value for each out.

Here You can see exactly HW for what is program determined. You can see three power MOSFETS transistors - driven from OUTs, information LEDs about OUTs status, power terminal and remote terminal for six (3x2) buttons , where will be connected button board. But - as I am wrote - program still does not work and I cant help my self... :-(

rexmundi: ... this one value have affect to all three LEDs outs, but it does not affect in "void loop" part where I need independent change value for each out.

Yes it does. I think Paul is right.

You need a separate brightness level in each of the sections, like this:-

int brightness1 = 5;    // how bright the LED 1 is
int brightness2 = 5;    // how bright the LED 2 is
int brightness3 = 5;    // how bright the LED 3 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(brightness1<255)
brightness1+=digitalRead(2);    //increase brightness level by 1 if up is pressed else do nothing
if(brightness1>0)
brightness1-=digitalRead(3);    //decrease brightness level by 1 if down is pressed else do nothing
analogWrite(9, brightness1);
delay(100);

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

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

However, I must say that this is the most weird backwards way of coding this. To use the value of a digital read as the increment / decrement value is not a good idea. The code is hurtling round doing not very much.
The real way to write it would be to look at the digital inputs and increment / decrement the brightness value according to what you see with if statements. Then hold the value to prevent it going out of range with a constrain statement.

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

And if they are both pressed?

Yes!!!... Pauil is right... :-) It must be there " int brightness(*)" with an index and corresponding index must be writern for appropriate brightness values in "Loop" section...

Many thanks for all...

HA! I am see right now - I did not refreshing screen and I was start investigation with Pauls recommendation and I reach the same solution as Grumpy and Nick recommend me latter… in this case - my previous comment it look in this context like from idiot… anyway - again many THX for all… good lock and have a nice day…

To Nick: If are pressed both buttons - nothing happen. Appropriate LED is in this case frozen at last state, but - this event can not in real come up. Inputs will primary manage from micro relays controlled by webserver and there theoretically can not come event both switch on relays and - secondary can be controlled by hidden button board, but only for an emergency case - if webserverl will not work and - of course - there can come up event of both pressed buttons, but - it does not matter - system will not collapse - only will not changed output value…