OK!
So i tried a new way.
Now i just let the interrupt increase revcount, when it is above a certain value (20), the arduino prints
revcount and the elapsed time since the last print.
I let the processing program handle all the calculations, hz, rpm, power and so on.
Since i dont have my dyno at home, i wrote a code that sends fake revcount and time readings with a delay of 1ms to my laptop.
The processing software can read and handle all the data from the fake readings at this high speed.
So this way i have basicly minimized the work for the arduino and let the laptop do it instead.
3ghz is faster than 16mhz right?
Ill try this tomorrow.
Fake serial print code
/*
VeraDyno
Fredrik Johansson 2013
Purpose:
Send rpmcounts and millis to processing
*/
void setup() {
Serial.begin(115200); //Faster baud?
}
void loop() {
Serial.print("20");
Serial.print(" ");
Serial.println("550");
delay(1);
Serial.print("20");
Serial.print(" ");
Serial.println("500");
delay(1);
Serial.print("20");
Serial.print(" ");
Serial.println("450");
delay(1);
Serial.print("20");
Serial.print(" ");
Serial.println("400");
delay(1);
Serial.print("20");
Serial.print(" ");
Serial.println("350");
delay(1);
Serial.print("20");
Serial.print(" ");
Serial.println("300");
delay(1);
Serial.print("20");
Serial.print(" ");
Serial.println("250");
delay(1);
Serial.print("20");
Serial.print(" ");
Serial.println("200");
delay(1);
Serial.print("20");
Serial.print(" ");
Serial.println("150");
delay(1);
Serial.print("20");
Serial.print(" ");
Serial.println("100");
delay(1);
delay(10000);
}
The real code for the arduino:
// Variables
unsigned long lastmillis=0;
unsigned long deltamillis=0;
volatile byte revcount = 0;
void setup()
{
Serial.begin(115200);
attachInterrupt(0, revInterrupt, RISING);
}
void loop()
{
if (revcount>20) {
deltamillis = millis() - lastmillis;
lastmillis = millis();
Serial.print(revcount); //Print current hz
Serial.print(" "); //Print space
Serial.println(deltamillis); //Print elapsed time
revcount=0;
}
}
void revInterrupt() {
revcount++;
}
The processing code:
/*
Veradyno
Fredrik Johansson 2013
*/
import processing.serial.*;
Serial port;
PrintWriter output;
PImage img;
// Variables
//Data
String[] splitdata;
long hz=0;
long hzold=0;
float watt;
float wattold=0;
float rpm;
float rpmold;
int revcount = 0;
int revcountold=0;
float rads2;
float rads1;
long maxhzrulle=100;
int dtime=1; // Time between readings
int timeold=1; // To keep time of readingss
String fileExtension = ".txt";
// Colors
long bgcolor=40;
long bgcolor2=60;
long txtcolor=240;
// Time and date
long year = year();
long month = month();
long day = day();
long hour = hour();
long minute = minute();
long second = second();
float inertia = 0.054405996665378;
void setup() {
// GUI---------------------------
background(bgcolor); //Background color
size(100, 210); // Picture size
img = loadImage("logo.JPG"); // Image
image(img, 270, 0);
size(800, 600); //Screen size
frame.setTitle("Vera Dyno"); // Title of window
textSize(32);
fill(txtcolor);
text("Vera dyno", 10, 50); //Header
fill(bgcolor2);
strokeWeight(0);
rect(50, 100, 610, 450); // Plot window
fill(255);
fill(txtcolor);
textSize(20);
text("Rpm:", 670, 130); // Measurements
text("Millis:", 670, 170); // Measurements
textSize(18);
text("0", 17, 555);
text("Watt", 4, 335);
text("1000", 3, 120);
text("0", 45, 580);
text("Rpm", 320, 580);
text(maxhzrulle, 600, 580);
// Serial communication
port = new Serial(this, "COM6", 115200); //port baud 9600
port.bufferUntil('\n'); // buffer until prlonged line prlongln
//Logging file specification
output = createWriter(year + "-" + month + "-" + day + " " + hour + "_" + minute + "_" + second + ".txt");
output.print("HZ "); // Headers for logging file
output.println("WATT");
}
void draw() {
}
void serialEvent (Serial port) {
splitdata = splitTokens(port.readStringUntil('\n'));
revcount= int(splitdata[0]);
dtime = int(splitdata[1]);
revcount=revcount*1000;
hz=(revcount/dtime);
if (hz>hzold) {
rads2 = (hz)*2*3.14;
rads1 = (hzold)*2*3.14;
float E2=0.5*inertia*(rads2*rads2);
float E1=0.5*inertia*(rads1*rads1);
watt=((E2-E1)/dtime)*1000;
rads1=rads2;
// // plotting
rpm=hz*60;
rpm=map(rpm, 0, 5000, 0, 250);
float watt2=map(watt, 0, 50000, 0, 100);
strokeWeight(3);
stroke(0);
line(rpmold+50, 550-wattold, rpm+50, 550-watt2);
rpmold=rpm;
wattold=watt2;
output.print(hz);
output.print(" ");
output.println(watt); //Prlonging to file
output.flush(); // Writes the remaining data to the file
hzold=hz;
}
}