Last stage debugging - Help !![SolveD]

Hello ,

I reached my last stage of debugging my prgm code, with 2 of my sensors working very fine, i mean as i expect them to work :-), my output as well work fine.

The only issue is with my soil moisture sensor, it is not flipping my damm voltage pin from pin 13 to 12. (tested with voltmeter, and in serial port always returning value 1023)

The peculiar part is with another sketch which i first drafted having only the soil moisture probe sketch, here the voltage is flipping nicely and im having my results as expected.

Second sketch:

/*--- Declaration of I/Os ---*/ 

//INPUT pins definition

// OUTPUT pins definition
const uint8_t voltageFlipPin01 = 13;    
const uint8_t voltageFlipPin02 = 12;
const uint8_t pump_Active = 11;         
const uint8_t LightOn = 10;            
const uint8_t Fan01 = 9;

// Definition of Analog pins for Soil Moisture Sensor Readings.
const uint8_t Moisture_Input = 5;       
const uint8_t photoSense = 4;           
const uint8_t TempLm35 = 3;             
                        
/*--- Declaration of Variables ---*/

// Switch case Variable
char itemIdx = 0;

// Status flag
boolean pump_Status = false;          
boolean FuncActive = false;                 
boolean LightStatus = false;                      
boolean ReadPhoto = false;
boolean TempRead = false;
boolean FanStatus = false;

// Moisture Value Variables
int Moist_Val01;
int Moist_Val02;
int Moist_Avg01;
double percentage_Moist;

int photo_Read;    
int Lm35_Read;      

float TempReadC = 0.0;
float Actualtemp;

// Timer Parameters
unsigned long currentTimer;
unsigned long timerLast = 0;
const unsigned long timerRate = 1000UL;
unsigned long lastPump = 0;
const unsigned long timerRateX = 7000UL;

void setup(){
  Serial.begin(9600);                   
  
  pinMode(Moisture_Input, INPUT);
  pinMode(photoSense,INPUT);
  pinMode(TempLm35,INPUT);
  
  pinMode(voltageFlipPin01, OUTPUT);
  pinMode(voltageFlipPin02, OUTPUT);
  pinMode(pump_Active, OUTPUT);
  pinMode(LightOn,OUTPUT);
  pinMode(Fan01,OUTPUT);
  
  digitalWrite(voltageFlipPin01, LOW);
  digitalWrite(voltageFlipPin02, LOW);
  digitalWrite(pump_Active,LOW);
  digitalWrite(LightOn,LOW);
  digitalWrite(Fan01,LOW);
}

float temperatureRead (){
  if (TempRead == true){
     //getting the Raw analog reading from the temperature sensor
 int Lm35_Read = analogRead(TempLm35);  
 
  // converting that reading to voltage
 float voltage = Lm35_Read * 5.00;
 voltage /= 1024.00; 
 // Convert voltage to Temperature
 TempReadC = (voltage) * 100.00 ;  
   
  return TempReadC; 
  }
 TempRead = false;
}

void FanControlON (){
if (FanStatus == false){
  if (Actualtemp >= 26.50){
   digitalWrite(Fan01,HIGH);
  FanStatus = true;}
}
}

void FanControlOFF (){
if (FanStatus == true);{
  if (Actualtemp <=24.50){
   digitalWrite(Fan01,LOW);
   FanStatus = false;  
 }   
}
}

void pumpControlON (){
if (FuncActive == true){
  if (pump_Status == false){
  if ( percentage_Moist <= 85.00 ){
    digitalWrite(pump_Active,HIGH);
    pump_Status = true;
}
}
}
}

void pumpControlOFF (){
if ( pump_Status == true){
  if ( currentTimer >= (lastPump + timerRateX )|| percentage_Moist >= 90.00){
    lastPump = currentTimer;
    digitalWrite(pump_Active,LOW);  
    pump_Status = false; 
  }
}
}
void LightAction (){
 if (ReadPhoto == true){
 photo_Read = analogRead(photoSense);
   if (photo_Read < 200) {
     digitalWrite(LightOn,HIGH);
           LightStatus = true;}
   else if (photo_Read > 520){
     digitalWrite(LightOn,LOW);
           LightStatus = false;}
}
}
void loop(){
 

 currentTimer = millis();
 

 if( currentTimer >= (timerLast + timerRate) ){
   timerLast = currentTimer;

 switch(++itemIdx){
     case 6: 
             itemIdx = 0; 
             break;
     
     case 0: 
             digitalWrite(voltageFlipPin01, HIGH);
             digitalWrite(voltageFlipPin02, LOW);
             break;
    
     case 1:
             Moist_Val01 = 1023 - analogRead(Moisture_Input);
             break;
    
     case 2:
             digitalWrite(voltageFlipPin01, LOW);
             digitalWrite(voltageFlipPin02, HIGH);
             break;
 
     case 3:
             Moist_Val02 = analogRead(Moisture_Input);
             Moist_Avg01 = (Moist_Val01 + Moist_Val02) / 2;
             percentage_Moist = ((Moist_Avg01/1000.00)*100.00);
             FuncActive = true;
             digitalWrite(voltageFlipPin01, LOW);
             digitalWrite(voltageFlipPin02, LOW);
             break;
     case 4:
             ReadPhoto = true; 
             break;
    
    
    
    case 5:
             TempRead = true;
             break;
   }
 }

pumpControlON ();
pumpControlOFF();
LightAction ();
Actualtemp = temperatureRead ();
FanControlON ();
FanControlOFF ();

    if ( pump_Status == true ){
      Serial.print (" Pump Status : ACTIVE ");
      Serial.println();}
      
    else if ( pump_Status == false ){
      Serial.print ( " Pump Staus : INACTIVE ");
      Serial.println();}

   if ( LightStatus == true ){
      Serial.print (" Light Status : ACTIVE ");
      Serial.println();}
      
   else if ( LightStatus == false ){
      Serial.print ( " Light Staus : INACTIVE ");
      Serial.println();}
      
   if ( FanStatus == true ){
      Serial.print (" Fan Status : ACTIVE ");
      Serial.println();}
      
  else if ( FanStatus == false ){
      Serial.print ( " Fan Staus : INACTIVE ");
      Serial.println();}

  delay(500UL);

// Photo Sensor Reading and displaying  results through LCD and Serial Port.    
  Serial.print("Photo_Reading = ");
  Serial.print(photo_Read);     // the raw analog reading A4
  
   // We'll have a few threshholds, qualitatively determined reference to table of Lux.
  if (photo_Read < 10) {
    Serial.println(" - Dark");
  } else if (photo_Read < 200) {
    Serial.println(" - Dim");
  } else if (photo_Read < 500) {
    Serial.println(" - Light");
  } else if (photo_Read < 800) {
    Serial.println(" - Bright");
  } else {
    Serial.println(" - Very bright");
ReadPhoto = false;  
}
  delay(1000);

}

First sketch: where voltage flipping occurs:

/*--- Declaration of I/Os ---*/ 
const uint8_t voltageFlipPin01 = 13;
const uint8_t voltageFlipPin02 = 12;

const uint8_t pump_Active = 11;
const uint8_t Moisture_Input = 5;


/*--- Declaration of Variables ---*/

// Switch case Variable
char itemIdx = 0;

// Pump status flag
boolean pump_Status = false;
boolean Active = false;

// Moisture Value Variables
int Moist_Val01;
int Moist_Val02;
int Moist_Avg01;
double percentage_Moist;

// Timer Parameters
unsigned long currentTimer;
unsigned long timerLast = 0;
const unsigned long timerRate = 1000UL;
unsigned long lastPump = 0;
const unsigned long timerRateX = 7000UL;
// Codes running only once at start up. Initialisation.

void setup(){
  Serial.begin(9600);
  pinMode(Moisture_Input, INPUT);
  
  pinMode(voltageFlipPin01, OUTPUT);
  pinMode(voltageFlipPin02, OUTPUT);
  pinMode(pump_Active, OUTPUT);
  
  digitalWrite(voltageFlipPin01, LOW);
  digitalWrite(voltageFlipPin02, LOW);
  digitalWrite(pump_Active,LOW);
}
void pumpControlON (){
if (Active == true){
  if ( percentage_Moist <= 80.00 ){
  pump_Status = true;
  digitalWrite(pump_Active,pump_Status);
  Active = false;
}
}
}

void pumpControlOFF (){
if ( pump_Status == true){
  if ( currentTimer >= (lastPump + timerRateX )|| percentage_Moist >= 90.00){
    lastPump = currentTimer;
    pump_Status = false; 
    digitalWrite(pump_Active,pump_Status);  
  }
}
}
// Codes running forever.

void loop(){
 
// Keep track of milliseconds elapsed. 
 currentTimer = millis();
 if( currentTimer >= (timerLast + timerRate) ){
   timerLast = currentTimer;
 switch(++itemIdx){
      
     case 5: 
             itemIdx = 0;
     
     case 0: 
             digitalWrite(voltageFlipPin01, HIGH);
             digitalWrite(voltageFlipPin02, LOW);
             break;
    
     case 1:
             Moist_Val01 = 1023 - analogRead(Moisture_Input);
             break;
    
     case 2:
             digitalWrite(voltageFlipPin01, LOW);
             digitalWrite(voltageFlipPin02, HIGH);
             break;
    
     case 3:
             Moist_Val02 = analogRead(Moisture_Input);
             Moist_Avg01 = (Moist_Val01 + Moist_Val02) / 2;
             percentage_Moist = ((Moist_Avg01/1000.00)*100.00);
             digitalWrite(voltageFlipPin01, LOW);
             digitalWrite(voltageFlipPin02, LOW);
             break;
    
    case 4: Active = true;
             
   }
 }

pumpControlON ();
pumpControlOFF();
   
    if ( pump_Status == true ){
      Serial.print (" Pump Status : ACTIVE ");}
      Serial.println();
      
    if ( pump_Status == false ){
      Serial.print ( " Pump Staus : INACTIVE ");}
      Serial.println();
  delay(500UL);

  }

Any help please, i spent the whole day today looking for my bugs . . but :frowning: . .

i spent the whole day today looking for my bugs . . but

But you didn't once use Tools + Auto Format to fix the indenting. You didn't once think that putting each { on a new line would help visualize the code structure.

Blank lines between functions are useful. Blank lines in functions are less useful. Starting a function with a blank line never is useful.

 if( currentTimer >= (timerLast + timerRate) ){

Adding time variables is not a good idea. now minus then...

Mixing millis() and delay() in the same code is rarely a good idea.

Why is itemIdx a char ?

This has me curious -

oid FanControlOFF()
{
    if ( FanStatus == true )
    {   }

    {
        if ( Actualtemp <= 24.50 )
        {
            digitalWrite(Fan01, LOW);
            FanStatus = false;
        }
    }
}

But you didn't once use Tools + Auto Format to fix the indenting. You didn't once think that putting each { on a new line would help visualize the code structure.

i do have a nice indenting in my source code, i removed all coments and extra spaces before posting here, so as not to exceed max number of characters.

Mixing millis() and delay() in the same code is rarely a good idea.

the delay is just for Serial port viewing for debugging purpose.

why is itemIdx a char

i could have used other variable as well, but its just i think a matter of saving space . . i dont really know, as i was helped upon this issue while i had to be using multiple millis, having the same time duration.

lloyddean:
This has me curious -

oid FanControlOFF()

{
    if ( FanStatus == true )
    {  }

{
        if ( Actualtemp <= 24.50 )
        {
            digitalWrite(Fan01, LOW);
            FanStatus = false;
        }
    }
}

Curious . . why ?? when conditions for my fan are to be run, i set the flag for Fanstatus to be true, and i use same to get it to shut off along with the temperature reading desired. my simulation with leds do work well.

anything i missed or something you finding suspicious here . .

thanks ..

Sorry but my formatting tool screwed that up somewhat. This is your code -

void FanControlOFF()
{
    if (FanStatus == true)
        ;


    {
        if (Actualtemp <=24.50)
        {
            digitalWrite(Fan01,LOW);
            FanStatus = false;  
        }   
    }
}

the delay is just for Serial port viewing for debugging purpose.

That may be the intended use, but delay() doesn't know that. It stops your code from doing anything else, except processing interrupts.

the delay is just for Serial port viewing for debugging purpose.

That may be the intended use, but delay() doesn't know that. It stops your code from doing anything else, except processing interrupts.

i did remove the delay from the code still, my voltage pin would not flip . .

Say if i change those code lines as such, will that have a contrast in running the whole code. (( Im at my workplace for now, so i cannot test these, i just asking a suggestion))

float temperatureRead (){
  if (TempRead == true){
. . . .
}

instead

void loop () {
 if (TempRead == true){
float temperatureRead (){
. . . 
}

and for those as well :

void LightAction (){
 if (ReadPhoto == true){
. . . . 
}

[code]
void () loop {

 if (ReadPhoto == true){
     void LightAction (){
. . . . .
}

thanks . . .[/code]

void loop () {
 if (TempRead == true){
float temperatureRead (){
. . . 
}

That won't work. You can't define a function inside a function.

ok .. i do get it ..

in anyhow, how would i remove this damm bug which is preventing my voltage from flipping..

im trying all combination since then, but any of them is not working..

do you think that the voltage flipping which is under case 0, is being missed or anything likewise.. i wanna try to put that on a different case say case 1 . .

thansks for any sugestion . ..

taz..

You've made some code changes, hopefully. Now, you need to post the current code.

taz3m:
soil moisture sensor ... pin 13

I haven't looked at your code at all, but this comment above caught my eye. If you are implementing a resistive moisture sensor then you will probably be trying to set up a voltage divider with some high resistances. In that case, the integral LED that most Arduinos have connected to pin13 might be acting as a pull-down which prevents your circuit from dividing the voltage correctly. You could try using any pin other than 13; if that then works correctly it suggests this was the problem.

hello . .

Been working a bit with the code. .

I haven't looked at your code at all, but this comment above caught my eye. If you are implementing a resistive moisture sensor then you will probably be trying to set up a voltage divider with some high resistances. In that case, the integral LED that most Arduinos have connected to pin13 might be acting as a pull-down which prevents your circuit from dividing the voltage correctly. You could try using any pin other than 13; if that then works correctly it suggests this was the problem.

I agree with you that the pull down resistor willbe adding up in the voltage divider circuit, even though changing the pin from 13 to 8 still results in my voltage not flipping. however, i did observed some more stable values.

I bring some changes here with the code, but still stick to my bug :

// Function to run water pump on conditions of soil moisture

if (FuncActive == true){
  if (pump_Status == false){
pumpControlON ();
  }
}

// Function to run off pump based on conditions
if ( pump_Status == true){
pumpControlOFF();
}

// Function to Activate LightSwitch for plants on conditions of ambient light
if (ReadPhoto == true){
LightAction ();
}
 // Function in taking Temperature Readings and saving to global variable Actualtemp
if (TempRead == true){
Actualtemp = temperatureRead ();
TempRead = false;
}

i think i will be changing the switch case variable, the itemIdx from incrementing directly as a parameter function, rather incrementing it through each case, any suggestion towards that approach.

Thanks

I tried to post this earlier but the site seems to have junked my reply - sorry if you end up with two copies.

Look here:

 if( currentTimer >= (timerLast + timerRate) ){
   timerLast = currentTimer;
 switch(++itemIdx){

Firstly, that's not a very good way to compare the times because it doesn't cope with timer overflow.

You would be better changing the code to this:

if(millis() - timerLast >= timerRate)
{
  timerLast += timerRate;
  // ...

That's not going to stop your sketch from blinking, though. I suspect the reason for that is here:

switch(++itemIdx){

This increments itemIdx but does not wrap back from five to zero. It would be better to do it like this:

const int NUM_ITEMS = 6;
itemIdx = (itemIdx + 1) % NUM_ITEMS;
switch(itemIdx)
{
  ...

Whatever you do though, once you've made any of the suggested changes, please post the entire sketch that's giving you problems - attach it if necessary rather than posting it inline.

hi peter ..
thanks for your kind response, i appreciate it loads..

i did read about the stack memory being overflow after around 50 days, but did not really know how to aproach for its preventive measures .. here you showed to me..

i was suspecting the itemIdx being my culprit the way it was incrementing.. thanks for confirming ..

i shall try out these changes once im back home, n will update all here with the entire code as attachment as requested by wildbill also ..

thanks again..

see ya ..

Hello,

Thanks for the great help peterH . .

Now my voltage pin do flip like a charm, my moisture values are quite stable and im happy that my sensors work as expected.

The modulo function do great in the switching of the case, great idea . . thanks load peterH.

I attached the source code for reference, and if any other changes and improvements can be made, i will be glad to review it and make necessary changes, while getting to learn more and new things at the same time.

Thanks again for all the responses towards this post.

My next step is to implement an LCD , to display all the real values and essential information with a pushbutton to change the mode Display.
Im looking forward to be using another switch Case function again, hope i dont run into much trouble.

However, troubles are here for us to learn more , . . . and Learning never ends . . .

Regards

Tazlim . . .

GreenHouse_Rev_F_02.ino (7.37 KB)

When are you going to fix -

	if ( FanStatus )
	    ;
	{
		FanControlOFF();
	}

And didn't you mean -

    if ( ! FanStatus )
    {
        FanControlON();
    }
    else
    {
        FanControlOFF();
    }