no for //Library Init============================================================
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SPI.h>
#include <SD.h>
//========================================================================
//OLED SCREEN PARAMETERS==================================================
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
//========================================================================
//Push button states======================================================
int button1 = 0; //Selector State
int button2 = 0; //Apply Color State
int button3 = 0; //Random Color State
//========================================================================
//RGB LED Values==========================================================
int red = 0;
int green = 0;
int blue = 0;
//========================================================================
//Selector switch state for choosing R,G,B colors=========================
int sel = 1;
//========================================================================
int potVal = 0;//The Value stored from the potentiometer
int gunVal = 0;//The Value transformed from potVal to
//map(potVal,0,1023,0,255);
//========================================================================
long randNumber;//Used for storing a random number
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
//SD CARD DECLERATION FOR FILE============================================
File myFile;
//SETUP PORTION============================================================
void setup() {
Serial.begin(9600); //Establish Serial connection
//========================================================================
//Pin init. - used pins are 6,9,19,11,12,13
//========================================================================
pinMode(11, OUTPUT);//RED
pinMode(10, OUTPUT);//GREEN
pinMode(9, OUTPUT);//BLUE
//========================================================================
pinMode(13, INPUT); //Button1
pinMode(12, INPUT); //Button2
pinMode(6, INPUT); //Button3
//========================================================================
randomSeed(analogRead(0)); //Generate seed for random number via
//analog pin 0
//========================================================================
//DISPLAY INIT. FIRST TIME================================================
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D
//for 128x32
Serial.println(F("SSD1306 allocation failed"));//If there
//wasn't enough memory
for (;;);
}
delay(2000);
display.clearDisplay();//Remove contents of display
display.setTextSize(1); // Set size of text on OLED
display.setTextColor(WHITE); // Color for font
display.setCursor(0, 10); // Control cursor position
display.println("Starting"); // Display static text
display.display(); //Show stuff on OLED
delay(1000);
display.clearDisplay();
display.display();
//SD CARD CODE============================================================
if (!SD.begin(4)) {
oledDisplayCenter(String("INIT FAILED"));
delay(1000);
}
oledDisplayCenter(String("INIT DONE"));
delay(1000);
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
oledDisplayCenter(String("Writing to test.txt..."));
delay(1000);
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
oledDisplayCenter(String("done"));
delay(1000);
} else {
// if the file didn't open, print an error:
oledDisplayCenter(String("Error opening test.txt"));
delay(1000);
}
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
oledDisplayCenter(String("test.txt:"));
delay(1000);
// read from the file until there's nothing else in it:
while (myFile.available()) {
// Serial.write(myFile.read());
String sdStoredString = String(myFile.read());
oledDisplayCenter(sdStoredString);
delay(1000);
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
oledDisplayCenter(String("ERROR OPENING"));
delay(1000);
}
}
//========================================================================
//========================================================================
//========================================================================
void loop() {
//Button1 Read value is located in selector() function
button2 = digitalRead(12); //Read value for Button2
button3 = digitalRead(6);// Read value for Button3
//========================================================================
potVal = analogRead(A5); //Use A5 to read from potentiometer
gunVal = map(potVal, 0, 1023, 0, 255); //Transform the value into
//a usable 255 PWM value.
//========================================================================
selector(); //checks for Selector state by running function
showDisplay();//Updates display
//========================================================================
if (button2 == 1) { //apply color by pressing button 2 in conjuction with
//selector value
if (sel == 1) {
red = gunVal;
}
if (sel == 2) {
green = gunVal;
}
if (sel == 3) {
blue = gunVal;
}
}
//========================================================================
if (button3 == 1) { //RANDOM NUMBER GENERATOR
randomSeed(analogRead(0));
red = random(256);
green = random(256);
blue = random(256);
delay(200);
}
//========================================================================
analogWrite(11, red);//write to RGB LED
analogWrite(10, green);//write to RGB LED
analogWrite(9, blue);//write to RGB LED
//========================================================================
}//END OF LOOP FUNTION
//========================================================================
//========================================================================
//========================================================================
//========================================================================
//FUNCTIONS:::::==========================================================
void selector()//Function to choose next color, loops back to 1
{
button1 = digitalRead(13); //Read Button 1 State
if (button1 == 1) {
if (sel < 3) {
sel = sel + 1;
delay(200);
}
else {
sel = 1;
delay(200);
}
}
}
//========================================================================
void showDisplay()//Function to bring contents of RGB selection values
//to OLED display in order to have a real time update on values.
{
display.clearDisplay();
display.setCursor(0, 10);
display.println(String(red) + " " + String(green) + " " + String(blue)
+ " " + String(gunVal) + " " + String(sel));
display.display();
}
//========================================================================
void oledDisplayCenter(String text) { //CENTER ANY TEXT BY CALLING FUNCTION
//AND A STRING INSIDE THE BRACKETS
int16_t x1;
int16_t y1;
uint16_t width;
uint16_t height;
display.getTextBounds(text, 0, 0, &x1, &y1, &width, &height);
// display on horizontal and vertical center
display.clearDisplay(); // clear display
display.setCursor((SCREEN_WIDTH - width) / 2, (SCREEN_HEIGHT - height) / 2);
display.println(text); // text to display
display.display();
}
//========================================================================
I can't get the Oled to properly display a string from the SD card file. All I'm getting are integers instead of readable text.
I've tried looking everywhere but I couldn't find anything.
The problem is in the section "SD CARD CODE" which is really just the example for read/write but modified to use the OLED display instead of serial.