Hey GolamMostafa, thanks for the code. I tried implementing that but i am not able to get to the void loop.
//slave reciever teensy
#include<Wire.h>
#include<Servo.h>
Servo myservo;
int pos;
volatile int flag1=false;
volatile byte c;
void setup() {
// put your setup code here, to run once:
Wire.begin(

; // serial comm at address 8
Wire.onReceive(receiveEvent); // create event for serial comm
Serial.begin(9600); //baud rate
myservo.writeMicroseconds(1500);
myservo.attach(8,900,2100); //servo on pwm pin 9 (c8 of teensy)
pinMode(LED_BUILTIN, OUTPUT); // led indicator
}
void receiveEvent(int howMany)
{ //activates when a byte is recieved
int c = Wire.read(); // read byte ; declare in global area : volatile byte c; volatile flag1 = false;
flag1 = true; //indication that receiveEvent() handler has been visited
Serial.println(c);
Serial.println(flag1);
}
void loop()
{
if(flag1 == true)
{
if(c==51)
{ //3=51,4=52
cut();
Serial.println("x");
delay(500);
retract();
flag1 = false;
}
}
}
void cut()
{
digitalWrite(LED_BUILTIN, HIGH); // led on when byte is available
for (pos=1500; pos<=2100;pos+=100) { // travel 60 degrees
myservo.writeMicroseconds(pos); // tell servo to go to position in variable 'pos'
delay(50); // waits 20ms for the servo to reach the position
}
}
void retract()
{
digitalWrite(LED_BUILTIN, LOW);
for (pos=2100; pos>=1500; pos-=100) { // travel 60 degrees
myservo.writeMicroseconds(pos); // tell servo to go to position in variable 'pos'
delay(50); // waits 20ms for the servo to reach the position
}
}
when i mean not getting in the void loop, the arduino monitor does not print "x" on the serial monitor, meaning the code does not reach the loop. Please confirm volatile int flag1 is correctly defined variable.