Friday, May 30, 2014

itWORKS

Everything put together and running!!!!


Final Arduino Code

I finally got the code to work and do everything I want (mostly)...


// Perpetual Windmill USB Charger for Arduino
// Kyle Thatcher
// Last Modified -- May 30, 2014

////////////////////////////////////////////
////////////////////////////////////////////
#include <Servo.h>

int pinMill = A0;  // input pin for the brushless motor phase sensor
int sensor[] = {0,0};  // stores the current sensor reading [0] and the previous sensor reading [1]
long i = 0;  // keeps count of revolutions of mill motor
long millRPM = 0;  // stores the RPM calculated from the mill motor's phase sensor
unsigned long t[] = {0,0};  // the elapsed time [0] and previous time at elapse [1]

Servo fan;  // creates a servo object
int pinFan = 9;  // output pin for PPM (servo) fan motor ESC
float battVolts = 7.4;  // the nominal voltage of the battery in use (7.4 or 11.1)
int kV = 1000;  // the kV value (RPM/Volt) of fan motor
int fanRPM = 0;  // stores the fans intended RPM
int maxFanRPM = 1000;  // the maximum fan RPM desired
int initVal = 18;  // the minimum value that the ESC will use
int val;  // the PPM value (18-179) to output to the ESC

int pinUSB = 4;  // the output pin for controlling USB charging port


////////////////////////////////////////////
void setup()
{
  pinMode(pinMill, INPUT);
  fan.attach(pinFan);
  pinMode(pinUSB, OUTPUT);

  fan.write(initVal);
  digitalWrite(pinUSB, LOW);

  Serial.begin(9600);
  Serial.println("Initializing");
}


////////////////////////////////////////////
void loop()
{
  // What is the mill's RPM //
    sensor[0] = digitalRead(pinMill);
    if ( sensor[0] == 1 && sensor[1] == 0) ++i;
    sensor[1] = sensor[0];
    t[0] = millis() - t[1];
    if ( t[0]  >= 200 )
    {
      millRPM = 1000 * i / t[0];
      i = 0;
      t[1] = millis();
    }


  // Control ESC (fan speed) values from 18-179  //
    if ( millRPM >= 15 )
    {
      //fanRPM = 2 * millRPM;
      //if ( fanRPM > 100 ) fanRPM = 100;
      //val = ( fanRPM * 255 ) / ( kV * battVolts );    
   
      val = 75;
    }
    else
    {
      val = initVal;
    }
 
    fan.write(val);


  // Control USB Charging Port //
    if ( millRPM >= 20 ) digitalWrite(pinUSB, 1);
    else digitalWrite(pinUSB, 0);
    //digitalWrite(pinUSB, 1);


  // Print Debug Values //
    //Serial.println(millRPM);


  delay(1);


}

////////////////////////////////////////////
////////////////////////////////////////////