analogRead sometimes returning 0

hi all,

i'm working on a project at the moment that requires an accelerometer. i'm using 3 seperate functions which read in values from the accelerometer using analogRead.

what's going wrong is that two of the functions execute perfectly, while the third returns a value of zero. what's more, when i call up the other two functions for a second time, they also return a value of zero.

It's always regular so it couldn't be faulty wiring.

OK, thanks for letting us know.

If you want help:

Please show us a schematic of your circuit.
Please, you must show us your complete sketch. Attach your code using the </> icon on the left side of the posting menu.
Put your sketch between the code tags [code][/code]
A good photo of your wiring and any links to components, will get responses.

for (counter_see_saw=1; counter_see_saw<=5;)
{
accel_read=analogRead(5);
accel_hold=(accel_read)+(accel_hold);
counter_see_saw++;
}

accel_avg=((accel_hold)/(counter_see_saw-1));

this is the code i have within the functions to read the data from the accelerometer, all three are the same just with different variable names.

i’m sure this isnt a hardware problem, because if i run the functions in seperate scripts they all work flawlessly, it’s only when they’re all put into the same loop that they return 0.

all three are the same just with different variable names.

Why?

When we ask you to post your code, we really, really mean it. In code tags.

Nothing particularly wrong with your for loop construction, it's just a little. . . eccentric. Any reason?

accel_hold=(accel_read)+(accel_hold);

(Why) all the (brackets)?

I’m incredibly sorry, but i tried posting my code but it exceeds the 900 character limit, so i thought i’d only put up what i thought was relevant.

#include <Pololu3pi.h>
#include <PololuQTRSensors.h>
#include <OrangutanMotors.h>
#include <OrangutanAnalog.h>
#include <OrangutanLEDs.h>
#include <OrangutanLCD.h>
#include <OrangutanPushbuttons.h>
#include <OrangutanBuzzer.h>

//command abbreviations
Pololu3pi robot;
OrangutanPushbuttons buttons; 
OrangutanLCD lcd;
OrangutanMotors motors;
OrangutanBuzzer buzzer;
OrangutanLEDs leds;
OrangutanAnalog analog;

//global variables
unsigned int sensors[5];
float accel_calib_key;
int accel=5;

void setup()
{
  Serial.begin(9600);
}


void loop()
{
  //variable initialisation
  int function_call;
  int function_checker;
  int counter=1;
  int looper=1;
  
  
  accel_calib_key=accelerometer_calibration();
  clear();
 
   lcd_goto_xy(0,0);
   lcd.print("Auto:");
   lcd_goto_xy(0,1);
   lcd.print("Press A");
   delay(1000);
   clear();
   lcd_goto_xy(0,0);
   lcd.print("Manual:");
   lcd_goto_xy(0,1);
   lcd.print("Press B");
   delay(1000);
   unsigned char button=buttons.waitForPress(ANY_BUTTON);
   
   if (button == MIDDLE_BUTTON)
   {
     function_call=1;
   }
   else if (button == BOTTOM_BUTTON || TOP_BUTTON)
   {
     function_call=0;
   }
   
    
  
  clear();

  if (function_call>=1)
  { 
    for (looper=1; looper<=3;)
    {   
      lcd_goto_xy(0,0);
      lcd.print("Select");
      lcd_goto_xy(0,1);
      lcd.print("Function");
      delay(1000);
      unsigned char button=buttons.waitForPress(ANY_BUTTON);
    

      if (button == MIDDLE_BUTTON)
      {
   
        Line_follow_calibration();
        clear();
        function_checker=1;
        while (function_checker!=10)
        {
          Line_Follow_Function();
          clear();
          function_checker=Balance();   //checking if we've hit the see-saw 
          clear();
          lcd_goto_xy(0,0);
          print_long(function_checker);

        }
      }
      if (button == TOP_BUTTON) 
      {
          delay(500);
          motors.setSpeeds(50,50);
          delay(250);
          motors.setSpeeds(0,0);
          
        for (counter=1; counter<10000;)
        {
          accelerometer_drive();
          clear();
          counter=counter+1;
        }
    
      }
      looper=looper+1;
    }
  }
  
  
  
  if (function_call>=0)
  {

    
    while (function_checker<10)
    {
      Line_Follow_Function();
      clear();
      function_checker=Balance();
      clear();
      lcd_goto_xy(0,0);
      print_long(function_checker);
      delay(500);
      clear(); //checking if we've hit the see-saw 
    }
    
    for (counter=1; counter<10000;)
    {
          accelerometer_drive();
          clear();
          counter=counter+1;
    }
  }
}


float accelerometer_calibration()
{
lcd_goto_xy(0,0);
lcd.print("accel.");
lcd_goto_xy(0,1);
lcd.print("calibrate");
delay(500);
clear();

  float accel_read_calibration=0;
  float accel_hold_calibration=1;
  float accel_avg_calibration=0;
  int counter=1;
  
  
  while (counter<=10)
  {
    accel_read_calibration=analogRead(5);
    accel_hold_calibration=(accel_read_calibration)+(accel_hold_calibration);
    counter=counter+1;
  }
 
  accel_avg_calibration=((accel_hold_calibration)/(counter-1));
  
  lcd_goto_xy(0,1);
  print_long(accel_avg_calibration);
  delay(1000);
  return accel_avg_calibration;
  clear();
}








int Balance()
{
lcd_goto_xy(0,0);
lcd.print("balance");
lcd_goto_xy(0,1);
lcd.print("check");
delay(500);

  int counter_balance=1;
  float balance_read=0;
  float balance_hold=0;
  float tilt;

  balance_read=accelerometer_calibration();
  tilt=balance_read-accel_calib_key;
  
  clear();
  print_long(tilt);
  delay(1000);
  clear();
  
  if (tilt<-10 || tilt>10)
  {
    return(20);
  }
  else
  {
    return(1);
  }
  
}





void accelerometer_drive()
{
  clear();
lcd_goto_xy(0,0);
lcd.print("see_saw");

  int counter_see_saw=1;
  float accel_read=1;
  float accel_hold=1;
  float accel_avg=1;
  float accel_value=1;
 
  
  
  for (counter_see_saw=1; counter_see_saw<=5;)
  {
    accel_read=analogRead(accel);
    accel_hold=(accel_read)+(accel_hold);
    counter_see_saw=counter_see_saw+1;
  }
 
  accel_avg=((accel_hold)/(counter_see_saw-1));
  
  accel_value=accel_avg-accel_calib_key;
  
  if (accel_value>25)
  {
    accel_value=25;
  }
  if (accel_value<-25)
  {
    accel_value=-25;
  }
    if (accel_value>-5 && accel_value<0)
  {
    accel_value=-5;
  }
    if (accel_value<5 && accel_value>0)
  {
    accel_value=5;
  }
  
  lcd_goto_xy(0,1);
  print_long(accel_value);

      unsigned int position = robot.readLine(sensors, IR_EMITTERS_ON);

    if (position < 1000)
    {
      motors.setSpeeds((pow((accel_value/10),5)-5), pow((accel_value/10),5));
      delay(50);
    }
  
    else if (position < 3000)
    {
      motors.setSpeeds((pow((accel_value/10),5)), pow((accel_value/10),5));
      delay(50);
    }
    
    else
    {
      motors.setSpeeds(pow((accel_value/10),5), (pow((accel_value/10),5)-5));
      delay(50);
    }
}

I’ve removed some of the functions which aren’t relevant to fit within the 900 character limit. the same reason why i only included what i though was the relevant code in my last post. my apologies.

Where did you learn that crazy for loop construction? There's nothing syntactically wrong with it, but it makes your code hard to follow.

sorry i taught myself how to code and i must have picked up a few bad habits

I think I can only see one analog read in that code. Where are the rest?

How did your code get so big before you found this critical bug?

i worked on all the functions seperately. i got them all working seperately and it was only once i put them together that it stopped working.

So, go back to the stage that it last worked, and proceed from there.

thanks for trying to help but i've fixed the problem by changing the names of some of the variables. i don't understand how this would help but i'm not complaining.

Can't you simply isolate the three functions and call them in sequence?