Code checking before programming

Hey all,

I havn't played around in a while but I think I still remember the basics. Basically I'm building a common cathode RGB that is controlled by three encoders, each red, green, blue. Teaching my 4 year old the primary colors. (Leonardo Board, CTS 288 encoders, and a three common cathode RGB LEDs on a bus strip)

I believe I have the setup correct but if one of the amazing people here can check it over for me that would be greatly appreciated. Remembering the coding is going to be the hard part, but at least I want to make sure my roots that i'm building the code on are good.

int brightnessRed = 120; //Starting at half the value of full brightness
int brightnessGreen = 120;//Starting at half the value of full brightness
int brightnessBlue = 120;//Starting at half the value of full brightness
int fadeAmount = 5;  //incremental increase or decrease in LED brightness
unsigned long currentTime;
unsigned long loopTime;
const int pin_A1 = 2;//A pin red encoder
const int pin_B1 = 3;//B pin red encoder
const int pin_A2 = 4;//A pin green encoder
const int pin_B2 = 5;//B pin green encoder
const int pin_A3 = 6;//A pin blue encoder
const int pin_B3 = 7;//B pin Blue encoder
const int LED_Red = 11;//Has to be set as output
const int LED_Green = 10;//Has to be set as output
const int LED_Blue = 9;//Has to be set as output
unsigned char encoder_A1;
unsigned char encoder_B1;
unsigned char encoder_A2;
unsigned char encoder_B2;
unsigned char encoder_A3;
unsigned char encoder_B3;
unsigned char encoder_A1_prev=0;
unsigned char encoder_A2_prev=0;
unsigned char encoder_A3_prev=0;

void setup()
{
  Serial.begin(9600); //Can add Serial.println later to each section
  pinMode(LED_Red, OUTPUT);
  pinMode(LED_Green, OUTPUT);
  pinMode(LED_Blue, OUTPUT);
  pinMode(pin_A1, INPUT);
  pinMode(pin_B1, INPUT);
  pinMode(pin_A2, INPUT);
  pinMode(pin_B2, INPUT);
  pinMode(pin_A3, INPUT);
  pinMode(pin_B3, INPUT);
  currentTime = millis();
  loopTime = currentTime;
}
}

unsigned char encoder_A1;
Could be
byte encoder_A1;

const int pin_A1 = 2;//A pin red encoder
Could be
const byte pin_A1 = 2;//A pin red encoder

int brightnessRed = 120; //Starting at half the value of full brightness
Could be
byte brightnessRed = 120; //Starting at half the value of full brightness

Depending on the wiring, you may be able to turn on pull-ups.

You will of course need a loop() function.

You need to get the better half buy your child a soldering iron, DVM, hand tools, drill etc.

Overall, you could drastically shorten & simplify your code with arrays.
Also remember LEDs are not linear, you may want a table in PROGMEM to map your brightness value to PWM value.

lastchancename:
Overall, you could drastically shorten & simplify your code with arrays.
Also remember LEDs are not linear, you may want a table in PROGMEM to map your brightness value to PWM value.

larryd:
unsigned char encoder_A1;
Could be
byte encoder_A1;

const int pin_A1 = 2;//A pin red encoder
Could be
const byte pin_A1 = 2;//A pin red encoder

int brightnessRed = 120; //Starting at half the value of full brightness
Could be
byte brightnessRed = 120; //Starting at half the value of full brightness

Depending on the wiring, you may be able to turn on pull-ups.

You will of course need a loop() function.

You need to get the better half buy your child a soldering iron, DVM, hand tools, drill etc.

Both are excellent suggestions. As I don't know how to write arrays I'm probably going to leave that one alone. For simplicity purposes anyway. I will have to research the PROGMEM commands as that is above my head right now, but certainly worth looking into.

I do have a build station with soldering iron and all, but my daughter is 4, so she wont be handling any of that. Just figured I would build her a toy that can teach colors.

I'll be putting together the loop function tonight but needed to make sure what I have so far is a solid foundation (for a beginner anyway)

I appreciate the help

Maybe have a potentiometer the can be adjusted to create a 'divisor'.

It would be used to divide into all three: R, G and B values to set brightness/intensity.

larryd:
unsigned char encoder_A1;
Could be
byte encoder_A1;

const int pin_A1 = 2;//A pin red encoder
Could be
const byte pin_A1 = 2;//A pin red encoder

int brightnessRed = 120; //Starting at half the value of full brightness
Could be
byte brightnessRed = 120; //Starting at half the value of full brightness

Depending on the wiring, you may be able to turn on pull-ups.

You will of course need a loop() function.

You need to get the better half buy your child a soldering iron, DVM, hand tools, drill etc.

So if I understand correctly in functionality of the project, changing it to this...

int brightnessRed = 120; //Starting at half the value of full brightness
int brightnessGreen = 120;//Starting at half the value of full brightness
int brightnessBlue = 120;//Starting at half the value of full brightness
int fadeAmount = 5;  //incremental increase or decrease in LED brightness
unsigned long currentTime;
unsigned long loopTime;
const byte pin_A1 = 2;//A pin red encoder
const byte pin_B1 = 3;//B pin red encoder
const byte pin_A2 = 4;//A pin green encoder
const byte pin_B2 = 5;//B pin green encoder
const byte pin_A3 = 6;//A pin blue encoder
const byte pin_B3 = 7;//B pin Blue encoder
const byte LED_Red = 11;//Has to be set as output
const byte LED_Green = 10;//Has to be set as output
const byte LED_Blue = 9;//Has to be set as output
byte encoder_A1;
byte encoder_B1;
byte encoder_A2;
byte encoder_B2;
byte encoder_A3;
byte encoder_B3;
unsigned char encoder_A1_prev=0;
unsigned char encoder_A2_prev=0;
unsigned char encoder_A3_prev=0;

void setup()
{
  Serial.begin(9600); //Can add Serial.println later to each section
  pinMode(LED_Red, OUTPUT);
  pinMode(LED_Green, OUTPUT);
  pinMode(LED_Blue, OUTPUT);
  pinMode(pin_A1, INPUT);
  pinMode(pin_B1, INPUT);
  pinMode(pin_A2, INPUT);
  pinMode(pin_B2, INPUT);
  pinMode(pin_A3, INPUT);
  pinMode(pin_B3, INPUT);
  currentTime = millis();
  loopTime = currentTime;
}

will keep the 8 bytes of information needed and not load up like a const int will.

In terms of this part of the code:

unsigned char encoder_A1_prev=0;
unsigned char encoder_A2_prev=0;
unsigned char encoder_A3_prev=0;

Does it have to remain an unsigned char since the event of the encoder moving forward or backward has already happened and is just being compared to?

An 'unsigned char' is a 'byte'.

Since 'brightnessRed' will not be larger than 255, this could also be 'byte'

Keeping a variable's 'type' small helps save RAM, however, I suppose in this small project RAM usage will not be a problem.

It’s just good practise.

'byte' and 'char' one byte
'int' two bytes.
'long' four bytes

BTW
pinMode(pin_A1, INPUT); <———<<<< at power up, inputs are already set to INPUT

larryd:
An 'unsigned char' is a 'byte'.

Since 'brightnessRed' will not be larger than 255, this could also be 'byte'

Keeping a variable's 'type' small helps save RAM, however, I suppose in this small project RAM usage will not be a problem.

It’s just good practise.

'byte' and 'char' one byte
'int' two bytes.
'long' four bytes

BTW
pinMode(pin_A1, INPUT); <———<<<< at power up, inputs are already set to INPUT

Great to know! Thank you for the help.

So basically all of this,

  pinMode(pin_A1, INPUT);
  pinMode(pin_B1, INPUT);
  pinMode(pin_A2, INPUT);
  pinMode(pin_B2, INPUT);
  pinMode(pin_A3, INPUT);
  pinMode(pin_B3, INPUT);

Useless code

So this should look a little cleaner as a foundation,

unsigned long currentTime;
unsigned long loopTime;
int fadeAmount = 5;  //incremental increase or decrease in LED brightness
byte brightnessRed = 120; //Starting at half the value of full brightness
byte brightnessGreen = 120;//Starting at half the value of full brightness
byte brightnessBlue = 120;//Starting at half the value of full brightness
byte encoder_A1;
byte encoder_B1;
byte encoder_A2;
byte encoder_B2;
byte encoder_A3;
byte encoder_B3;
const byte pin_A1 = 2;//A pin red encoder
const byte pin_B1 = 3;//B pin red encoder
const byte pin_A2 = 4;//A pin green encoder
const byte pin_B2 = 5;//B pin green encoder
const byte pin_A3 = 6;//A pin blue encoder
const byte pin_B3 = 7;//B pin Blue encoder
const byte LED_Red = 11;//Has to be set as output
const byte LED_Green = 10;//Has to be set as output
const byte LED_Blue = 9;//Has to be set as output
unsigned char encoder_A1_prev=0;
unsigned char encoder_A2_prev=0;
unsigned char encoder_A3_prev=0;

void setup()
{
  Serial.begin(9600); //Can add Serial.println later to each section
  pinMode(LED_Red, OUTPUT);
  pinMode(LED_Green, OUTPUT);
  pinMode(LED_Blue, OUTPUT);
  currentTime = millis();
  loopTime = currentTime;
}
}

"Useless code"
Yes, not needed, but then again, it does document things :wink:

If you used the internet pull-up resistors, you would need:

  pinMode(pin_A1, INPUT_PULLUP);
  pinMode(pin_B1, INPUT_PULLUP);
  pinMode(pin_A2, INPUT_PULLUP);
  pinMode(pin_B2, INPUT_PULLUP);
  pinMode(pin_A3, INPUT_PULLUP);
  pinMode(pin_B3, INPUT_PULLUP);

Use descriptive variable names. You shouldn't need comments to explain what they mean. If the pin is for the red encoder then just say so in the variable name.

int fadeAmount = 5; this could be 'byte' if the sketch keeps fadeAmount 0-255

unsigned char encoder_A1_prev=0; these could also be 'byte'

Looks like your daughter is going to be a programmer.

Edit
Not sure you saw this:

Maybe have a potentiometer the can be adjusted to create a 'divisor'.
It would be used to divide into all three: R, G and B values to set brightness/intensity.

pert:
Use descriptive variable names. You shouldn't need comments to explain what they mean. If the pin is for the red encoder then just say so in the variable name.

The comments help keep me organized as I am a beginner but I do get what you mean.

larryd:
int fadeAmount = 5; this could be 'byte' if the sketch keeps fadeAmount 0-255

unsigned char encoder_A1_prev=0; these could also be 'byte'

Looks like your daughter is going to be a programmer.

Edit
Not sure you saw this:

Maybe have a potentiometer the can be adjusted to create a 'divisor'.
It would be used to divide into all three: R, G and B values to set brightness/intensity.

I did see that, I do have a potentiometer, but not three. Thus the use of the encoder.

So basically the simplified version of the setup would look like

unsigned long currentTime;
unsigned long loopTime;
byte fadeAmount = 5;  //incremental increase or decrease in LED brightness
byte brightnessRed = 120; //Starting at half the value of full brightness
byte brightnessGreen = 120;//Starting at half the value of full brightness
byte brightnessBlue = 120;//Starting at half the value of full brightness
byte encoder_A1;
byte encoder_B1;
byte encoder_A2;
byte encoder_B2;
byte encoder_A3;
byte encoder_B3;
byte encoder_A1_prev=0;
byte encoder_A2_prev=0;
byte encoder_A3_prev=0;
const byte pin_A1 = 2;//A pin red encoder
const byte pin_B1 = 3;//B pin red encoder
const byte pin_A2 = 4;//A pin green encoder
const byte pin_B2 = 5;//B pin green encoder
const byte pin_A3 = 6;//A pin blue encoder
const byte pin_B3 = 7;//B pin Blue encoder
const byte LED_Red = 11;//Has to be set as output
const byte LED_Green = 10;//Has to be set as output
const byte LED_Blue = 9;//Has to be set as output


void setup()
{
  Serial.begin(9600); //Can add Serial.println later to each section
  pinMode(LED_Red, OUTPUT);
  pinMode(LED_Green, OUTPUT);
  pinMode(LED_Blue, OUTPUT);
  currentTime = millis();
  loopTime = currentTime;
}
}

Looking good.

I do have a potentiometer, but not three.

You only need one.

You would read the value of the pot.
Then 'map' the reading to a reasonable range.
Then divide the values of all three R G B by the mapped value.
Maybe the mapping could be 0-1023 to 1-5

Example:
Let’s say the pot was set to center, the reading would be 512.
The let’s say you 'map' 512 to 2
Let’s say R=100 G=50 and B=80
Then after dividing R G B by 2 would give: R=50 G=25 and B=40
Therefore the same colour but less bright.

larryd:
Looking good.

You only need one.

You would read the value of the pot.
Then 'map' the reading to a reasonable range.
Then divide the values of all three R G B by the mapped value.
Maybe the mapping could be 0-1023 to 1-5

Example:
Let’s say the pot was set to center, the reading would be 512.
The let’s say you 'map' 512 to 2
Let’s say R=100 G=50 and B=80
Then after dividing R G B by 2 would give: R=50 G=25 and B=40
Therefore the same colour but less bright.

I think that would be awesome to do. Ideally what I'm trying to accomplish is the three knobs having their own individual value, R, B, G, so my 4 year old can learn what mixing the colors does. A little bit of blue and green makes yellow, red and blue make purple, all three make white, etc.

Just an educational toy.

Just an educational toy.

No no no :o

This will lead to something much bigger as she grows up! :slight_smile:

BTW
There are things called neopixels that might be better to use.

larryd:
No no no :o

This will lead to something much bigger as she grows up! :slight_smile:

BTW
There are things called neopixels that might be better to use.

Got it but i'm not there yet with her. Just need to focus on this one and take it step by step. Shes 4. She wants crayons and world peace.

Neopixel, or WS2812B,
Spartkfun carries them as thru hole LEDs also if you don't want to deal with SMDs.

or a flatter top,

mgttrottier:
The comments help keep me organized as I am a beginner but I do get what you mean.

Comments are great. Definitely use lots of comments. But you wouldn't want to have the same comment explaining what a variable is for everywhere you use it, that's why you should use descriptive variable names so you don't have to think for a second "Now which color was A?".

CrossRoads:
or a flatter top,
LED - RGB Addressable, PTH, 5mm Clear (5 Pack) - COM-12999 - SparkFun Electronics

I like that "straw hat" style of LED but I think a bare non-diffused LED would be confusing because you still see too much of each individual color. If there will be some external diffuser added that would be fine (you still don't get as good of color mixing in my experience but it's not too bad) but I haven't read anything here about a diffuser.

Love the suggestions, and I will most certainly look into addressable LEDs for future projects. But to steer back to what the point was...

Is this a viable base code for using three encoders to control a common cathode RGB LED?

Again, Im trying to do the good father thing and teach her how colors mix. I certainly do appreciate the education I can receive, but this is just a simple toy for a four year old her dad built.

Leonardo board, three cts encoders, 1 common cathode RGB led, that's it.

I'm a beginner, i'm a network security expert, not a programmer. I'm learning....