Sending serial data once during a rotation (routine) of the MPU6050 sensor

Hello LMRians. How's everyone doing?

I have been fiddling around with an MPU-6050 and Arduino. Basically, a current idea is to flip in between Power-Point Slides based on the direction of rotation of the sensor. In short:

1) I am using Gobetwino to receive serial data from Arduino and execute commands. Thus, I cannot make many modifications on this end to handle strange/large number of serial readings as it executes an action per serial read. For eg: to emulate the pressing of the "right" key, it cannot receive more than one signal from the Arduino. It cannot be modified to handle 10 signals to  execute one command.

2) When the current protype system I have rotates about the positive Z-axis the next slide should appear. Think of it as rotating a pen vertically.

3) All the acceleration and gyroscopic values are instantaneous and so, whenever I set the condition to "rotates about positive Z-axis" and insert Serial.print("_______") , it also gets executed repeteadly. In other words, if Gobetwino requires a SINGLE command string, say "#S|SENDK|[0&{ENTER}]#", every 180 degrees of rotation, it's continually getting sent. (Obviously!)

Hope it's not confusing. The summary is : Gobetwino requires one command string per instruction and I am having a couple of problems limiting it. Here's a part of the code where all this is happening. I know,  the code is embarassing! Did whatever that made it give correct LED indications. 

if ( angle_z > 0 ) { // the system is rotating about positive Z-axis
if (angle_z > 180) set_last_read_angle_data(millis(), 0, 0, 0, 0, 0, 0); // after every 180 degrees, reset the values
if (unfiltered_gyro_angle_z > 30) { // a gap of 30 degrees after every routine
Serial.print("#S|SENDK|[0&{ENTER}]#"); // send this command string to serial monitor (pressing ENTER takes us to the next slide)
digitalWrite(led, HIGH); // indication
}
else {
digitalWrite(led, LOW); // no activity in the slide
}
}
if ( angle_z < 0 ) { // the system is rotating about negative Z-axis
if (angle_z < -180) set_last_read_angle_data(millis(), 0, 0, 0, 0, 0, 0); // after every 180 degrees, reset the values
if (unfiltered_gyro_angle_z < -30) { // a gap of 30 degrees after every routine
Serial.print("#S|SENDK|[0&{LEFT}]#"); // send this command string to serial monitor (pressing left returns us to the previous slide)
digitalWrite(led, HIGH); // indication
}
else {
digitalWrite(led, LOW); // no activity in the slide
}
}

(I have used separate LEDs for indications. Just have mentioned the same one in the code.Shortened it.)
So, would be awesome to hear what ideas you have. I am not great at coding. So yes, it’s pretty much fixing a few lines of bad code! :smiley:
Thanks guys. Let me know if other parts of the code/the code itself is/are required. :slight_smile:

If I am following your question,

could you not set in you code:

Value=Sensor.read();

If (value==rotate right) {

While (value=rotate right) {

Value=sensor.read();

}//endwhile

Serial.print(value);

}//endif

Set your various values as constants and make similar blocks of code for each condition. You will only get one print per move then.

I hope you realize my code is more pseudo than real code.

Thank you birdmun. Got the

Thank you birdmun. Got the idea of your pseudocode clearly. I will try to test it out. Perhaps I will try to set a flag, as bdk suggested, to indicate turnings.