/*
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"
*/
/*
The circuit:
SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 10
*/
#include <SPI.h>
#include <SD.h>
#include <LCD.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27 // <<----- Add your address here. Find it from I2C Scanner
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
LiquidCrystal_I2C lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);
File dataFile;
//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
int r = 0;
int g = 0;
int b = 0;
int num1 = 0;
int savebutton = 8; //save button pin 12 -> gnd
int rst = 4;
int save = 0;
int saven = 0;
char alpha = 'a'; //set the intial letter to lower case "a"
char alphabet[65] = {"abcdefghijklmnopqrstuvwxyz0123456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ"}; //store the alphabet, lowercase and uppercase as well as some numbers and characters. obviously incorrect. Not sure how to do this
char filename[] = "FileName.txt";
const int chipSelect = 10;
void setup() {
Serial.begin(9600);
SD.begin(10);
pinMode(RLed, OUTPUT);
pinMode(GLed, OUTPUT);
pinMode(BLed, OUTPUT);
pinMode(RFad, INPUT);
pinMode(GFad, INPUT);
pinMode(BFad, INPUT);
pinMode(savebutton, INPUT);
digitalWrite(savebutton, HIGH);
pinMode(rst, OUTPUT);
digitalWrite(rst, HIGH);
lcd.begin(16, 2);
lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
lcd.setBacklight(HIGH);
Serial.println("setup initialized");
}
void loop() {
save = digitalRead(savebutton);
while (save == HIGH) {
Serial.println(" R******G*****B");
r = (analogRead(RFad) / 4);
g = (analogRead(GFad) / 4);
b = (analogRead(BFad) / 4);
Serial.print(r);
Serial.print(",");
Serial.print(g);
Serial.print(",");
Serial.println(b);
analogWrite(RLed, r);
analogWrite(GLed, g);
analogWrite(BLed, b);
delay(33);
save = digitalRead(savebutton);
if (save == LOW) {
Serial.println(" R******G*****B");
Serial.print(r);
Serial.print(",");
Serial.print(g);
Serial.print(",");
Serial.println(b);
Serial.println("saving");
delay(1000);
saveit();
}
}
}
void saveit() {
save = digitalRead(savebutton);
while (save == HIGH) {
num1 = analogRead(RFad); //read the value from the red potentiometer
num1 = map(num1, 0, 1023, 0, 64); //map the value read to the amount of characters in the array
alpha = (alphabet[num1]); //put a character from the array into the variable "alpha"
Serial.println("Name Colour");
Serial.println(filename);
int x = analogRead(BFad);
x = map(x, 0, 1023, 0, 7);
lcd.setCursor(x, 1);
lcd.print(alpha);
filename[x] = alpha;
delay(150);
save = digitalRead(savebutton);
if (save == LOW) {
yousure();
}
}
}
void yousure() {
delay(1000);
save = digitalRead(savebutton);
lcd.clear();
while (save == HIGH) {
Serial.println(filename);
Serial.println("Save File? ");
num1 = analogRead(RFad); //read the value from the red potentiometer
num1 = map(num1, 0, 1023, 0, 10); //map the value read to the amount of characters in the array
if (num1 <= 5) {
Serial.println("N");
}
else if (num1 >= 5) {
Serial.println("Y");
}
save = digitalRead(savebutton);
}
if (save == LOW) {
//this part of the code is used to identify the users answer and save or restart
if (num1 <= 5) {
saveit();
}
else if (num1 >= 5) {
Serial.println(filename);
Serial.print(r);
Serial.print(",");
Serial.print(g);
Serial.print(",");
Serial.println(b);
Serial.println("saving");
delay(3000);
dataFile = SD.open(filename, FILE_WRITE);
if (dataFile) { // code seems to be getting stuck here and jumping to else statement
dataFile.print(r);
dataFile.print(",");
dataFile.print(g);
dataFile.print(",");
dataFile.println(b);
dataFile.close();
Serial.println("Complete");
delay(3000);
digitalWrite(rst, LOW);
}
else {
Serial.println("SD Write Error");
Serial.println(filename);
delay(3000);
digitalWrite(rst, LOW);
}
}
}
}
cattledog:
How do you know if data is written or not written.
-When I open the file on the sd card it contains no information.
Is there a sequence number of time stamp as part of the data?
-no just 3 values. a red value from 0-255 a blue and a green.
What do you mean by "Writing to the file"? and "Writing to the card"?
-every save to the sd card should create a new file. Every file should contain 3 values.