Wednesday, May 28, 2014

Initial Arduino Code

The process of developing the Arduino code has also a great way for me to organize my ideas. It forces me to write down there verminous aspects of the project in a symbolic, systematic and sequenced structure. My initial


// Perpetual Windmill USB Charger for Arduino
// thatchek
// Last Modified -- May 28, 2014

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

int pinRPM = 7;  // 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]

int pinMotor = 9;  // output pin for PWM fan motor ESC
float battVolts = 7.4;  // the nominal voltage of the battery in use
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
float fanVal = 0;  // the PWM value (0-255) to output to the ESC

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


////////////////////////////////////////////
void setup()
{
  pinMode(pinRPM, INPUT);
  pinMode(pinMotor, OUTPUT);
  pinMode(pinUSB, OUTPUT);
  digitalWrite(pinUSB, LOW);

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


////////////////////////////////////////////
void loop()
{
  // What is the mill's RPM //
    sensor[0] = digitalRead(pinRPM);
    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 0 to 255 //
    if ( millRPM >= 10 )
    {
      fanRPM = 2 * millRPM;
      if ( fanRPM > 500 ) fanRPM = 500;
      fanVal = ( fanRPM * 255 ) / ( kV * battVolts );
    }
    else
    {
      fanVal = 0;
    }
    analogWrite(pinMotor, fanVal);


  // Control USB Charging Port //
    if ( millRPM >= 50 ) digitalWrite(pinUSB, HIGH);
    else digitalWrite(pinUSB, LOW);
 

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


  delay(10);


}

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

No comments:

Post a Comment