Jump to content

repeating problems in arduino (C++) code

hazza_ob
Go to solution Solved by SpaceNugget,

Lets say you have a function called day() and it returns the day. if you have:

 

string a = day();

 

and its tuesday, a holds the value "tuesday". tomorrow a will still hold the value "tuesday" but day() will return "wednesday"

 

 

so when you write:

int moisture = analogRead(Moist);

moisture then equals the result of the function analogRead() exactly when that line is executed.

 

look through your program to see how many times you call moisture = analogRead(moist);

Only once, so you only have a single reading of the moisture. so every loop when you have:

  • float M1 = 1023 - moisture;
  • float M2 = M1 / 10;
  • float M3 = 100 - M2-22;

moisture still equals the same thing from before, so M3 never changes.

So, the idea of this program is to make a self watering plant pot which will automatically water the plant when the soil moisture goes too low, and will then stop the pump when it wet again. However, when water is being added the software gets stuck in a loop

 

the code:

#include <LiquidCrystal.h> #include <Servo.h>LiquidCrystal lcd(13, 12, 7, 6, 5, 4);int Moist = A0;int Temp = A2;int Pump = 0;      // leads to a relay for the pumpvoid setup() {     //initializing all inputs and outputs    lcd.begin(16,2);  Serial.begin(9600);  lcd.clear();     //clear lcd}void loop() {    //start of moisture reading  //read the input on A0:  int moisture = analogRead(Moist);  moisture = map(moisture, 0, 1023, 1023, 0);    float M1 = 1023 - moisture;  float M2 = M1 / 10;  float M3 = 100 - M2-22;    Serial.print("Moisture: ");  Serial.println(M3);    lcd.clear();  lcd.print("Moisture: ");  lcd.print(M3);  lcd.print("%");  lcd.setCursor(0,1);  lcd.print("Target: 75 - 90%");     if (M3 < 75) {                     //THE PROBLEM IS HERE  digitalWrite(Pump, HIGH);   for (M3 < 75; M3 <= 80;)  {       float M1 = 1023 - moisture;       float M2 = M1 / 10;        float M3 = 100 - M2-22;         Serial.print("Moisture: ");       Serial.print(M3);       Serial.println("%");       Serial.println();       delay(500);   }  }  digitalWrite(Pump, LOW);    delay(30000);  int tempVal = analogRead(Temp);    // convert the reading to voltage  float tempvoltage = (tempVal / 1024.0) * 5.0;  float temperature = (tempvoltage - .5) * 100;  Serial.print("Temperature: ");  Serial.println(temperature);      lcd.clear();    lcd.print("Temp: ");    lcd.print(temperature);    lcd.print("c");    lcd.setCursor(0,1);     Serial.println(); //clears the serial display    delay(30000); //wait 30 seconds before next check}

I believe it's something to do with the 'float' command and inproper use of it but I'm not sure. Like i said it just gets stuck on that section. Any help would be appreciated (bearing in mind this is my first project) thanks :) 

 

-Harry

Link to comment
Share on other sites

Link to post
Share on other sites

There seems to be nothing updating the moisture in that for loop, I think.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

There seems to be nothing updating the moisture in that for loop, I think.

does it not run

for (M3 < 75; M3 <= 80;)  {

every time it repeats the loop or not? or is it just when it's first initialised

Link to comment
Share on other sites

Link to post
Share on other sites

does it not run

for (M3 < 75; M3 <= 80;)  {

every time it repeats the loop or not? or is it just when it's first initialised

Oh, that loop is wrong.

 

A for loop goes like this :

for(<initialization>; <condition>; <increment/decrement/etc.>) <instruction>

The way it works is simple :

  1.     initialization is executed. Generally, this declares a counter variable, and sets it to some initial value. This is executed a single time, at the beginning of the loop.
  2.     condition is checked. If it is true, the loop continues; otherwise, the loop ends, and statement is skipped, going directly to step 5.
  3.     statement is executed. As usual, it can be either a single statement or a block enclosed in curly braces { }.
  4.     increase is executed, and the loop gets back to step 2.
  5.     the loop ends: execution continues by the next statement after it.

For example, this code will generate all integers from 0 to 10 :

for(int i = 0; i <= 10; i++) cout << i << ' ';

For loops are usually used when you have a known amount of repetitions.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

Oh, that loop is wrong.

 

A for loop goes like this :

for(<initialization>; <condition>; <increment/decrement/etc.>) <instruction>

The way it works is simple :

  1.     initialization is executed. Generally, this declares a counter variable, and sets it to some initial value. This is executed a single time, at the beginning of the loop.
  2.     condition is checked. If it is true, the loop continues; otherwise, the loop ends, and statement is skipped, going directly to step 5.
  3.     statement is executed. As usual, it can be either a single statement or a block enclosed in curly braces { }.
  4.     increase is executed, and the loop gets back to step 2.
  5.     the loop ends: execution continues by the next statement after it.

For example, this code will generate all integers from 0 to 10 :

for(int i = 0; i <= 10; i++) cout << i << ' ';

For loops are usually used when you have a known amount of repetitions.

Diagnosis was spot on but the prescription was a bit off =D.

 

You will need to change a few things around.

 

line 43: float M1 = 1023 - moisture;

  ^

                                                   this is still equal to the analog reading in line 22.

 

Analog read is returns the reading at a specific time, you need to call it again if you want to update the reading.

Link to comment
Share on other sites

Link to post
Share on other sites

For loops are usually used when you have a known amount of repetitions.

could i use a while loop instead? for example:

EDIT: i tried and i can't - same problem

while (M3 < 75) {  digitalWrite(3, HIGH);       float M1 = 1023 - moisture;       float M2 = M1 / 10;       float M3 = 100 - M2-10;       Serial.print("Moisture: ");       Serial.print(M3);       Serial.println("%");       Serial.println();       delay(500);       }  digitalWrite(3, LOW);
Link to comment
Share on other sites

Link to post
Share on other sites

Diagnosis was spot on but the prescription was a bit off =D.

 

You will need to change a few things around.

 

line 43: float M1 = 1023 - moisture;

  ^

                                                   this is still equal to the analog reading in line 22.

 

Analog read is returns the reading at a specific time, you need to call it again if you want to update the reading.

what do you mean by calling it again? could you give an example of the code? (again with the whole my first project thing haha)

Link to comment
Share on other sites

Link to post
Share on other sites

Lets say you have a function called day() and it returns the day. if you have:

 

string a = day();

 

and its tuesday, a holds the value "tuesday". tomorrow a will still hold the value "tuesday" but day() will return "wednesday"

 

 

so when you write:

int moisture = analogRead(Moist);

moisture then equals the result of the function analogRead() exactly when that line is executed.

 

look through your program to see how many times you call moisture = analogRead(moist);

Only once, so you only have a single reading of the moisture. so every loop when you have:

  • float M1 = 1023 - moisture;
  • float M2 = M1 / 10;
  • float M3 = 100 - M2-22;

moisture still equals the same thing from before, so M3 never changes.

Link to comment
Share on other sites

Link to post
Share on other sites

what do you mean by calling it again? could you give an example of the code? (again with the whole my first project thing haha)

It means the moisture hasn't change from before, so M1 always remains the same.

You need to update the moisture with a new value before you do that (or after, depends on the situation) :

moisture = analogRead(Moist);

Your code should look smth like this :

while(M3 <= 80){moisture = analogRead(Moist);M1 = 1023 - moisture;M2 = M1/10;M3 = 100-M2-22;//Print etc etc...delay(500);}

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×