/* hey, this is a simple code to make your arduino read
the value of a potentiometer and display it in percentage form on
a 16 X 2 LCD screen. I am pretty new at this so sorry if this code
is terrible or if i have no idea what i'm talking about in the
comments.
the circuit(pasted from examples):
LCD RS pin to digital pin 12
LCD Enable pin to digital pin 11
LCD D4 pin to digital pin 5
LCD D5 pin to digital pin 4
LCD D6 pin to digital pin 3
LCD D7 pin to digital pin 2
LCD R/W pin to ground
10K resistor:
ends to +5V and ground
wiper to LCD VO pin (pin 3)
Potentiometer attached to analog input 0
center pin of the potentiometer to the analog pin
one side pin (either one) to ground
the other side pin to +5V
***************************************************************
*/
//#include <LiquidCrystal.h> // include the LCD library
#include <LiquidCrystal.h>
#include <SD.h>
#define CS_PIN 10
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const int chipSelect = 10; // Set the chip select pin for your SD card module
int pot1Pin = A0; //Potentiometer 1 input pin
int pot2Pin = A1; //Potentiometer 2 input pin
int pot3Pin = A2; //Potentiometer 3 input pin
int pot1Value1 = 0;
int pot1Value2 = 0; // final display variable
int pot2Value1 = 0;
int pot2Value2 = 0; // final display variable
int pot3Value1 = 0; // final display variable
int pot3Value2 = 0;
void setup()
{ lcd.begin(16, 2); // lcd rows and columns
lcd.print("Potentiometer"); // title of sorts
File root;
Serial.begin(9600);
Serial.print("Initializing SD card... ");
if (!SD.begin(CS_PIN)) {
Serial.println("Card initialization failed!");
while (true);
}
Serial.println("initialization done.");
Serial.println("Files in the card:");
root = SD.open("/");
printDirectory(root, 0);
Serial.println("");
// Example of reading file from the card:
File textFile = SD.open("wokwi.txt");
if (textFile) {
Serial.print("wokwi.txt: ");
while (textFile.available()) {
Serial.write(textFile.read());
}
textFile.close();
} else {
Serial.println("error opening wokwi.txt!");
}
}
void loop() {
// read then divide the input(max 1020 in this case) by 10
pot1Value1 = analogRead(pot1Pin) / 10;
pot2Value1 = analogRead(pot2Pin) / 10;
pot3Value1 = analogRead(pot3Pin) / 10;
// divide by 1.02 to get percentage
pot1Value2 = pot1Value1 / 1.02;
pot2Value2 = pot2Value1 / 1.02;
pot3Value2 = pot3Value1 / 1.02;
// set cursor to second row, first column
lcd.setCursor(0, 1);
//display final percentage
lcd.print(pot1Value2);
//print the percent symbol at the end
lcd.print("%");
lcd.setCursor(4, 1);
lcd.print(pot2Value2);
lcd.print("%");
lcd.setCursor(8, 1);
lcd.print(pot3Value2);
lcd.print("%");
//wait 0.1 seconds
delay(100);
//wipe the extra characters
lcd.print(" ");
delay(1);
}
void printDirectory(File dir, int numTabs) {
while (true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
break;
}
for (uint8_t i = 0; i < numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
printDirectory(entry, numTabs + 1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
/*
#include <SD.h>
#define CS_PIN 10
File root;
void setup() {
Serial.begin(9600);
Serial.print("Initializing SD card... ");
if (!SD.begin(CS_PIN)) {
Serial.println("Card initialization failed!");
while (true);
}
Serial.println("initialization done.");
Serial.println("Files in the card:");
root = SD.open("/");
printDirectory(root, 0);
Serial.println("");
// Example of reading file from the card:
File textFile = SD.open("wokwi.txt");
if (textFile) {
Serial.print("wokwi.txt: ");
while (textFile.available()) {
Serial.write(textFile.read());
}
textFile.close();
} else {
Serial.println("error opening wokwi.txt!");
}
}
void loop() {
// nothing happens after setup finishes.
}
void printDirectory(File dir, int numTabs) {
while (true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
break;
}
for (uint8_t i = 0; i < numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
printDirectory(entry, numTabs + 1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}
*/