pokerdaniel4:
So you are saying there is a ISR method in the library somewhere?
Yes, if you find these files in your computer's Arduino installation folder, you can see how it works: Wire.cpp, Wire.h, twi.c, twi.h.
Your code is fragile at best. You've made a couple mistakes dealing with interrupt code. First all variables shared between ISR and non-ISR code must be declared 'volatile'. Otherwise, the compiler might happily optimize out your intent.
Second, your accesses to the shared variables in the non-ISR code might not be atomic on an 8-bit processor. You should surround them with noInterrupts() / interrupts() pairs.
Finally, the code is rather clumsy. You don't need to share that many variables between the ISR and non-ISR code. The multiple 'if' statements testing for mutually exclusive conditions beg for a 'switch' statement. The use of blocking code in the 'loop()' function is also not desirable. Here's a skeletal outline of what I'd try. It's not complete and won't compile as is. But it should give you an idea of how to go about it.
volatile int c;
volatile bool newValue = false;
void loop() {
static int localC = 0;
if (newValue) {
noInterrupts();
localC = c;
newValue = false;
interrupts();
}
switch (localC) {
case 0:
break;
case 1:
servoHev.write(vinkelHev1);
delay(speed);
servoRo.write(vinkelRo2);
delay(speed);
Serial.println("Båten går fremover en gang");
servoHev.write(vinkelHev2);
delay(150);
servoRo.write(vinkelRo1);
delay(350);
servoHev.write(vinkelHev1);
delay(speed);
servoRo.write(vinkelRo2);
delay(speed);
break;
case 8:
speed -= 100; // you should put a lower limit on this
localC = 1;
break;
case 9:
speed += 100; // you should put an upper limit on this
localC = 1;
break;
case 2:
Serial.println("STOPP");
servoRo.write(8);
delay(speed);
servoHev.write(vinkelHev1);
delay(speed);
servoRo.write(8);
delay(speed);
servoHev.write(vinkelHev1);
delay(speed);
break;
case 3:
// stuff for command 3 here
break;
case 4:
// stuff for command 4 here
break;
//
//
//
//
//
default:
break;
}
}
// void receive_I2C_Event(int) { // Not the best syntax
void receive_I2C_Event(int numBytes) { // Better syntax
while (Wire.available()) {
c = Wire.read();
//Serial.println(c); // Don't use Serial.print inside an ISR
}
newValue = true;
}