Percentage indicator

Hey guys,

I've got these exercises for school: http://puu.sh/bB2ud/e35cd8ef13.pdf
I have finished the first 5, so exercise 6.
I have connected 6 LEDs at the ports 8-13 and a potmeter at A0.

This is the code I've got so far: http://puu.sh/bB2zq/bb497f2398.txt

The thing is, when I upload it to my arduino uno, the first four LEDs start burning up and the potmeter cant change that.
The fifth LED however is changable.

Can anyone help me out? why isn't my code working out?

Foitn

why isn't my code working out?

How many of those if statements are going to be true? How many should be true?

There should only be one that is true. So, you need to be using if/else if/else, not if/if/if.

What value are you reading from the potentiometer? We can't see what you see.

    brightness = maxbrightness * sensorValue / (waarde3 - waarde2);

waarde3 is 510. waarde2 is 340. The difference is 170. If the sensor value is between 340 and 510, you are multiplying maxbrightness by 2. Does that make sense? Multiplying (sensorValue - waarde2) might.

My idea is that when I turn the potmeter, the LEDs will do something different.
At all times, there should only one of the "if"statements be "true".
I've edited my code, so there is a smaller chance of bugs.
I've disabled the brightness, I just want the program to work first, once it's working I want to make it fancy.
this is the code I've got right now: http://puu.sh/bBbf5/b8cba12157.txt

When I turn the potmeter fully to the right, it has a value of 1023 and when it's fully turned left it has a value of 27 (this should be 0% on the percentage indicator, this is why below 30 I have disabled the LEDs, as you can see in the last part of the code).

As far as I can tell, right now there only can be one if statement be true, due to the fact that none of the values are the same in the different if statements.

Is it true that when I use and if statement that I do not really need the else statement?

At the moment, when I use the code I just gave you guys, this happens: http://puu.sh/bBctH/479b71888a.mp4
There are 3 states
At the lowest point, where, according to my code, all the LEDs should be turned off, the first 5 LEDs burn slightly.
In between the first 5 LEDs burn fully
At the highest point all the LEDs burn fully.
This is not suppost to happen :~

  if (sensorValue <= 170){
  }
  
  if ((sensorValue > 171) && (sensorValue <= 342)){
  }
  
  if ((sensorValue > 342) && (sensorValue <= 513)){

would make more sense (to me) as:

  if (sensorValue <= 170)
  {
  }
  
  else if (sensorValue <= 342)
  {
  }
  
  else if (sensorValue <= 513)
  {

If the value is 30, that is less than 170, so the first block is true and the rest of the if statements are NOT evaluated.
If the value is 300, that is not less than 170, so the next statement is evaluated. The value is less than 342, so the block of code is executed, and the rest of the if statements are not evaluated.

Is it true that when I use and if statement that I do not really need the else statement?

Yes. The thing is sometimes you don't want a bunch of it statements being evaluated unnecessarily. Suppose, for instance, you wanted to turn on one of 4 LEDs depending on the temperature obtained from a sensor. You could do:

int temp = GetTemp();
if(temp < 30)
{}
else if(temp < 40)
{}
else if(temp < 50)
{}
else
{}

Or, you could have:

if(GetTemp() < 30)
{}
else if(GetTemp() < 40)
{}
else if(GetTemp() < 50)
{}
else
{}

Now, the second code looks cleaner, since there is no wasted temporary variable. But, suppose that the temperature sensor takes 1 second to produce a result. The first example calls the function once. The second calls it 3 times, which means that it could be much slower. Now, imagine what would happen in your code structure with 6 compound if statements, if GetTemp() was called 11 times - 10 of which were unnecessary.

Which Arduino are you using? On the 328-based Arduinos, not all the pins you are using are capable of PWM.

That totally fixed my problem!
Thanks a lot!
I've learned a lot today thanks to you!