attachInterrupt + bluetooth codes dosen't work together ?

I am intermediate in arduino programming…
In the code below, I want to give a required value in attachinterrrupt via bluetooth.
but the program is not working and the error I got is this;

"arduino: 23: 28: error: ‘y’ was not declared in this scope
exit status 1
expected unqualified-id before ‘if’ "

#include<SoftwareSerial.h>
SoftwareSerial bt(10,11);
int sensorpulse = 0;

void setup()
{
Serial.begin(9600);
bt.begin(9600);
attachInterrupt(0,counter, RISING);
}
void loop()
{
if (bt.available() > 0)
{
int y= bt.parseInt();
}
}
void counter()
{
sensorpulse=sensorpulse+y; bt.print("Pulse = "); bt.println(sensorpulse);
}

 int y= bt.parseInt();

This goes out of scope at the next } - make it global (GOOGLE : c++ scope). :slight_smile:

I'm about to go crazy ... please solution

jamescaner:
I'm about to go crazy ... please solution

See reply #1.

Lemme put it this way.

This part is called GLOBAL. It’s a “community pool” of data where every functions can visit.

#include<SoftwareSerial.h>
SoftwareSerial bt(10, 11);
int sensorpulse = 0;

Now let’s define the functions. Each function can only visit their OWN house AND the community pool.

This is setup function. No problem here.

void setup()
{
  Serial.begin(9600);
  bt.begin(9600);
  attachInterrupt(0, counter, RISING);
}

This is loop function. Loop has a variable inside their own house called ‘y’.

void loop()
{
  if (bt.available() > 0)
  {
    int y = bt.parseInt();
  }
}

And this is counter, which relies on a variable that is owned by loop.

void counter()
{
  sensorpulse = sensorpulse + y;    
  bt.print("Pulse = ");  bt.println(sensorpulse);
}

So, take the ‘y’ variable, and move it to the “community pool”.

I revised the code the way you said … and I try it serial monitor … still doesn’t work.

#include<SoftwareSerial.h>
const int sensor = 2;
int sensorpulse = 0;
int y;

void func(int y)
{
if (Serial.available() > 1)
{
int y= Serial.parseInt();

}
sensorpulse=sensorpulse+y; Serial.print("Pulse = "); Serial.println(sensorpulse);

}

void setup()
{
pinMode(sensor , INPUT);
Serial.begin(9600);
attachInterrupt(0,counter, RISING);
}
void loop()
{

}
void counter()
{

func(y);

}

You need to post your code using a code block. Also, you NEVER do any serial processing from an interrupt.

What exactly is your code supposed to do? What is attached to the interrupt?

Also, if y is to be shared between the main loop() processing and the interrupt it needs to be declared as volatile and interrupts need to be disabled when accessing y from loop().

Do you understand how interrupts work and what they are used for?

Hello Everyone,
In my project, I will send a time limit via Bluetooth to Arduino, and I am using an obstacle sensor, if the delay of between two pulses small time limit I will count the pulse number, if not, I will count the total time,
and at finally I will send data via Bluetooth to receive total time and total rotate

int pin;
int pinpulse, timelimit, rotate=0;
unsigned long duration, total_time=0;
unsigned long time_in, time_start, time_first;
int i=0;

void setup()
{
Serial.begin(9600);
pinMode(pin, INPUT);
attachInterrupt(digitalPinToInterrupt(pinpulse),pulserising ,RISING);
}

void loop()
{
time_start=millis();
while(int timelimit=Serial.read()>500 && timelimit<=750)
{
time_in=time_start;
pin=7;
pinpulse=7;
}

duration = pulseInLong(pin, HIGH, 30000000);
//Serial.println(duration);
if(duration<timelimit)
{
rotate++;
Serial.println(rotate); // discard this code
}
else
{
total_time=total_time+duration;
Serial.print(“total_time:”);
Serial.println(total_time);
}

while(Serial.read()==‘a’)
{
Serial.print(“total_time:”);
Serial.println(total_time);
Serial.print(“rotate:”);
Serial.println(rotate);

while(1)
{
//wait here !!!
}
}
}

void pulserising()
{
time_first=time_start-time_in;
pinpulse=4; // This pin won’t be use on Arduino board…DON’T FORGET!!!
Serial.print(“time_first”);
Serial.println(time_first);
}

  1. You need to post your code using a code blocks.
  2. You perform an attachInterrupt without pinpulse initialized.
  3. You perform pinMode() without pin being initialized
  4. pinpulse and pin should declared const since they are pin numbers. Why are you modifying them in the code?
  5. In the following while loop you re-declare timelimit; therefore the timelimit you use in the while loop is NOT the same as the global timelimit.
 while(int timelimit=Serial.read()>500 && timelimit<=750)
  1. You are sharing time_in and time_start between the main loop() processing and the interrupt. You need to declare those variables as volatile and interrupts need to be disabled when accessing them from loop().
  2. NEVER do any serial processing from an interrupt
  3. You are performing Serial.read() when there may not be any serial data to read. You should always check Serial.available() first.
  1. Code blocks. It’s on the top left of reply window. The </> icon.
  2. Setup always run first, then loop will run next.
pinMode(pin, INPUT);
attachInterrupt(digitalPinToInterrupt(pinpulse), pulserising , RISING);
pin = 7;
pinpulse = 7;

This is baking bread but you forgot to buy the eggs, so you eat the flour and then you go to grocery store to buy eggs.
3. Don’t modify pin numbers in code. Put them in GLOBAL, declare and assign, and leave them the frick alone.
4. If by “Obstacle Sensor” you mean Ultrasonic Sensor, that is not how you use an Ultrasonic Sensor.
5. What is even your interrupt.
6. If you’re using UNO/NANO/MINI/MEGA, pin 7 and 4 are NOT INTERRUPT PINS. Read up the docs here.
7. This softlocks your code if you enter value between 501 and 750.

while (int timelimit = Serial.read() > 500 && timelimit <= 750)
{
  time_in = time_start;
  pin = 7;
  pinpulse = 7;
}

Also, redeclaring variable.

int pin;
int pinpulse=2, timelimit=1400, rotate=0;
unsigned long duration, total_time=0,timer,totalPulse=0;
unsigned long time_in, time_start, time_first, time_end;
int pulseFirst=1;

void setup() 
{
  Serial.begin(9600);
  pinMode(pin, INPUT);
  attachInterrupt(digitalPinToInterrupt(pinpulse),pulserising ,RISING);

 
}

void loop()
{
  time_in = millis();
  if(Serial.read()=='a')
  {
    time_start=time_in;
    
  }
  
}

void pulserising()
{
  if(pulseFirst==1){
     time_first = time_in-time_start;
     if(time_first<timelimit)
     {
        totalPulse++;
     }
      else
      {
        total_time = total_time+time_first;
      }
      Serial.print("Pulse time : ");
      Serial.println(time_first);
      timer = time_first;
  }
  
  else{
     duration = time_in-timer;
     if(duration<timelimit)
     {
        totalPulse++;
      }
      else
      {
        total_time = total_time+duration;
      }
     Serial.print("Pulse time: ");
      Serial.println(duration);
      timer = timer+duration;
      duration=0;
  }
  pulseFirst = pulseFirst+1;
  Serial.print("Total Time: ");
  Serial.println(total_time);
  Serial.print("Total Pulse: ");
  Serial.println(totalPulse);
  Serial.println();
}

linearity64

  1. Code blocks. It’s on the top left of reply window. The </> icon. :))) THANKS

First of all, thank you very much for your help.

I made changes in the code by taking your suggestions into consideration. I got one step closer to my goal …

but time calculation doesn’t work right.

  1. You perform pinMode() without pin being initialized
  2. pinpulse and pin should declared const since they are pin numbers.
  3. You are sharing time_in and time_start between the main loop() processing and the interrupt. You need to declare those variables as volatile and interrupts need to be disabled when accessing them from loop().
  4. NEVER do any serial processing from an interrupt
  5. You are performing Serial.read() when there may not be any serial data to read. You should always check Serial.available() first.

ToddL1962:

  1. You perform pinMode() without pin being initialized
  2. pinpulse and pin should declared const since they are pin numbers.
  3. You are sharing time_in and time_start between the main loop() processing and the interrupt. You need to declare those variables as volatile and interrupts need to be disabled when accessing them from loop().
  4. NEVER do any serial processing from an interrupt
  5. You are performing Serial.read() when there may not be any serial data to read. You should always check Serial.available() first.

5 ? how can I always check Serial.available() ?

jamescaner:
5 ? how can I always check Serial.available() ?

if (Serial.available() > 0)
{
  if(Serial.read() == 'a')
  {
    // do something[/color]
  }
}

or

if (Serial.available() > 0 && Serial.read() == 'a')
{
  // do something
}