Help me combine two sketches please

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

In the combined code, you are only reading the switch state in the ISR that is called when the hall effect sensor fires. In the standalone sketch, the switch state is read in loop.

In the combined code, why is the switch state NOT read in loop()? What was the purpose of moving it into the ISR?

Thank you for answering me, PaulS

I know this is very stupid.. I am still learning to know the Arduino environment. Actually i just inserted the button sketch, in the end of the hall sketch, hoping for that to work (since it compiled fine). I've got no reason what so ever to put the switch state into the ISR, as I just though it would still run in the main loop, because it was placed in the bottom of the hall sketch...

What do i do, to put it back in the loop, and sort everything out?

thank you very much

What do i do, to put it back in the loop, and sort everything out?

Move:

if (reading == HIGH && prev == LOW && millis() - time > bounceDelay) {
headsetstatus = ! headsetstatus;
time = millis();
}
digitalWrite(headset, headsetstatus);
prev = reading;

into loop().

It would really help you see the structure of your code if you put each { on a new line, and nothing on the line after the }, and then used Tools + Auto Format.

I don't think I've done it right, since nothing works now, but still compiles fine. But at least I have tried

//-----------------------------------------------
 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 (reading == HIGH && prev == LOW && millis() - time > bounceDelay) {
headsetstatus = ! headsetstatus;
time = millis();
}
digitalWrite(headset, headsetstatus);
prev = reading;
   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);
 }
//-----------------------------------------------

It would really help you see the structure of your code if you put each { on a new line, and nothing on the line after the }, and then used Tools + Auto Format.

Like this, dannable?

//-----------------------------------------------
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 (reading == HIGH && prev == LOW && millis() - time > bounceDelay) 
  {
    headsetstatus = ! headsetstatus;
    time = millis();
  }
  digitalWrite(headset, headsetstatus);
  prev = reading;
  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);
}
//-----------------------------------------------
  reading = digitalRead(trykknap);

Missed one. This needs to be moved to loop(), too.

Done that, PaulS

//-----------------------------------------------
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 (reading == HIGH && prev == LOW && millis() - time > bounceDelay) 
  {
    headsetstatus = ! headsetstatus;
    time = millis();
  }
  digitalWrite(headset, headsetstatus);
  prev = reading;
  reading = digitalRead(trykknap);
  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
}
//-----------------------------------------------

The button works as supposed, but when RPM drops below 60, the headset pin isn't toggled (neither rising above 60 RPM) but the led toggles just fine.

  if (reading == HIGH && prev == LOW && millis() - time > bounceDelay) 
  {
    headsetstatus = ! headsetstatus;
    time = millis();
  }
  digitalWrite(headset, headsetstatus);
  prev = reading;
  reading = digitalRead(trykknap);

Test the value of reading. Save a copy of the value. Then get the value. What's wrong with this sequence of events?

It doesn't write headset pin high or low, only the led pin changes state

If you are going to test something, don't you think that you should have something to test, first? That something is what you read from the pin. Do the digitalRead() first.