R/C Tech Forums - View Single Post - The Homebuilt Dynamometer (Dyno)Thread!!!
Old 04-14-2024 | 04:28 PM
  #138  
PDR's Avatar
PDR
Tech Elite
iTrader: (31)
 
Joined: Oct 2008
Posts: 2,207
From: Sydney, Australia
Default

Originally Posted by kufman
So construct a little loop using millis() to create the delay? Or us the modulo operator and every time it is 0, rerun the main loop?
Something like this. Define a value that determines time between updates and then check each loop if that time has expired.

const int NumPortsToRead = 6;
int AnalogResult[NumPortsToRead];
volatile unsigned long TimeStamp = 0;
volatile unsigned long time1 = 0;
volatile unsigned long time2 = 0;
volatile unsigned long Oldtime1 = 0;
volatile unsigned long Oldtime2 = 0;
volatile unsigned long TempTime1 = 0;
volatile unsigned long TempTime2 = 0;
String AllResult = "";

#define UPDATE_INTERVAL 50 // Number of milliseconds between each update
unsigned long lastUpdateTime;

void setup() {
// Initialize serial communication
// Ensure that Baud rate specified here matches that selected in SimpleDyno
// Availailable Baud rates are:
// 9600, 14400, 19200, 28800, 38400, 57600, 115200
Serial.begin(115200);
// Initialize interupts (Pin2 is interrupt 0 = RPM1, Pin3 in interrupt 1 = RPM2)
attachInterrupt(0,channel1,FALLING);
// attachInterrupt(1,channel2,FALLING);

lastUpdateTime = millis();
}

void loop() {
unsigned long currentTime = millis();
if((currentTime - lastUpdateTime) > UPDATE_INTERVAL){
lastUpdateTime = currentTime;

AllResult = "";
AllResult += micros();
AllResult += ",";
AllResult += TempTime1;
AllResult += ",";
AllResult += time1;
AllResult += ",";
AllResult += TempTime2;
AllResult += ",";
AllResult += time2;
for (int Looper = 0; Looper < NumPortsToRead;Looper++){
AnalogResult[Looper] = analogRead(Looper);
AllResult += ",";
AllResult += AnalogResult[Looper];
}
Serial.println (AllResult);
Serial.flush();
}

//delay(20); //20 is default
}

//Interrupt routine for RPM1
void channel1(){
TempTime1 = micros();
time1 = TempTime1-Oldtime1;
Oldtime1 = TempTime1;

}

PS: I've got some ideas for a smarter board to give you better results.
PDR is offline