I've got a function that decreases over time (1000 / 60 seconds) and by touching a softPotentiometer you can increase that value. By every increment of 200 a LED is lid. So for 0-200, 1 LED; 201-400, 2 LEDs; etc. And since it decreases over time while doing nothing, if it drops below a certain value, a LED is shut off.
However, I've got a (amateur) coded set up, but somehow it lids the LEDs in the opposite direction. In a way that, I've coded it that if it's above 800, lid 5 LED's. However, it will only lid one LED when it's dropped below the 800, in the area of 601-800.
That's exactly the opposite of what I want. So what am I doing wrong here?
My code:
And with amateur, I mean that I didn't use arrays and therefor write everything multiple times..
int cleanCount = 1000;
int IsItWorkingLED = 13;
int softpotPin = A0; //analog pin 0
int LED1 = 1;
int LED2 = 2;
int LED3 = 4;
int LED4 = 5;
int LED5 = 7;
void setup () {
Serial.begin(9600);
digitalWrite(softpotPin, HIGH); //enable pullup resistor
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(7, OUTPUT);
digitalWrite(1, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(2, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(4, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(5, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(7, HIGH); // turn the LED on (HIGH is the voltage level)
}
void loop() {
int softpotReading = analogRead(softpotPin);
if (softpotReading < 980) { // IF TOUCHED
digitalWrite(IsItWorkingLED, HIGH);
++cleanCount;
Serial.println(cleanCount);
delay(16); // Drain 5 times as fast
}
else if (softpotReading > 980) { // IF NOT DTOUCHED
digitalWrite(IsItWorkingLED, LOW);
--cleanCount;
Serial.println(cleanCount);
delay(64); // 1000 / 60 seconds
}
else { // Als er een fout optreed..
Serial.println("Something wrong!");
digitalWrite(IsItWorkingLED, HIGH);
delay(250);
digitalWrite(IsItWorkingLED, LOW);
delay(250);
}
if (cleanCount >= 0 && cleanCount <= 200) {
Serial.println("Knal 1 LED aan");
digitalWrite(LED1, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(LED2, LOW); // turn the LED on (HIGH is the voltage level)
digitalWrite(LED3, LOW); // turn the LED on (HIGH is the voltage level)
digitalWrite(LED4, LOW); // turn the LED on (HIGH is the voltage level)
digitalWrite(LED5, LOW); // turn the LED on (HIGH is the voltage level)
}
else if (cleanCount >= 201 && cleanCount <= 400) {
Serial.println("Knal 2 LEDs aan");
digitalWrite(LED1, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(LED2, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(LED3, LOW); // turn the LED on (HIGH is the voltage level)
digitalWrite(LED4, LOW); // turn the LED on (HIGH is the voltage level)
digitalWrite(LED5, LOW); // turn the LED on (HIGH is the voltage level)
}
else if (cleanCount >= 401 && cleanCount <= 600) {
Serial.println("Knal 3 LEDs aan");
digitalWrite(LED1, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(LED2, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(LED3, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(LED4, LOW); // turn the LED on (HIGH is the voltage level)
digitalWrite(LED5, LOW); // turn the LED on (HIGH is the voltage level)
}
else if (cleanCount >= 601 && cleanCount <= 800) {
Serial.println("Knal 4 LEDs aan");
digitalWrite(LED1, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(LED2, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(LED3, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(LED4, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(LED5, LOW); // turn the LED on (HIGH is the voltage level)
}
else if (cleanCount >= 801 && cleanCount <= 1000) {
Serial.println("Knal 5 LEDs aan");
digitalWrite(1, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(2, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(4, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(5, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(7, HIGH); // turn the LED on (HIGH is the voltage level)
}
else {
Serial.println("Just..do nothing");
}
}
I've coded it that if it's above 800, lid 5 LED's.
doesn't really map ontoif (softpotReading < 980) { // IF TOUCHED
– Paul Apr 7 '15 at 20:18