Hello.
I'm doing a project with the Arduiono 101 and iv'e connected several sensors to it.
Each sensor needs it's own code to run in order to read the data and do some thing with it.
In order to select the code segment to run the correct code for the specific sensor, i'm using a joystick that has 2 axis and a push button.
While in the "main menu" and inside void loop,();, tilting the joystick to a certain direction calls a function outside void loop();. Inside the called function is a code that reads the sensor the user has selected.
In order to break the loop of the called function and go back to the "main menu" I have to use an interrupt by pressing the push button (must be interrupt, there are no other options).
Interrupts are enabled throughout the code.
The interrupt is called fine just for the first time, but i can't call the interrupt again and I don't know why. How can I fix this?
for example:
void setup()
{
// stuff....
pinMode(Interrupt_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(Interrupt_PIN), ISR, CHANGE); // in order to break loops after user selection
//....
}
void loop()
{
joystick_pos();
}
void ISR() // Interrupt Serivce Routine - call joystick function in order to switch sensor
{
Serial.println("INTERRUPT!!!!!!!!!!!!!!!!!!!!!!!!!");
digitalWrite(greenLED,HIGH);
analogWrite(redpin, 255);
analogWrite(bluepin, 255);
analogWrite(greenpin, 255);
joystick_pos();
}
void joystick_pos()
{
noInterrupts(); // no interrupts while user is selecting sensor
digitalWrite(greenLED,LOW);
analogWrite(redpin, 255); // Turn on Purple color on full color LED
analogWrite(bluepin, 255);
analogWrite(greenpin, 0);
user_selection = NULL;
while (user_selection == NULL)
{
int x_pos = analogRead(in_x); //read the signal of x and y and store them in variables. pos means position.
int y_pos = analogRead(in_y);
// Serial.print("X: ");
// Serial.println(x_pos);
// Serial.print("Y: ");
// Serial.println(y_pos);
if((x_pos <= (x_up+5))) // X is UP && ........
{
if(((y_center -5) <= y_pos) && ((y_pos) <= (y_center + 5))) // Y CENTER
{
Serial.println("12");
}
if((y_pos <= (y_right+5))) // Y is RIGHT
{
Serial.println("1 and a half");
}
if(((y_left -5) <= y_pos) && ((y_pos) <= (y_left + 5)))
{
Serial.println("10 and a half");
}
} // end of X UP
if(((x_center -5) <= x_pos) && ((x_pos) <= (x_center + 5))) // X is CENTER && ........
{
if(((y_center -5) <= y_pos) && ((y_pos) <= (y_center + 5))) // Y CENTER
{
Serial.println("CENTER");
}
if((y_pos <= (y_right+5))) // Y is RIGHT
{
Serial.println("3");
for (int i=0; i<=2; i++) // flash three times to show user selection is received
{
digitalWrite(greenLED, HIGH);
delay(200);
digitalWrite(greenLED, LOW);
}
mic_touch_vibration(); // user selected mic touch vibration
}
if(((y_left -5) <= y_pos) && ((y_pos) <= (y_left + 5))) // Y is LEFT
{
Serial.println("9");
digitalWrite(greenLED, HIGH);
analogWrite(redpin, 0);
analogWrite(bluepin, 0);
analogWrite(greenpin, 0);
blynk_setup();
}
} // end of X CENTER
if(((x_down -5) <= x_pos) && ((x_pos) <= (x_down + 5))) // X is DOWN && ........
{
if(((y_center -5) <= y_pos) && ((y_pos) <= (y_center + 5))) // Y CENTER
{
Serial.println("6");
for (int i=0; i<=2; i++) // flash the color LED three times (WHITE color) to show user selection is received
{
analogWrite(redpin, 255);
analogWrite(bluepin, 255);
analogWrite(greenpin, 255);
delay(200);
analogWrite(redpin, 0);
analogWrite(bluepin, 0);
analogWrite(greenpin, 0);
delay(200);
}
sharpIR(); // user selected SharpIR sensor
}
if((y_pos <= (y_right+5))) // Y is RIGHT
{
Serial.println("4 and a half");
}
if(((y_left -5) <= y_pos) && ((y_pos) <= (y_left + 5))) // Y is LEFT
{
Serial.println("7 and a half");
}
} // end of X DOWN
}
}
//....... other function I have written........
// The called functions enable interrupt and read the sensor until interrupt occurs.
//for example:
void blynk_setup()
{
interrupts();
digitalWrite(greenLED,HIGH);//turn on the green LED and turn off everything else
analogWrite(redpin, 0); //
analogWrite(bluepin, 0 ); //
analogWrite(greenpin, 0);
blynk_run();
}
void blynk_run()
{
user_selection = 1;
while(1)
{
blePeripheral.poll();
Blynk.run();
}
}