Go Down

Topic: Last stage debugging - Help !![SolveD] (Read 3809 times) previous topic - next topic

taz3m

Mar 03, 2013, 04:48 pm Last Edit: Mar 20, 2013, 09:01 am by taz3m Reason: 1
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:
Code: [Select]

/*--- 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:

Code: [Select]

/*--- 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 :( . .
tz
.....

PaulS

Quote
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.

Code: [Select]
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.

UKHeliBob

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

lloyddean

This has me curious -

Code: [Select]

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

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


taz3m

#4
Mar 03, 2013, 06:08 pm Last Edit: Mar 04, 2013, 07:31 am by taz3m Reason: 1
Quote
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.

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


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

Quote
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.

tz
.....

taz3m


This has me curious -

Code: [Select]

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 ..
tz
.....

lloyddean

#6
Mar 03, 2013, 06:29 pm Last Edit: Mar 03, 2013, 06:31 pm by lloyddean Reason: 1
Sorry but my formatting tool screwed that up somewhat.  This is your code -



Code: [Select]

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


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


PaulS

Quote
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.

taz3m

Quote

Quote
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))

Code: [Select]

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


instead

Code: [Select]

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


and for those as well :
Code: [Select]

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

[code]
void () loop {

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



thanks .  . .[/code]
tz
.....

PaulS

Code: [Select]
void loop () {
if (TempRead == true){
float temperatureRead (){
. . .
}

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

taz3m

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..
tz
.....

PaulS

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

PeterH


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.
I only provide help via the forum - please do not contact me for private consultancy.

taz3m

hello . .

Been working a bit with the code. .

Quote

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 :


Code: [Select]

// 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
tz
.....

PeterH

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:

Code: [Select]

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:

Code: [Select]


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:

Code: [Select]

switch(++itemIdx){


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

Code: [Select]

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

I only provide help via the forum - please do not contact me for private consultancy.

Go Up