1

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"); 
        }



}
  • Ever thought of what would happen if the softpotReading is exactly 980? But what if you switch ++cleanCount and --cleanCount. Not sure why you're using those, but switching them around might cause it to work like you expected? There would be a way more efficient way to map your potentiometer reading onto the leds. Don't have time to explain it at the moment. And I don't have an arduino at the moment... Veel succes! – Paul Apr 7 '15 at 20:16
  • I've coded it that if it's above 800, lid 5 LED's. doesn't really map onto if (softpotReading < 980) { // IF TOUCHED – Paul Apr 7 '15 at 20:18
6

If your LEDs are connected anode to +5, cathode to i/o pin, they would behave this way. Cathode to ground, anode to i/o would behave as you expect.

  • 1
    @FuaZe His answer checks out to me, the LED is off when the IO pin matches the ground/power on the other side making the voltage diff 0, and on when the IO pin opposes the power connection making a voltage difference. Many LED's are wired so that they are lit when IO is LOW because of some electrical characteristics of the IO pins I couldn't fully explain. – BrettAM Apr 7 '15 at 20:45
  • Yeah, I didn't get it at first glance as I was sure something wasn't 100% in the code. But current will flow from the 5V to the arduino. And indeed putting negative to ground and positive (with current limiting resistor) to arduino will work. – Paul Apr 7 '15 at 21:05
  • Should I post a photo of my setup (wires and such?). Pretty sure it's correct. If there is software to create a schematic, tell me, else you've got to do it with a picture :) – Sander Schaeffer Apr 7 '15 at 21:06
  • Basically it comes down to this: if one side of the LED is attached to 5V instead of GND/0V then it will be 'inverted' – Paul Apr 7 '15 at 21:10
0

Try reversing the if statements. You say:

I've coded it that if it's above 800, lid 5 LED's.

And your code is: (softpotReading < 980) { // IF TOUCHED

The code describes: IF softpotReading IS SMALLER THAN 980, DO{ code.. }


In dutch (as OP is dutch, and to be honest his English somehow confuses me a bit, to be sure it's not the other way around also):

Ik denk dat je de IF statements verkeerd om hebt. Je beschrijft dat bij een waarde hoger dan 800 de ledjes aan gaan...

Echter staat in je code (softpotReading < 980) { // IF TOUCHED Wat dus zal betekenen dat de code wordt uitgevoerd als die < (kleiner dan) 980 is.


    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
}
  • Yeah I'm Dutch ;) But I'll reply in English, for Site's sake. You really got to ignore the 'IF SoftpotReading < 980. Which is completely different. I've got a softPod module which reads out about 1000 if it's not touched. Once you touch it, it never goes above 980. You only have to look at the other statements. I could indeed reverse the IF statements. Thanks – Sander Schaeffer Apr 7 '15 at 20:50
  • Sander you could also try to do int softpotReading = 1024-analogRead(softpotPin); This way, your softpot wil give 0 for low to 1024 for high – Paul Apr 7 '15 at 21:00
  • Try to think of what your input is and what you want your output to be. Then you should use your code to bridge that gap. – Paul Apr 7 '15 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.