RGB Colour Mixing Program Help

Hey everyone!

I am trying to create a program that would allow a user to create a colour using 3 level faders for each of the rgb channels. Once a colour has been created it should ask the user to name the colour. Scroll through letters using the red fader. Use save button to advance cursor. Once colour has been named save a text file to a sd card as "Colourname.txt". In the file store the RGB values as "R,red value,G,green value,B,blue value" ie. "R,134,G,255,B,003".

I have some of the code fleshed out but am a little lost on how to name the file.

Any help would be greatly appreciated. Code is attached

Colour_Mixer.ino (1.85 KB)

You will probably get more help if you post your code here rather than as an attachment. When you do that please use [­code]code goes here[­/code] tags around the code.

/*
This program should allow the user to "create" a colour using the RGB potentiometers. 
Once a colour has been created, ask the user to name the colour. Scroll through letters using the red pot. 
Use save button to advance cursor. Once colour has been named save a text file to a sd card as "Colourname.txt".
In the file store the RGB values as "R,r,G,g,B,b" ie. "R,134,G,255,B,003" 
*/

#include <SPI.h>
#include <SD.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); //update to use other pins

//LED Pins
int RLed = 3; 
int GLed = 5;
int BLed = 6;
//Level Pots Pins 
int RFad = A0; 
int GFad = A1; 
int BFad = A2; 
//LED Level Variables
long r = 0; 
long g = 0;
long b = 0;

int num1 = 0;
int savebutton = 13;
char alpha = 'a';
char alphabet[] = {"abcdefghijklmnopqrstuvwxyz0123456789-_#ABCDEFGHIJKLMNOPQRSTUVWXYZ"}; // obviously incorrect. Not sure how to do this
void setup() {
pinMode(RLed, OUTPUT);
pinMode(GLed, OUTPUT);
pinMode(BLed, OUTPUT);
pinMode(RFad, INPUT);
pinMode(GFad, INPUT);
pinMode(BFad, INPUT);
pinMode(savebutton, INPUT_PULLUP);
lcd.begin(16,2);

}

void loop() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("R******G*******B");
r = (analogRead(RFad)/4);
g = (analogRead(GFad)/4);
b = (analogRead(BFad)/4);
lcd.setCursor(0,1);
lcd.print(r);
lcd.setCursor(7,1);
lcd.print(g);
lcd.setCursor(13,1);
lcd.print(b);
analogWrite(RLed, r);
analogWrite(GLed, g);
analogWrite(BLed, b);
delay(28);
int save = digitalRead(savebutton);
if (save = LOW) {
  saveit(); 
}
}

void saveit(){
analogWrite(RLed, r);
analogWrite(GLed, g);
analogWrite(BLed, b);
num1 = analogRead(RFad);
num1 = map(num1, 0, 1023, 0, 65);
alpha = 'alphabet.charAt(num1)'; 
lcd.setCursor(0, 1);
lcd.print("R******G*******B");
lcd.setCursor(0,1);
lcd.print(r);
lcd.setCursor(6,1);
lcd.print(g);
lcd.setCursor(12,1);
lcd.print(b);
lcd.clear();
lcd.print("Name Colour");
lcd.print(alpha);
}

if (save = LOW)Before you go any further fix this.

Then fix thisalpha = 'alphabet.charAt(num1)';

UKHeliBob:

if (save = LOW)

Before you go any further fix this.

Not really understanding what is wrong with this? Some explanation would be greatly appreciated. In the initialization of the inputs I declared the save button as an input with a pull up resistor so would this not trigger the save function when the button was pressed and the pin pulled to ground?

AWOL:
Then fix this

alpha = 'alphabet.charAt(num1)';

Again, not really sure what is wrong with this? I am thinking its the apostrophes? Looking for help on how to fix this or even a link to some reference material that may help.

Thanks for your help so far!

if (save == LOW)

Two == for comparison
One = for value assignment

The other one, try without the apostrophes.

Oh wow, I should have known that :P, Thank you CrossRoads!

When I try and run this program after making the suggested edits I get the following error:

Colour_Mixer.ino: In function 'void saveit()':
Colour_Mixer.ino:71:18: error: request for member 'charAt' in 'alphabet', which is of non-class type 'char [66]'
request for member 'charAt' in 'alphabet', which is of non-class type 'char [66]'

I think it has something to do with this horrible string array:

char alpha = 'a'; //set the intial letter to lower case "a"
char alphabet[] = {"abcdefghijklmnopqrstuvwxyz0123456789-_#ABCDEFGHIJKLMNOPQRSTUVWXYZ"}; //store the alphabet, lowercase and uppercase as well as some numbers and characters. obviously incorrect. Not sure how to do this

and recalling this code at this point:

void saveit(){
analogWrite(RLed, r);
analogWrite(GLed, g);
analogWrite(BLed, b);
num1 = analogRead(RFad); //read the value from the red potentiometer
num1 = map(num1, 0, 1023, 0, 65); //map the value read to the amount of characters in the array
alpha = alphabet.charAt(num1); //put a character from the array into the variable "alpha" 
lcd.setCursor(0, 1);
lcd.print("R******G*******B");
lcd.setCursor(0,1);
lcd.print(r);
lcd.setCursor(6,1);
lcd.print(g);
lcd.setCursor(12,1);
lcd.print(b);
lcd.clear();
lcd.print("Name Colour");
lcd.print(alpha);
}

alphabet.charAt() works with a String object.
alphabet is not a String object, it is a C style string.

So I would need to put my upper and lowercase alphabet with extra characters into a string object which could then be referenced by charAt?

Also I was noticing limits on how many characters can be in strings and arrays. Is there a simpler/better way to do what I am trying to do?

So I would need to put my upper and lowercase alphabet with extra characters into a string object which could then be referenced by charAt?

Or you could leave it in a char array and access them using the array index.

Also I was noticing limits on how many characters can be in strings and arrays.

The problem is shortage of memory. Using strings instead of Strings could help with that.

Is there a simpler/better way to do what I am trying to do?

Could use derive the ASCII code of the character that you want from a counter variable ?

So just to be clear, if i was going to use the array index, (because I have no idea how to work with ASCII code) I would need to reformat my text in the array to match the example:

char Str2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'};
ie.
char alphabet[] = {'a','b','c','d'...etc}

just to be clear, I am named the string "alphabet" because thats what it contains. Or do i need to leave the name as "Str#"?

Thank you!

The array name can be any valid variable name. It is the array type that matters.

Try this example

char alphabet[] = {"abcdefABCDEF"};

void setup()
{
  Serial.begin(115200);
  Serial.println(alphabet[4]);
  Serial.println(alphabet[10]);
}

void loop() {}

@UKHeliBob THANK YOU! You made a lightbulb click! My brackets were not square and that was preventing my code from working.