int const ledPin = 13;
int half_revolutions = 0;
int state = LOW;
int rpm = 0;
unsigned long lastmillis = 0;
void setup()
{
Serial.begin(9600);
attachInterrupt(0, rpm_fan, FALLING);
pinMode (ledPin, OUTPUT);
}
void loop(){
if (millis() - lastmillis == 1000){ //Uptade every one second, this will be equal to reading frecuency (Hz).
detachInterrupt(0);//Disable interrupt when calculating
rpm = half_revolutions * 60; // Convert frecuency to RPM, note: this works for one interruption per full rotation. For two interrups per full rotation use half_revolutions * 30.
Serial.print("RPM =\t"); //print the word "RPM" and tab.
Serial.print(rpm); // print the rpm value. Serial.print("\t Hz=\t"); //print the word "Hz".
half_revolutions = 0; // Restart the RPM counter
lastmillis = millis(); // Uptade lasmillis
attachInterrupt(0, rpm_fan, FALLING); //enable interrupt
}
}
// this code will be executed every time the interrupt 0 (pin2) gets low.
void rpm_fan()
{
digitalWrite (ledPin, state);
(state = !state);
half_revolutions++;
}
well now, that's true! i'm currently using an opto-pair to detect each round, but, it still troubles me that won't even slightest change in rpm cause unreadable display? i mean when leds are not in sync with disc groove? or then i just didn't understood what you meant by ''making graft to plot the leds'' (language barrier...sorta?)You don't need to no how many rpms it's doing just where the leds are
hmm, perhaps i should fix this first hand so i can focus only where leds should flashThe rpm of the motor driving your clock, this is fixed right at the beginning and doesn't need to change.
#include <Servo.h>
Servo esc;
int throttlepin = 0; //analog input pin for potentiometer (as voltage divider/reference)
int inPin = 2; // the number of the input pin
int outPin = 13; // the number of the output pin
int state = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long previousmillis = 0;
long starttime;
long elapsedtime;
void setup()
{
esc.attach(9);
pinMode(inPin, INPUT);
pinMode(outPin, OUTPUT);
}
void loop()
{
int throttle = analogRead(throttlepin);
throttle = map(throttle, 0, 1023, 0, 179);
esc.write(throttle);
reading = digitalRead(inPin);
if (reading == HIGH && previous == LOW)
{
if (state == HIGH)
starttime = millis();
}
previous = reading;
elapsedtime = millis () - starttime;
if (elapsedtime ==0 && elapsedtime <=4.17) //turn led on between 0-4.17ms
{
digitalWrite (outPin, HIGH);
}
if (elapsedtime >=5) //turn led off at 5ms
{
digitalWrite (outPin, LOW);
}
}
Like this motor at say 120 rpm just saying. You base your figures on that as fixed math. Motor spins 121 rpm you display looks bad. But you catch the true revolution of the disk and use that it don't matter how fast or slow it spins you'll still see the display
somehow this sounds either complex or expensiveusing a phase locked loop control for the motor driver
attachInterrupt(digitalPinToInterrupt(pin), ISR, mode);
hmm, i think i know why it misses pulses: i should use interrupts instead of digitalRead
somehow this sounds either complex or expensive
i'm planning to do it this manner too after this projectnot a bunch of leds spinning on a disk.
oh no, no control, only variable speed control via potentiometer"phase locked loop control for the motor driver" there.
i had in mind using interrupts but to me it's bit confusing, but the way i see it, interrupts interrupt code when they detect change? unlike digitalwirte that is constantly checked?attachInterrupt(digitalPinToInterrupt(pin), ISR, mode);
yeah, i was thinking also of using that, instead of back-emfYou already have your little slot opto on there for example, so you could just directly read the spindle rpm and adjust your motor driver accordingly, that would also provide closed loop control
speaking of this, i made an circuit around 555, and this works better than arduino-controlset a fixed speed for the motor
int pin = 13;
volatile int state = LOW;
void setup() {
pinMode(pin, OUTPUT);
attachInterrupt(digitalPinToInterrupt(2), blink, FALLING);
}
void loop()
{
//timing calculation here?
}
void blink() {
digitalWrite(pin, state);
state = !state;
}
int const outpin = 13;
int state = LOW;
int reading;
int previous = LOW;
volatile long time = 0;
volatile long previousmillis = 0;
volatile long starttime;
volatile long elapsedtime;
void setup() {
pinMode(outpin, OUTPUT);
attachInterrupt(digitalPinToInterrupt(2), blink, CHANGE);
}
void loop()
{
reading = state;
if (reading == HIGH && previous == LOW)
{
if (state == HIGH)
starttime = millis();
}
previous = reading;
elapsedtime = millis () - starttime;
if (elapsedtime ==0 && elapsedtime <=4.17) //turn led on between 0-4.17ms
{
digitalWrite (outpin, HIGH);
}
if (elapsedtime >=5) //turn led off at 5ms
{
digitalWrite (outpin, LOW);
}
}
void blink() {
digitalWrite(outpin, state);
state = !state;
Serial.println(elapsedtime);
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?