1

I've found this code below, but I can't figure out how it works. Can someone explain to me the flow of the program?

#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 10, 7, 5, 4);
int mem=0;
void setup() {
    pinMode (12,OUTPUT);
    pinMode(13,OUTPUT);
    // initialize the LED pin as an output:

    // initialize serial communications:
    Serial.begin(9600);
    lcd.begin(16, 2);
}

void loop() {

    lcd.setCursor(12,1);
    int se25 = analogRead(A4);  
    int se50 = analogRead(A3); 
    int se75 = analogRead(A5);
    int se100 = analogRead(A2);  
    if(se100>=1000){lcd.print("100%   ");mem=100; }  
    else if(se75>=1000){lcd.print("75%   ");mem=75; } 
    else if(se50>=1000){lcd.print("50%   ");mem=50; }
    else if(se25>=1000){lcd.print("25%  ");mem=25; }
    else{lcd.print("emty");mem=0;}
    if(se100<1000&&se75<1000&&se50<1000&&se25<1000){
        digitalWrite(13,LOW);
        lcd.setCursor(12,1);
        lcd.print("emty");
    }

    if(digitalRead(6)==HIGH){digitalWrite(13,LOW);}

    if(digitalRead(6)==LOW&&mem>0) 
    {// read the value of the potentiometer:
        int tempw = 0;
        //digitalWrite(12,LOW);
        int tempa = analogRead(A1);
        delay(20); 
        int tempb = analogRead(A1);
        delay(20); 
        int tempc = analogRead(A1);
        delay(20); 
        int tempd = analogRead(A1);
        delay(20); 
        int tempe = analogRead(A1);
        delay(20); 
        int tempf = analogRead(A1);
        delay(20); 

        int avg = ((tempa + tempb + tempc + tempd + tempe + tempf ) / 6);

        int temp = avg;  
        // map it to the range of the analog out:
        temp = map(temp, 0, 1023, 0, 4900); 
        float tempx = temp * (0.1); 
        // change the analog out value:
        int tempy=analogRead(A0);
        tempy = map(tempy, 0, 1023, 0, 150);
        // print the results to the serial monitor:
        lcd.setCursor(0,0);
        lcd.print("act T=" ); 
        lcd.print(tempx);
        lcd.print("'C  " );
        lcd.setCursor(0,1);
        lcd.print("set T=");  

        lcd.print(tempy);  
        // Serial.println("'C ");
        lcd.print("'C  "); 

        if(tempy<=tempx){digitalWrite(13,LOW);}
        if(tempy>tempx){digitalWrite(13,HIGH);}

        delay(200);
    }

}
  • 1
    who voted this down? This is a legit question from a new user. – PhillyNJ Jun 11 '14 at 10:08
10

I'm assuming you already have some understanding of programming, so I won't literally explain everything line-by-line (that would probably be beyond the scope of this site, and would result in a very long answer). I'll try to explain the important points though. If you need to lookup more information about specific commands, I recommend checking out the Arduino reference.

At a very broad level, the program is taking some readings, and reporting data to a screen (LCD). I would guess that the data is the fluid level in a tank, as well as some kind of temperature reading. It's impossible to be sure about that though, as I don't know what's actually connected to the inputs.

This line creates an object for talking to the LCD screen. The numbers specify which pins it's connected to:

LiquidCrystal lcd(2, 3, 10, 7, 5, 4);

The setup() function gets run once when the device starts up, and is typically used for initialisation tasks. In this case, the lines within setup() initialise two pins as outputs (#12 and #13), initialise serial communication (which isn't actually being used), and initialise the LCD object/screen:

pinMode (12,OUTPUT);
pinMode(13,OUTPUT);
...
Serial.begin(9600);
lcd.begin(16, 2);

The rest of the program is in the loop() function, which runs repeatedly as long as the device is on.

The following lines take 4 analog-to-digital readings (from different inputs), and output the approximate result:

int se25 = analogRead(A4);  
int se50 = analogRead(A3); 
int se75 = analogRead(A5);
int se100 = analogRead(A2);  
if(se100>=1000){lcd.print("100%   ");mem=100; }  
else if(se75>=1000){lcd.print("75%   ");mem=75; } 
else if(se50>=1000){lcd.print("50%   ");mem=50; }
else if(se25>=1000){lcd.print("25%  ");mem=25; }
else{lcd.print("emty");mem=0;}
if(se100<1000&&se75<1000&&se50<1000&&se25<1000){
    digitalWrite(13,LOW);
    lcd.setCursor(12,1);
    lcd.print("emty");
}

My guess is that each of the analog readings comes from a different fluid sensor in a tank. If the top sensor shows a strong reading, the tank is considered full. Otherwise, it checks the next sensor down, and so on. If none of them shows a strong reading, the tank is considered empty. Interestingly, the condition for an empty tank is actually checked twice, which is presumably a mistake.

(Once again, bear in mind that I'm just guessing that the readings are from fluid sensors in a tank. I don't know for sure.)

The remainder of the code only runs if a particular input (#6) is low, and if the tank is not empty. Input #6 could be from some kind of override switch, but again that's just speculation.

The following block takes 6 analog readings, all from the same input. There's a short delay (20 millisecond) between each reading. The results are then averaged. This is a common approach when you want to try to eliminate noise spikes in an analog signal.

int tempa = analogRead(A1);
delay(20); 
int tempb = analogRead(A1);
delay(20); 
int tempc = analogRead(A1);
delay(20); 
int tempd = analogRead(A1);
delay(20); 
int tempe = analogRead(A1);
delay(20); 
int tempf = analogRead(A1);
delay(20); 

int avg = ((tempa + tempb + tempc + tempd + tempe + tempf ) / 6);

The next block re-maps that reading onto a different range (presumably related to the expected temperature range). It then takes another analog reading from a different input, and remaps that to a different range:

int temp = avg;  

temp = map(temp, 0, 1023, 0, 4900); 
float tempx = temp * (0.1); 

int tempy=analogRead(A0);
tempy = map(tempy, 0, 1023, 0, 150);

The following block outputs the two re-mapped readings. It looks like both of them are treated as temperatures. One is presumably from a temperature sensor, and the other is presumably from a potentiometer used to calibrated the system:

lcd.setCursor(0,0);
lcd.print("act T=" ); 
lcd.print(tempx);
lcd.print("'C  " );
lcd.setCursor(0,1);
lcd.print("set T=");  

lcd.print(tempy);  

lcd.print("'C  "); 

These lines compare the two readings from above, and set output pin 13 high or low depending on which reading is higher. I'd guess it could be connected to a warning light or similar.

if(tempy<=tempx){digitalWrite(13,LOW);}
if(tempy>tempx){digitalWrite(13,HIGH);}

Lastly, this delay stops the loop from running too fast. Interestingly though, the code only reaches this point under certain circumstances. In other circumstances, there's no limit on loop execution speed:

delay(200);

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.