cannot switch relay

Hi I am using a standard piece of code to get rpm with a hall effect sensor (which seems t o work OK). However I want to switch a relay if rpm hits a certain level but this doesnt seem to work

code:

// read RPM

volatile int rpmcount = 0;//see http://arduino.cc/en/Reference/Volatile int rpm = 0; unsigned long lastmillis = 0;

define RELAY1 7 // pin to switch relay

void setup(){ Serial.begin(9600); attachInterrupt(0, rpm_fan, FALLING);//interrupt cero (0) is on pin two(2). pinMode(RELAY1, OUTPUT); //initialise as output for relay

}

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 = rpmcount * 60; /* Convert frecuency to RPM, note: this works for one interruption per

full rotation. For two interrups per full rotation use rpmcount * 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". Serial.println("");//(rpmcount); /print revolutions per second or Hz. And print new line or enter./

if (rpm > 300){ digitalWrite(RELAY1, HIGH); // switch on relay }

rpmcount = 0; // Restart the RPM counter lastmillis = millis(); // Uptade lasmillis attachInterrupt(0, rpm_fan, FALLING); //enable interrupt } }

void rpm_fan(){ /* this code will be executed every time the interrupt 0 (pin2) gets low.*/ rpmcount++; }

Any suggestions?

OK chaps, found the answer - relay needs to be set HIGH in setup. Lot of searches to find the simple solution. Onto next problem....

For next time…

Please put your code in its own window as seen in other posts. This can be done by placing     [code]  and [/code]  around the code or use the </> icon. This makes it easier for others to read.

How to use this forum

Weedpharma