const int NUM_OPTIONS = 6;
int counter = 3;
int up = 9;
int down = 10;
int LEDR = 8;
int LEDB = 7;
int LEDG = 6;
int sensPin = A2;
int relayPin = 5;
int tempPin = A3;
void setup()
{
pinMode(sensPin, INPUT);
pinMode(up, INPUT);
pinMode(down, INPUT);
pinMode(LEDR, OUTPUT);
pinMode(LEDG, OUTPUT);
pinMode(LEDB, OUTPUT);
pinMode(relayPin, OUTPUT);
pinMode(tempPin, INPUT);
}
void loop() {
int val = analogRead(tempPin);
//this makes sure the system only runs if it is above freezing
if(val > 260) {
int upval = digitalRead(up);
if(upval == HIGH) {
counter++;
if (counter > NUM_OPTIONS) {
counter = 1;
}
}
int downval = digitalRead(down);
if(downval == HIGH) {
counter--;
if (counter <= 0) {
counter = NUM_OPTIONS;
}
}
switch (counter) {
case 1:
{
analogWrite(LEDR, LOW);
analogWrite(LEDB, HIGH);
analogWrite(LEDG, LOW);
int val = analogRead(sensPin);
if(val > 600)
{
digitalWrite(relayPin, HIGH);
}
else if(val < 580);
{
digitalWrite(relayPin, LOW);
}
delay(100);
break;
}
case 2:
{
analogWrite(LEDR, LOW);
analogWrite(LEDG, 75);
analogWrite(LEDB, HIGH);
int val = analogRead(sensPin);
if(val > 560)
{
digitalWrite(relayPin, HIGH);
}
else if(val < 540)
{
digitalWrite(relayPin,LOW);
}
delay(100);
break;
}
case 3:
{
analogWrite(LEDR, LOW);
analogWrite(LEDG, HIGH);
analogWrite(LEDB, LOW);
int val = analogRead(sensPin);
if(val > 500)
{
digitalWrite(relayPin, HIGH);
}
else if(val < 480);
{
digitalWrite(relayPin,LOW);
}
delay(100);
break;
}
case 4:
{
analogWrite(LEDR, 75);
analogWrite(LEDB, 75);
analogWrite(LEDG, HIGH);
int val = analogRead(sensPin);
if(val > 470)
{
digitalWrite(relayPin, HIGH);
}
else if(val < 450);
{
digitalWrite(relayPin,LOW);
}
delay(100);
break;
}
case 5:
{
analogWrite(LEDR, HIGH);
analogWrite(LEDB, LOW);
analogWrite(LEDG, LOW);
int val = analogRead(sensPin);
if(val > 440)
{
digitalWrite(relayPin, HIGH);
}
else if(val < 420);
{
digitalWrite(relayPin,LOW);
}
delay(100);
break;
}
}
}
else {
digitalWrite(relayPin, LOW);
digitalWrite(LEDR, LOW);
digitalWrite(LEDG, LOW);
digitalWrite(LEDB, 75);
delay(300);
digitalWrite(LEDB, LOW);
delay(2000);
}
}
Some random tips:
Properly indent your code; CTRL+T is your friend.
Use consistent variable names and capitalization. upBtnPin and downBtnPin would be better names, since you already have variables like sensePin.
A variable name should describe its role in the program. Counter here is not a counter, but a temperature option, so a better name would be for example currTempOption.
If you'd make counter go from 0 to 5 instead of 1 to 6 you could use it as an index into one or more arrays which would hold the various led pwm levels and temperature threshold. You could then have only a few lines of code instead of a "big" select/case where each option has the same code flow but different data.
BTW, case 6 is missing.
I think you mixed analogWrite and digitalWrite...
HTH