Verder was het te verwachten dat een kabel een hoop extra koppel zou vragen.
Goed bezig.

Code: Select all
while ((millis() - starttime) <= looptime) // Measure the RPM for a certain amount of time
{
sensorState = digitalRead(sensorPin); // Read the status on input pin:
if (sensorState != lastSensorState) { //Is not equal to
if (sensorState == HIGH) {
RPM++;
}
lastSensorState = sensorState;
}
} //End of RPM measurement block
Code: Select all
#include <SimpleTimer.h>
SimpleTimer timer; // timer to do display writings e.d.
int timerID; // variable to store the timer ID
volatile bool puls = false; // true after eacht received puls
volatile bool halted = true; // to check if still receiving pulses
volatile unsigned long pulseT; // stores micros() at each puls
unsigned long prevT,deltaT; // for speed calculation
int PulseVal = 813; // distance unit (pulses per 100 meter travel)
Code: Select all
void setup() {
//setup interrupt on D2 (speedpulse input)
attachInterrupt(0, speedpulse, FALLING);
// Setup the 200mS timer
timerID = timer.setInterval(200, timerAction);
}
Code: Select all
// do interrupt D2 stuff, speedpulse received +++++++++++++++++++++++++++++++++++++++++++
void speedpulse(){
pulseT = micros(); // save time interupted
puls = true; // set puls
}
Code: Select all
void loop() {
if (puls){
// speed pulse received
deltaT = runningAverage(pulseT-prevT); // calc time between pulses (take average of last 10)
prevT = pulseT; // save 'time' for next round
puls = false; // clear puls
halted = false; // bool to check if minimal 1 pulse received in next round
}
// tick the timer
timer.run();
}
Code: Select all
// function calculating running average out of last 10 elements ++++++++++++++++++++++++++++++++++++++
// making this higher gives faulty speed calculations at higher speeds ( if X elements * deltaT >160mS)
long runningAverage(long M)
{
static long LM[10]; // LastMeasurements
static byte index = 0;
static long sum = 0;
// keep sum updated to improve speed.
sum -= LM[index];
LM[index] = M;
sum += LM[index];
index = (index + 1) % 10;
return sum / 10;
}
Code: Select all
// a function to be executed every 200mS +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void timerAction() {
// now calculate actual speed
if (halted){ // check is still received minimal 1 pulse in 200mS
calcSpeed = 0;
}
else{
calcSpeed = ((360000000/PulseVal)/deltaT);
}
printSpeed(calcSpeed);
halted = true;
}
Code: Select all
looptime = 500
boutencardan = 6
secfactor = (1000/looptime)
eenofandererekenfactor = xx
void setup(){
attachInterrupt(digitalPinToInterrupt(2), cardan, FALLING)
}
void loop(){
if ((millis() - starttime) >= looptime){
detachInterrupt(digitalPinToInterrupt(2))
RPM = secfactor * (1/boutencardan) * 60 * pulsen
blokgolf = RPM * eenofandererekenfactor
pulsen = 0
starttime = millis()
attachInterrupt(digitalPinToInterrupt(2), cardan, FALLING)
}
}
void cardan(){
pulsen ++
}
Code: Select all
////////// Pin definition
int OUT_PIN = LED_BUILTIN ;
int IN_PIN = 7 ;
////////// Define all constants
int bolts = 6; // The number of bolts to count
unsigned long factor = 6; // The factor to multiply one rotation of the driveshaft with to create the square wave
int treshold = 3000; // Define a treshold for the stop timer to end the squarewave
////////// Define all starting values
byte in_was = LOW; // Concept: Look for transitions; need state memory
byte in_now; // Read once per cycle
int squareState = LOW; // Variable for the square wave
int Tround = 0; // The time for the drive shaft to go round once
int count = 0; // A counter to count the six bolts of the driveshaft
int start = 0; // A timer to time the time to count six bolts
int ticker = 0; // A ticker needed to only start the wave again after being stopped after a whole rotation of the driveshaft
unsigned long freq = 1000; // The frequency of the square wave
unsigned long stoptimer = millis(); // A timer to stop the wave if it takes too long
unsigned long previousmillis = 0; // For the square wave (BlinkWithoutDelay)
unsigned long currentmillis = millis(); // For the square wave (BlinkWithoutDelay)
void setup() {
pinMode(OUT_PIN, OUTPUT);
pinMode(IN_PIN, INPUT);
Serial.begin(9600);
}
void loop()
{
in_now = digitalRead(IN_PIN); // Read the current state of the input
if (in_was == HIGH) { // Were we high or low?
if (in_now == LOW) { // If this statement is true, we have transitioned from high to low
in_was = LOW; // Set the was-state to the current state
count ++; // Start counting for six bolts to pass
stoptimer = millis() ; // Start a stop timer to end the cycle if it takes too long
// Serial.print(count); //Print result
if (count == 1) { // When the first bolt in a rotation passes, start a clock to time 6 bolts to pass
start = millis();
}
}
}
else { // If the previous state was not HIGH, ie LOW, and it if it is HIGH by now, we have again a change
if (in_now == HIGH) { // So, has transitioned from low to high
in_was = HIGH; // Set the state to the new current state
}
}
if (count == bolts) { // If we have counted the number of bolts count, there has been one rotation of the driveshaft
count = 0; // Reset the counter
Tround = (millis() - start); // Calculate the time that took the driveshaft to turn one rotation
ticker = 1; // Ticker is high after one rotation
}
unsigned long currentmillis = millis(); // Update the timer for the BlinkWithoutDelay
freq = Tround / factor; // This is the equation to fix the frequency to put out
if ((millis() - stoptimer) < treshold && ticker != 0) { // If the stop timer does not need to stop, proceed
if (currentmillis - previousmillis >= freq) { // If the difference is larger than the frequence, it is time to change this
previousmillis = currentmillis; //Save the last time the square wave changed
if (squareState == LOW) { // If the wave is low, turn it to high and vice-versa
squareState = HIGH;
} else {
squareState = LOW;
}
digitalWrite(OUT_PIN, squareState); // set the wave with the squareState of the variable:
}
}
else {
digitalWrite(OUT_PIN, LOW); // If there is no signal for the amount of the treshold, go to low
ticker = 0; // The ticker is reset
}
}
Jan Willem, ik heb je sketch even in een arduino geladen, en ondanks het vele aantal regels werkt je sketch verrassend simpel, en dat geeft helemaal niks als het microcontrollertje toch niks anders hoeft te doen.Wilmo wrote:Ik kwam er niet uit met het voortschrijdend gemiddelde en met de interrupt. Het is toch nog een stapje te hoog voor mij om dit te beheersen merk ik.
Maar ik geloof dat dit toch redelijk eenvoudige code is die ook nog lijkt te werken. Ik neem te tijd voor 6 bouten om te passeren en op die manier demp ik onregelmatigheden. Hoop ik. In plaats van een vierkante golf knippert nu het ledje op de PCB. Die factor wordt uiteraard nog anders.
Code: Select all
////////// Pin definition int OUT_PIN = LED_BUILTIN ; int IN_PIN = 7 ; ////////// Define all constants int bolts = 6; // The number of bolts to count unsigned long factor = 6; // The factor to multiply one rotation of the driveshaft with to create the square wave int treshold = 3000; // Define a treshold for the stop timer to end the squarewave ////////// Define all starting values byte in_was = LOW; // Concept: Look for transitions; need state memory byte in_now; // Read once per cycle int squareState = LOW; // Variable for the square wave int Tround = 0; // The time for the drive shaft to go round once int count = 0; // A counter to count the six bolts of the driveshaft int start = 0; // A timer to time the time to count six bolts int ticker = 0; // A ticker needed to only start the wave again after being stopped after a whole rotation of the driveshaft unsigned long freq = 1000; // The frequency of the square wave unsigned long stoptimer = millis(); // A timer to stop the wave if it takes too long unsigned long previousmillis = 0; // For the square wave (BlinkWithoutDelay) unsigned long currentmillis = millis(); // For the square wave (BlinkWithoutDelay) void setup() { pinMode(OUT_PIN, OUTPUT); pinMode(IN_PIN, INPUT); Serial.begin(9600); } void loop() { in_now = digitalRead(IN_PIN); // Read the current state of the input if (in_was == HIGH) { // Were we high or low? if (in_now == LOW) { // If this statement is true, we have transitioned from high to low in_was = LOW; // Set the was-state to the current state count ++; // Start counting for six bolts to pass stoptimer = millis() ; // Start a stop timer to end the cycle if it takes too long // Serial.print(count); //Print result if (count == 1) { // When the first bolt in a rotation passes, start a clock to time 6 bolts to pass start = millis(); } } } else { // If the previous state was not HIGH, ie LOW, and it if it is HIGH by now, we have again a change if (in_now == HIGH) { // So, has transitioned from low to high in_was = HIGH; // Set the state to the new current state } } if (count == bolts) { // If we have counted the number of bolts count, there has been one rotation of the driveshaft count = 0; // Reset the counter Tround = (millis() - start); // Calculate the time that took the driveshaft to turn one rotation ticker = 1; // Ticker is high after one rotation } unsigned long currentmillis = millis(); // Update the timer for the BlinkWithoutDelay freq = Tround / factor; // This is the equation to fix the frequency to put out if ((millis() - stoptimer) < treshold && ticker != 0) { // If the stop timer does not need to stop, proceed if (currentmillis - previousmillis >= freq) { // If the difference is larger than the frequence, it is time to change this previousmillis = currentmillis; //Save the last time the square wave changed if (squareState == LOW) { // If the wave is low, turn it to high and vice-versa squareState = HIGH; } else { squareState = LOW; } digitalWrite(OUT_PIN, squareState); // set the wave with the squareState of the variable: } } else { digitalWrite(OUT_PIN, LOW); // If there is no signal for the amount of the treshold, go to low ticker = 0; // The ticker is reset } }