Hi everyone,
I'm currently trying to control a digital pot, a MCP4261's resistance values via two tactile button switches. I got a crude implementation to work, except in incrementing the values, I can't get them to come back down properly. What I'm trying to do is limit the number of button presses to 20, so I'm using a constrain function, but I can't figure out how to lower the button presses for each button with the press of the other. Here's my code. I commented out the MCP part, that's not what's giving me problems. It's figuring out the code to go up the scale, and down the scale for the button presses, like a volume controller on a tiny mp3 player. I could sure use some help on this one.
/* This employs two buttons to turn on and off the LEDS, and to count the number of times
between each push. This is to later be incorporated with the MCP4261 Digital Pot to
control up and down movement of the resistance wiper.
*MCP4251 Digital Potentiometer
*CS to pin 10
*SCK to pin 13
*SDI to pin 11
*LED attached to MCP4251's wiper 0
Good exercise
*/
#include <DigiPot.h>
const int pin0 = 0; // setup the analog pin 0 as an input read out pin
int val = 0; // set up the value for transmission to the MCP4251
// this constant won't change:
const int buttonPin1 = 2; // Pin 2(nonPWM) is attached to the first button
const int buttonPin2 = 4; // Pin 4 (nonPWM) is attached to the second button
const int ledPin1 = 5; // the pin that the LED is attached to
const int ledPin2 = 6; // the pin that LED 2 is attached to
// Variables will change:
int buttonPushCounter1 = 0; // counter for the number of button1 presses
int buttonPushCounter2 = 0; // counter for the number of button2 presses
int potlevel = 0; // This is the counter number that's added to or subtracted from for the digital pot
int steplevel = 0; // The pot's step value from buttonPushCounter1
int button1 = 0;
int button2 = 0;
int buttonState1 = 0; // current state of button1
int buttonState2 = 0; // current state of button 2
int lastButtonState1 = 0; // previous state of button 1
int lastButtonState2 = 0; // previous state of button 2
int potval = 0; // the value initial of the digital pot
DigiPot mcp4251(10, 13, 11); //Start a new instance called "mcp4251"
//format: DigiPot <instance>(CS, SCK, SDI), and connect it to the digital outputs of
// 10, 13, and 11 from the Arduino board.
void setup() {
// initialize the button pins as a input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
// initialize the LED as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
// initialize the digital pot
mcp4251.clear(0); //clear both pots; they default to 127
mcp4251.clear(1); //format: <instance>.clear(pot)....pot is either 0 or 1
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
val = analogRead(pin0); // read the input from digital pot wiper 0
// read the pushbutton input pin:
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
// compare the buttonState to its previous state
if (buttonState1 != lastButtonState1) {
// if the state has changed, increment the counter
if (buttonState1 == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
digitalWrite(ledPin1, HIGH); // And let's indicate the button's being pushed on the led
buttonPushCounter1 = constrain(buttonPushCounter1++, 1, 20); // And increment the counter, but constrain it
}
else {
digitalWrite(ledPin1, LOW); // Turn it off when released
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState1 = buttonState1;
// compare the buttonState2 to its previous state
if (buttonState2 != lastButtonState2) {
// if the state has changed, increment the counter
if (buttonState2 == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
digitalWrite(ledPin2, HIGH); // Let's indicate the LED on when button 2 is pressed
buttonPushCounter2 = constrain(buttonPushCounter2++, 1, 20);
}
else {
digitalWrite(ledPin2, LOW); // And shut it off when it's off.
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState2 = buttonState2;
if (buttonPushCounter1 > buttonPushCounter2) {
buttonPushCounter1 = buttonPushCounter1 - buttonPushCounter2;
} else {
buttonPushCounter2 = buttonPushCounter2 - buttonPushCounter1;
}
Serial.println(buttonPushCounter1);
// Serial.println(potlevel);
// if (potlevel < 1) {
// steplevel1 = 1;
// } else {
// steplevel1 = potlevel;
// }
// Serial.println(steplevel1);
// int potval = map(steplevel1, 3, 1023, 1, 255); // map the values from
// the steplevel1 which is 0-1023
// re spread them across 0-255, which is the amount the
// mcp4251 can process
// mcp4251.write(0, potval); // send the value to the digital pot
// Serial.println(val);
}