Quick shot at new code:
Code: Select all /*
BasicAmperageMonitor-Feb-27-2012
The circuit:
* LED on board at pin 13
* Motor control on pin 3 (PWM pin)
* pushbutton attached to pin 7 from +5V
* 10K resistor attached to pin 7 from ground
* ADC on analog pin 0
Serial settings:
Bits per second 38400
*/
#include <TimerOne.h>
const int buttonPin = 7; //Trigger switch attached to digital pin 7
const int ledPin = 13; //onboard LED on pin 13
const int MotorPin = 3; //Motor drive pin (on MOSFET) connected to PWM pin at pin 3
const int CurrentSensor = 0; //Current flow sensor attached to Analog 0
const int MotorSpeed = 255; //PWM value for motor, max 255
int buttonState = 0; // Trigger button state
volatile int IMain = 0; // Direct ADC reading from current sensor
long ReadAmpsInterval = 1000; // This will provide 10 readings per AEG cycle
void setup() {
Serial.begin(115200); // High output rate to accept about 25 samples per motor cycle
pinMode(MotorPin, OUTPUT); //Set the MotorPin as an output
analogWrite(MotorPin, 0); // make sure that the MotorPin is Off
pinMode(ledPin, OUTPUT); // Set the LED Pin as an output
pinMode(buttonPin, INPUT); // set the trigger pin as an input
Timer1.initialize(ReadAmpsInterval); //set up the interrupt timer to read at the ReadAmpsInterval
Timer1.attachInterrupt(ReadAmps); //The function to call every interval
}
void loop() {
/* A simple loop to drive the motor while the trigger is held down */
buttonState = digitalRead(buttonPin);
while(buttonState == HIGH) {
analogWrite(MotorPin, MotorSpeed);
digitalWrite(ledPin,HIGH);
}
analogWrite(MotorPin, 0);
digitalWrite(ledPin, LOW);
}
void ReadAmps() //this is the interrupt function
{
IMain = analogRead(CurrentSensor); //simply reads the CurrentSensor and stores the value in IMain
/* Write out the sensor reading, MSB then LSB */
//Serial.print(IMain,DEC);
Serial.write(48 + IMain/100);
Serial.write(48 + (IMain%100)/10);
Serial.write(48 + IMain%10);
/* Send a tab */
//Serial.write(0x09);
/* Write out the millisecond count (repeats after 50 days of run time) */
// unsigned long currentTime = millis();
//Serial.print(currentTime,DEC);
/* Send a carriage return */
Serial.write(0x0D);
}
I changed the serial code so it doesn't have to go through a conversion. This should be a bit faster than Serial.print as it doesn't have to find the size of the string (we know it to be 3 digits long) and I simply add the number 48 to produce the ascii equivalent.
Plug in this code and pull the trigger. It may lock the firing sequence down again. I didn't have it activate only when the trigger is pulled as there is no switch debouncing.
Dan