Hello,
I am working on project which is speaker and where I am using digital potentiometer to change the volume. This varying of volume will display on LCD and decrease or increase of volume take place via button (up for Increase button and down for decrease button). The display show the number of volume i.e (0, 16, 32, 48, 64, 80, 96, 112, 128), also it diplay "Min Volume" when reached to 16 and "Max Volume" when reached to 128. Here is the code:
/*
MCP4018T103E connected to arduino Board
CS >>> D10
SCLK >> D13
DI >>> D11
PA0 TO VCC
PBO TO GND
PW0 TO led with resistor 100ohm .
*/
#include <SPI.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 9, 5, 4, 3, 2); //RS = 12, E = 9,
byte address = 0x11;
const int CS= 10;
int i=0;
int vol = 0;
const int DOWN_BUTTON_PIN = 1;
const int UP_BUTTON_PIN = 0;
volatile boolean up = false;
volatile boolean down = false;
long time = 0;
long debounce = 200;
void setup()
{
lcd.clear();
lcd.begin(16, 2); //LCD have 16 column and 2 rows
lcd.setCursor(1,0); //LCD cursor set at 1st clomn and 0 rows
pinMode(UP_BUTTON_PIN, INPUT_PULLUP); //button
pinMode(DOWN_BUTTON_PIN, INPUT_PULLUP);
pinMode (CS, OUTPUT);
SPI.begin();
// adjust high and low resistance of potentiometer
// adjust Highest Resistance .
digitalPotWrite(0); //0
delay(1000);
// adjust wiper in the Mid point .
digitalPotWrite(64); //64
delay(1000);
// adjust Lowest Resistance .
digitalPotWrite(128); //128
delay(1000);
}
void loop()
{
checkIfDownButtonIsPressed(digitalRead(DOWN_BUTTON_PIN));
checkIfUpButtonIsPressed(digitalRead(UP_BUTTON_PIN));
if (up || down)
{ // Slow the up/down menus a bit
delay(1000);
}
for (i = 0; i <= 127; i=i+16)
{
if (up) { //UP Button Logics
up = false;
i = i + 16;
vol = digitalPotWrite(i);
lcd.setCursor(1,0); //Column 1 and Row 0
lcd.print("Volume: vol");
if ( i == 127)
{
lcd.setCursor(1,1);
lcd.print(" MAX VOLUME ");
delay(10);
}
}
}
delay(500);
for (i = 128; i > 0; i= i-16)
{
if (down) { //DOWN Button Logics
down = false;
i = i - 16;
vol = digitalPotWrite(i);
lcd.setCursor(1,0); //Column 1 and Row 0
lcd.print("Volume: vol");
if ( i == 16)
{
lcd.setCursor(1,1);
lcd.print(" MIN VOLUME ");
delay(10);
}
}
}
void checkIfDownButtonIsPressed(int newState)
{
static int lastDownButtonState = 0;
checkButton(down, lastDownButtonState, newState);
}
void checkIfUpButtonIsPressed(int newState)
{
static int lastUpButtonState = 0;
checkButton(up, lastUpButtonState, newState);
}
void checkButton(volatile boolean &output, int &oldState, int newState) {
if (oldState != newState) {
if (newState == 1) {
output = true;
}
delay(50);
}
oldState = newState;
}
int digitalPotWrite(int value)
{
digitalWrite(CS, LOW);
SPI.transfer(address);
SPI.transfer(value);
digitalWrite(CS, HIGH);
}
Problem I am facing :
1st : Code show error of digitalPotWrite is not in the scope.
2nd: LCD just glow and doesn't show anything in LCD.
Please help, I am new here. Please enlighten me from your knowldge.