My code is something like this:
int val;
void init() {
// some init code
Serial.begin(9600);
attachInterrupt(0, change,RISING );
}
void loop() {
while(val == 'a') { do something repeatly}
while(val == 'b') { do something repeatly }
...
while(val =='..') {do something repeatly}
}
void change() {
// read input from USB-Serial, then change val
val = Serial.read();
}
I use Serial Monitor to send data from PC to my arduino Board, but it does not work.
The program does not switch between while loops.
Short answer "You don't"
First of all interrupts are disabled in an ISR and interrupts are essential for receiving serial data.
Second, that is not what interrupts are for.
What is triggering the interrupt?
If an interrupt really is needed (which I doubt) then all it needs to do is set a flag so the main program knows the interrupt has happened.
Have a look at serial input basics for simple reliable non-blocking ways to receive data.
...R
Why isn't this at the start of loop?
void loop(){
if (Serial.available()>0){
val = Serial.read();
}
or a part of every while so the while can be exited if needed
while(val == 'a') {
//do something repeatly
if (Serial.available()>0){
val = Serial.read();
}
}