The link establishes what I'm trying to achieve. Is this possible with the Arduino language? I'm using PWM to control a 3-wire fan - Red,Black, and Yellow. I'm not sure if I completely understand it, but since you're capable of reading the fans RPM when the duty cycle is 255, I've been trying to set the duty cycle to this value right before I begin my calculations for getting the RPM, then setting it back to what ever value it was before that. However, I haven't been able to implement this correctly.
Any input would be helpful, I have a feeling that if it possible, it requires much more than the simplistic approach I've been trying.
Here's my code I'm currently using just in case, it doesn't have any of my attempts in it though:
/*To disable interrupts:
cli(); // disable global interrupts
and to enable them:
sei(); // enable interrupts
*/
//Varibles used for calculations
int NbTopsFan;
int Calc;
int ldr; //Light sensor
//Pin Locations
int OnGreenLed = 4;
int OffRedLed = 7;
int SpeedOrangeLed = 11;
int buttonPin = 8; //For Button
int transistor = 9;
int currentState = LOW;
int hallsensor = 2;
//int ldr = 0;
// ldr = analogRead(0)/4;
typedef struct{ //Defines the structure for multiple fans and their dividers
char fantype;
unsigned int fandiv;
}fanspec;
//Definitions of the fans
fanspec fanspace[3]={{0,1},{1,2},{2,8}};
char fan = 1; //This is the varible used to select the fan and it's divider, set 1 for unipole hall effect sensor
//and 2 for bipole hall effect sensor
void rpm () //This is the function that the interupt calls
{
NbTopsFan++;
}
//This is the setup function where the serial port is initialised,and the interrupt is attached
void setup()
{
pinMode (hallsensor, INPUT);
pinMode(OffRedLed,OUTPUT);
pinMode(OnGreenLed,OUTPUT);
pinMode(SpeedOrangeLed,OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);
attachInterrupt(0, rpm, RISING);
setPwmFrequency(9,1);
}
void loop ()
{
// read the state of the pushbutton value:
ldr = analogRead(A1);
currentState = digitalRead(buttonPin);
if(currentState == HIGH){
digitalWrite(OffRedLed, LOW);
digitalWrite(OnGreenLed, HIGH);
analogWrite(SpeedOrangeLed,ldr/4);
// delay(10);
analogWrite(transistor,ldr/4);
}
else{
// turn LED off:
digitalWrite(OffRedLed, HIGH);
digitalWrite(OnGreenLed, LOW);
digitalWrite(SpeedOrangeLed,LOW);
analogWrite(transistor,0);
}
NbTopsFan = 0; //Set NbTops to 0 ready for calculations
sei(); //Enables interrupts
delay (1000); //Wait 1 second
cli(); //Disable interrupts
Calc = ((NbTopsFan * 60)/fanspace[fan].fandiv); //Times NbTopsFan (which is apprioxiamately the fequency the fan is spinning at) by 60 seconds before dividing by the fan's divider
Serial.print (Calc, DEC); //Prints the number calculated above
Serial.print (" rpm\r Button State: "); //Prints " rpm" and a new line
Serial.print(currentState);
Serial.print(" ldr reading = ");
Serial.print(ldr);
Serial.print("\n");
}
void setPwmFrequency(int pin, int divisor) {
byte mode;
if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
switch(divisor) {
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 64: mode = 0x03; break;
case 256: mode = 0x04; break;
case 1024: mode = 0x05; break;
default: return;
}
if(pin == 5 || pin == 6) {
TCCR0B = TCCR0B & 0b11111000 | mode;
} else {
TCCR1B = TCCR1B & 0b11111000 | mode;
}
} else if(pin == 3 || pin == 11) {
switch(divisor) {
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 32: mode = 0x03; break;
case 64: mode = 0x04; break;
case 128: mode = 0x05; break;
case 256: mode = 0x06; break;
case 1024: mode = 0x7; break;
default: return;
}
TCCR2B = TCCR2B & 0b11111000 | mode;
}
}