Hello forum
I have two sketches. The first is just a simple sketch to toggle an output pin by the press of a button (called button). The second reads input from a hall effect sensor, and toggles a few pins based on the calculated RPM value (called hall sensor). Both sketches works great on their own, separately uploaded to the Arduino Nano 3.0, but if i combine them, strange things happens.
The "button sketch" toggles a pin, which is also used in the "hall sensor sketch". So i guess some variables doesn't "talk" right with each other, after the two sketches are combined, but I can't figure out myself what's wrong, and what to do to get them work.
button sketch
int trykknap = 4; // the number of the input pin
int headset = 5; // the number of the output pin
int headsetstatus = LOW; // current state of output
int reading; // the input reading
int prev = LOW; // previous input reading
long time = 0; // last time the output changed states
long bounceDelay = 200; // increase this if your output is unstable (flickers)
void setup()
{
pinMode(trykknap, INPUT);
pinMode(headset, OUTPUT);
digitalWrite(trykknap, HIGH);
}
void loop()
{
reading = digitalRead(trykknap);
if (reading == HIGH && prev == LOW && millis() - time > bounceDelay) {
headsetstatus = ! headsetstatus;
time = millis();
}
digitalWrite(headset, headsetstatus);
prev = reading;
}
hall sketch
//-----------------------------------------------
volatile byte rpmcount;
unsigned int rpm;
unsigned long timeold;
int hall = 2; //hall effect sensor på pin 2
int led = 3; //LED på pin 12
int headset = 5; //headset på pin 5
void setup()
{
pinMode(hall, INPUT); // hall effect sensor pin sættes som input pin
pinMode(led, OUTPUT); // LED pin sættes som output pin
pinMode(headset, OUTPUT); // headset sættes som output pin
digitalWrite(hall, HIGH); // aktivering pull up på hall effect sensor pin 2
digitalWrite(led, HIGH);
digitalWrite(headset, LOW);
Serial.begin(9600);
attachInterrupt(0, rpm_fun, RISING);
rpmcount = 0;
rpm = 0;
timeold = 0;
}
void loop()
{
if (rpmcount >= 1) {
//RPM opdateres for hver enkelt omdrejning
rpm = 30*1000/(millis() - timeold)*rpmcount*2;
timeold = millis();
rpmcount = 0;
Serial.print("RPM""\t");
Serial.println(rpm,DEC);
if ((rpm > 60)) {
digitalWrite(led, LOW);
digitalWrite(headset, HIGH);
} else if ((rpm < 170)) {
digitalWrite(led, HIGH);
digitalWrite(headset, LOW);
}
}
}
void rpm_fun()
{
rpmcount++;
//For hver omdrejning, køres denne funktion to gange
}
//-----------------------------------------------
And this is how I combined them
//-----------------------------------------------
volatile byte rpmcount;
unsigned int rpm;
unsigned long timeold;
int hall = 2; //hall effect sensor på pin 2
int led = 3; //LED på pin 12
int headset = 5; //headset på pin 5
int trykknap = 4; // the number of the input pin
int headsetstatus = LOW; // current state of output
int reading; // the input reading
int prev = LOW; // previous input reading
long time = 0; // last time the output changed states
long bounceDelay = 200; // increase this if your output is unstable (flickers)
void setup()
{
pinMode(hall, INPUT); // hall effect sensor pin sættes som input pin
pinMode(led, OUTPUT); // LED pin sættes som output pin
pinMode(headset, OUTPUT); // headset sættes som output pin
pinMode(trykknap, INPUT);
digitalWrite(trykknap, HIGH);
digitalWrite(hall, HIGH); // aktivering pull up på hall effect sensor pin 2
digitalWrite(led, HIGH);
digitalWrite(headset, LOW);
Serial.begin(9600);
attachInterrupt(0, rpm_fun, RISING);
rpmcount = 0;
rpm = 0;
timeold = 0;
}
void loop()
{
if (rpmcount >= 1) {
//RPM opdateres for hver enkelt omdrejning
rpm = 30*1000/(millis() - timeold)*rpmcount*2;
timeold = millis();
rpmcount = 0;
Serial.print("RPM""\t");
Serial.println(rpm,DEC);
if ((rpm > 60)) {
digitalWrite(led, LOW);
digitalWrite(headset, HIGH);
} else if ((rpm < 170)) {
digitalWrite(led, HIGH);
digitalWrite(headset, LOW);
}
}
}
void rpm_fun()
{
rpmcount++;
//For hver omdrejning, køres denne funktion to gange
reading = digitalRead(trykknap);
if (reading == HIGH && prev == LOW && millis() - time > bounceDelay) {
headsetstatus = ! headsetstatus;
time = millis();
}
digitalWrite(headset, headsetstatus);
prev = reading;
}
//-----------------------------------------------
the combined sketch compiles fine, but when I run it, the only function which works is the one where increase/decrease of RPM is able to toggle pins. The push button doesn't work at all.
I hope everything is clear and understandable. The two sketches works great on their own, but doesn't work combined. How do I fix that?
Thank you very much for you time and help. I appreciate it very much!
best regards
JohannesTN