Good morning,
I am currently embarking on a project to control a heater using 2 push buttons, 1 to increase the heat and 1 to decrease the heat. i have 5 levels of heat and the heater controller is expecting to see a PWM signal in. I have 5 LED's which i would like to turn on (in a bar graph style) to indicate the level at which the heater is operating.
When the unit is powered up i want nothing to happen (all Outputs Low and PWM at 0)
I have previosly created a project in which i switched audio sources using a push button and an internal counter either sending outputs high or low dependant on the value of the internal counter (using the if function) so i thought this would be a good starting point. The difference being this project counted up then when it reached the 4th output it returned to the first one after you press the button.
Mistakenly i thought it would be a case of just adding an aditional function and push button to decrease the counter rather than just using 1 push button to increase the counter then have it reset, unfortunately this does not appear to be the case.
Currently my hardware is as such.
INPUTS
UP push button connected to D2 with a pull down and switching to +5v
DOWN push button connected to D3 with a pull down and switching to +5v
OUTPUTS
1st LED Through a resistor on D4 (To indicate the LOWEST heat setting)
2nd LED Through a resistor on D5
3rd LED Through a resistor on D6
4th LED Through a resistor on D7
5th LED Through a resistor on D8 (To indicate the HIGHEST heat setting)
Input of my heater controller connected to D9 (PWM signal which controlls how hot the heater gets)
(I am aware i will probablyneed to use transistors to switch the LED's but just to simplify things for the time being i am not.)
Once the counter reaches 5 by pressing the UP button ideally i would like the counter to stop counting and just remain at 5 regardless of how many times the UP button is pressed until the DOWN button is pressed which will make the counter go down and similarly when the DOWN button is pressed i would like the counter to stop at 0 untill the UP button is pressed. - This is another thing i am strugling with as my previos project which this is based on just looped back arround hence the
{if(count > 4)
{count = 0;}
function came from in this project.
I have also put in some serial commands just so i can see what the counter is doing through the serial monitor however once the project is working i will probably remove these commands.
i have posted my code below as i just cant quite get my head round what i am doing wrong, i have been looking at this for days and it may be somethng simple but i just cant figure it out. any help would be gratefully received. If there is any further info you need please let me know. Hardware i am very competant but i am not too good with software/code as you may be able to tell below.
// Variables That will change:
int count = 0; // counter for the number of button presses
int UpState = 0; // current state of the up button
int UpButtonState = 0; // previous state of the up button
int DownState = 0; // current state of the down button
int DownButtonState = 0; // previous state of the down button
void setup() {
// initialize input/output:
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop()
//If The Count Value Reaches 4 Force The Count To Start Again
{if(count > 4)
{count = 0;}
// read the up pushbutton input pin:
UpState = digitalRead(2);
// compare the buttonState to its previous state
if (UpState != UpButtonState) {
// if the current state is HIGH then the button went from off to on
if (UpState == HIGH) {
// if the state has changed, increment the counter up
count++;
Serial.print("Count: ");
Serial.println(count);
}else {
// if the current state is LOW then the button went from on to off:
// save the current state as the last state, for next time through the loop
UpButtonState = UpState;
// read the down pushbutton input pin:
DownState = digitalRead(3);
// compare the buttonState to its previous state
if (DownState != DownButtonState) {
// if the current state is HIGH then the button went from off to on
if (DownState == HIGH) {
// if the state has changed, increment the counter down
count--;
Serial.print("Count: ");
Serial.println(count);
}else {
// if the current state is LOW then the button went from on to off:
// save the current state as the last state, for next time through the loop
DownButtonState = DownState;
//if the counter is at 1
if (count == 1){
digitalWrite(4, HIGH);
analogWrite(9, 51);
//if the counter is at 2
}else if (count == 2){
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
analogWrite(9, 102);
//if the counter is at 3
} else if (count == 3){
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
analogWrite(9, 153);
//if the counter is at 4
}else if (count == 4){
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
analogWrite(9, 204);
//if the counter is at 5
}else if (count == 5){
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
analogWrite(9, 255);
//if the counter is at 0 or has just been turned on default to
}else{
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
analogWrite(9, 0);
}}}}}}
Thanks and best regards,
Phil.