Notifications
Clear all

switch command vs many IFs

23 Posts
3 Users
3 Likes
1,340 Views
(@tperry724)
Member
Joined: 4 years ago
Posts: 33
Topic starter  

@zoolandermicro  Been looking at your color blender.  Why the value of 765 (255 x 3)?  Just trying to learn.  And, when the count increases, why do the LEDs get dimmer?  Thanks.  Tony


   
ReplyQuote
ZoolanderMicro
(@zoolandermicro)
Member
Joined: 4 years ago
Posts: 144
 

Yes indeed, you got it. Each case is assigning values between 0 and 255 to the color variables (redVal, grnVal, bluVal), then program uses the analogWrite() function to control the output using PWM. This code uses very little program memory and only requires three pins on the microcontroller that can output a PWM signal. The algorithm works well with addressable LEDs like NeoPixels. It can also be loaded in prog mem of an ATtiny25 (T25). The T25 has three PWM pins (physical 3, 5, 6) that you can name as 4 or A2, 0, and 1 in code for Arduino. I will make a short video of the code in action using a proto board that I made for T25 chips. It is a nice color blend that remains at about the same level of luminescence. As one color fades, another color brightens.    

ZoolanderMicro, where small ideas are a big deal


   
ReplyQuote
ZoolanderMicro
(@zoolandermicro)
Member
Joined: 4 years ago
Posts: 144
 
Posted by: @tperry724

  why do the LEDs get dimmer?  

This sketch is for common anode RGB LEDs. A value of 0 is equal to LOW, but the common pin is connected to 5V. A pin in LOW is a path to ground, so the led lights brightly. A value of 255 equates to HIGH, so there is little or no potential difference in voltage between 5V and the PWM output. Interesting how that works. Common cathode RGB LEDs will work with this code, only the color blend goes in reverse order.   

ZoolanderMicro, where small ideas are a big deal


   
ReplyQuote
(@tperry724)
Member
Joined: 4 years ago
Posts: 33
Topic starter  

@zoolandermicro It makes so much sense when you explain it.  Thank you.  I learned something today.  


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 
Posted by: @zoolandermicro

Excellent! Thanks FrogandToad, I have not tried that before. Could Tperry724 use this to accept user input of arithmetic symbols (+, -, *, /)? 

switch (calc_operator) {
case '+':
result = myNumber1 + myNumber2; //if addition then add
break;

case '-':
result = myNumber1 - myNumber2; //if subtraction then subtract
break;

case '*':
result = myNumber1 * myNumber2; //if multiplication then multiply
break;

case '/':
result = myNumber1 / myNumber2; //if division then divide
break;

default:
Serial.println("That was not a valid operator.");
}

No worries, and yes, 100%!

Cheers.


   
tperry724 reacted
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@zoolandermicro

Posted by: @zoolandermicro

For the case statements, should he still use single quotes for a character? I wouldn't think so.

If using character symbols such as: '+', '-', '*', etc... then single quotes are required.

Posted by: @zoolandermicro

As in:

switch (calc_operator) {
case 43: // ASCII for + symbol
result = myNumber1 + myNumber2; //if addition then add
break;

[snip]

Using number representations like the above is also valid, just as is the following to show interchangeability:

  // Print out the numbers 0-9 represented as characters
  for(char idx = 48; idx <= 57; idx++) {
    Serial.print(idx); 
   }

  Serial.println();
  
  // Print out lowercase a-z represented as characters
  for(char idx = 97; idx <= 122; idx++) {
    Serial.print(idx); 
   }
   
  Serial.println();

  // Print out uppercase A-Z represented as characters
  for(char idx = 65; idx <= 90; idx++) {
    Serial.print(idx); 
   }

As we can see, this works because as I stated previously; char is also an integral data type.  A lot of functions make use of this feature to compare characters in strings for equality, case, etc.

Likewise, in the older days, embedded programmers used ASCII math to save program space, and to improve the speed and efficiency of their program code - For example:

Character '7' == ASCII (55)
Character '0' == ASCII (48)

Note: - Subtracting a '0' character from an ISO ASCII character, yields its ASCII integer value equivalent.

Therefore, because ASCII int(55) - ASCII int(48) == ASCII int(7) ...so to prove that we can now yield a real world integer, and that the math works out correctly, here's a couple examples that demonstrate this outcome:

  char Seven = '7';
  Serial.println((Seven - '0') * 2); 
  Serial.println((Seven - '0') * (Seven - '0'));

-- OUTPUT --

0123456789
abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
14
49

Hope this helps.

Cheers.


   
ReplyQuote
(@tperry724)
Member
Joined: 4 years ago
Posts: 33
Topic starter  

@frogandtoad - the switch/case works for my calculator when evaluating operators!


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@tperry724

Excellent... I'd be worried if it didn't 😀

Hopefully you now have a better understanding of how switch statements work.

Cheers.


   
tperry724 reacted
ReplyQuote
Page 2 / 2