Winch System Payline in and Out (Solved) Any New Suggestions are Welcome!

Hello Everyone,

I am a noob to Arduino and I am looking for some help. I am trying to automate a winch system.
I want the winch to keep a load ranging between two voltage outputs on my load cell.

I have used bits and pieces of code I have found here and there. Most of it is from the ARDUINO Examples.

Everything is working ok until I try to add more data sets into the Index for the average, which makes me, think that my average is not working appropriately.

I am not a coder and I have been Reading the manual and experimenting but this is just kicking my butt.

It works if I use a lower numReadings (Example 25) but not a larger numReadings (Example 50).
At 50 on higher voltages, my average is becoming a negative, even though my voltage is still positive on the analog input pin. I verified with my MultiMeter.

P.S. I did read the forum http://forum.arduino.cc/index.php?topic=155767.0 Load cell - smoothing and using values in loop

Any Help is appreciated. Like I said I am very new and flipping through the C++ Books, But I know some of you have probably seen this and can explain a solution to me easily.

Cheers
RCNewt

// Winch System
//RCNEWTON V0.25
// Define the number of samples to keep track of.  The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input.  Using a constant rather than a normal variable lets
// use this value to determine the size of the readings array.
const int numReadings = 50;

int readings[numReadings];      // the readings from the analog input
int index = 0;                  // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average

int inputPin = A0;
int WinchPinUp = 11;           //Winch up
int WinchPinDown = 10;         //Winch Down


void setup()
{
  // initialize serial communication with computer:
  Serial.begin(9600);                   
  // initialize all the readings to 0: 
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0;   
pinMode (WinchPinUp, OUTPUT);  
pinMode (WinchPinDown, OUTPUT); 
}

void loop() {
  // subtract the last reading:
  total= total - readings[index];         
  // read from the sensor:  
  readings[index] = analogRead(inputPin); 
  // add the reading to the total:
  total= total + readings[index];       
  // advance to the next position in the array:  
  index = index + 1;                    

  // if we're at the end of the array...
  if (index >= numReadings)              
    // ...wrap around to the beginning: 
    index = 0;                           

  // calculate the average:
  average = (total * (5.00/1023.00)) / numReadings;         
  // send it to the computer as ASCII digits
  Serial.println(average);   
  delay(20);        // delay in between reads for stability 

  {if (average >= 3.00) digitalWrite (WinchPinDown, HIGH); // Winch up for weights over x
   else digitalWrite (WinchPinDown, LOW); // Winch is off}
   
   {if (average <= 1.50) digitalWrite (WinchPinUp, HIGH); // Winch up for weights over x
   else digitalWrite (WinchPinUp, LOW); // Winch is off
   }
}}

total is an int which cannot therefore exceed 32767. You are probably overflowing it. Try making it float.

      if (average <= 1.50) digitalWrite (WinchPinUp, HIGH); // Winch up for weights over x

average is an int so comparing it to 1.5 is not useful. Try making average a float.

Thanks for the help!

That fixed it!

Looks like it is working smooth. Going to set it up tomorrow for a full scale test run on winch W/ relays!

total is an int which cannot therefore exceed 32767. You are probably overflowing it. Try making it float.

I'm curious about this. Since total is being used to sum a series of integer values, why wouldn't you make total a long (or unsigned long), rather than a float?

Because if you average several integers the result is usually fractional?

PaulS: I'm curious about this. Since total is being used to sum a series of integer values, why wouldn't you make total a long (or unsigned long), rather than a float?

  average = (total * (5.00/1023.00)) / numReadings;

It needs to be converted to a float here anyway. Until then it could be a long admittedly.

Because if you average several integers the result is usually fractional?

That is not what [u]total[/u] is doing in OP's code.

WOW this has really opened up a lot learning to me... Thank Goodness for Google search and Index Pages in books.

Cheers RCNewt