Array + Calibrate

</code>
#include <Servo.h>
const int num = 3 // here we establish how many sensors and motors we have
Servo myServo[num]; // here we declare there are 3 motors
int val[num];
int valCali[num];
int readPin[num] = {0,1,2}; // here we declare there 3 pins, analog 0,1,2...
int writePin[num] = {13,12,11}; // here we declare there 3 pins, digital 13,12,11...
unsigned long time;          // unsigned longs are 32bit and do not store negatives
unsigned long lastTime = 0;  // unsigned longs carry a 0 to 4,294,967,295 value range
int numReadings = 10;
int total = 0;
int average = 0;

void setup () {

  for ( int i = 0; i<num; i++){ // here we set up a 'for' loop that counts from 0 to 2
    myServo[i].attach(writePin[i]); // here we attach the motors to writePins [0,1,2] as the for statements loops

  }
  Serial.begin(9600);

}

void loop() {

  time = millis();

  if ( time-lastTime >= 900000) {  // if the time minus the lastTime the code was calibrated is greater then 15 minutes run this code
    for ( int j = 0; j<num; j++){

      valCali[j] = calibrate(readPin[j]); // valCali equals the average returned by the calibrate function
      Serial.println("yes"); // here we make sure we can see it is working
      Serial.println(valCali[j]);
    }
    lastTime = time;
  }

  for ( int i = 0; i<num; i++){
    // in this loop for each 'num' or number of sensors and servos (in this case 3) we
    // run through a standard map and write operation. In this scenario we loop so quickly the response
    // is instant. if a delay is applied the code will slow down num*delay

    val[i] = analogRead(readPin[i]);
    Serial.print(i);
    Serial.print(" : ");
    Serial.println(val[i]);
    val[i] = map(val[i], valCali[i]*.5, valCali[i], 0 ,180);
   // here we map the val of each sensor between valCali of each sensor, our max value at
   //  the time of calibration, and .5 or fifty percent of valCali.

    myServo[i].write(val[i]);
    delay(200);

  }

}

int calibrate (int inputPin) { // 'int' instead of 'void' declares that this function returns a value

  // this code is a little simpler then what was presented in class
  // the function loops through the number of reading established in the global parameters
  // and adds it to a running total. At the end of the loop the total is divided by the number
  // of readings and set to '0' for the next calling of calibrate

  for (int k = 0; k < numReadings; k++){
    total= total + analogRead(inputPin);
  }
  average = total / numReadings;
  total = 0;
  return average;  // here we return the average. This will be the value used by valCali above

}
<code>
Advertisements

About Jonathan Grinham

Designer
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s