Updating sensorvalues in a while / do while loop

Below you find a project i am working on for school. It uses the 1sheeld+ module to read some sensors values from my phone. I have a problem with the do-while loop. The command in this loop OrientationSensor.getY(); reads the value of the Y-angle of my phone. This is used manytimes in this code. Based on this angle my stepper motors will turn my phone and the angle will change. This do-while loop has to be repeated until my phone is in the -1-1 degree range. The problem is that ones i enter the do while loop the sensor value doesnt change anymore altough my phone turns. Because of this the motors will keep on turning and not be stopped because the value is not updated. I think it has something to do because i am in a while loop inside the void loop() but i don't see any solutions or fix to this problem. The rest of the code works fine and the angle updates.

Ps. the variables have dutch names (hoek = angle)

/* Include required shields */
#define CUSTOM_SETTINGS
#define INCLUDE_TERMINAL_SHIELD
#define INCLUDE_ORIENTATION_SENSOR_SHIELD
#define INCLUDE_TOGGLE_BUTTON_SHIELD
#define INCLUDE_VOICE_RECOGNIZER_SHIELD
#define INCLUDE_KEYPAD_SHIELD

/* Include 1Sheeld library. */
#include <OneSheeld.h>

/* Declaring variables*/
float hoek = 0, hoek_in_rad = 0, vorige_hoek = 0;
int vorige_stappen = 0, flag = 0;
bool praten;
float x; /*Aantal cm op/af rollen*/
int aantal_stappen_motor = 0, relatief_aantal_stappen = 0, buttonState = 0;

const char stabiliseerCommand[] = "stabiliseer";


// defines pins numbers
const int stepPin = 3; 
const int dirPin = 4; 
const int stepPin2 = 6; 
const int dirPin2 = 7;
const int buttonPin = 12; 

void setup() {  
  /* Start 1Sheeld communication. */
    OneSheeld.begin();
  /* Start Serial communication. */
    Serial.begin(115200);

    pinMode(stepPin,OUTPUT); 
    pinMode(dirPin,OUTPUT);
    pinMode(stepPin2,OUTPUT); 
    pinMode(dirPin2,OUTPUT);
    pinMode(buttonPin,INPUT);

    VoiceRecognition.setOnError(error);
    VoiceRecognition.start();
}

void loop() {   
  /*Als togglebutton ingeduwt is dan in bewegen mogelijk*/ 
   if(ToggleButton.getStatus()==1){
    /*OneSheeld.delay(50);*/
    hoek = OrientationSensor.getY();
    
    /*Hoek bepaalt richting van de motoren*/
    if(hoek - vorige_hoek <= 0){
      digitalWrite(dirPin, HIGH);
      digitalWrite(dirPin2, LOW);
      Serial.println("RECHTS");
    }
    if(hoek - vorige_hoek > 0){
      digitalWrite(dirPin, LOW);
      digitalWrite(dirPin2, HIGH);
      Serial.println("LINKS");
    }
    Serial.println(hoek);
    /*Serial.println(vorige_hoek);*/
    Serial.println(hoek - vorige_hoek);
    
    /* Code hoek bepaalt aantal stappen gedraaid moet worden voor beide motoren */
    hoek_in_rad = hoek * (3.1415/180);
    x = sin(hoek_in_rad) * 10.5; /*Op/af afrollen in cm*/
  
    aantal_stappen_motor = round(x/0.0014725); /*cm omzetten naar aantal steppen dat motor moet draaien*/

    relatief_aantal_stappen = abs(aantal_stappen_motor - vorige_stappen);
    Serial.println(relatief_aantal_stappen);
    if(relatief_aantal_stappen > 50){ 
      for(int x = 0; x < relatief_aantal_stappen; x++) {
      digitalWrite(stepPin,HIGH);
      digitalWrite(stepPin2,HIGH); 
      delayMicroseconds(300); 
      digitalWrite(stepPin,LOW);
      digitalWrite(stepPin2,LOW); 
      delayMicroseconds(300);
      /*Serial.println(relatief_aantal_stappen);*/
      }
    }
    vorige_hoek = hoek;
    vorige_stappen = aantal_stappen_motor;
  }
  else{
    /*Serial.println("Niet Bewegen");*/
    hoek = OrientationSensor.getY();
    vorige_hoek = hoek;
    hoek_in_rad = hoek * (3.1415/180);
    x = sin(hoek_in_rad) * 10.5;
    aantal_stappen_motor = round(x/0.0014725);
    vorige_stappen = aantal_stappen_motor;
    /*OneSheeld.delay(200);*/

    if(VoiceRecognition.isNewCommandReceived()){
      if(!strcmp(stabiliseerCommand,VoiceRecognition.getLastCommand())){
        Serial.println("gelukt");
        hoek = OrientationSensor.getY(); 
        hoek_in_rad = hoek * (3.1415/180);
        x = sin(hoek_in_rad) * 10.5;
        aantal_stappen_motor = round(x/0.0014725);
        
        if(hoek <= 0){
          digitalWrite(dirPin, LOW);
          digitalWrite(dirPin2, HIGH);
        }
        if(hoek > 0){
          digitalWrite(dirPin, HIGH);
          digitalWrite(dirPin2, LOW);
        }

        do{
          hoek = OrientationSensor.getY(); 
          digitalWrite(stepPin,HIGH);
          digitalWrite(stepPin2,HIGH); 
          delayMicroseconds(300); 
          digitalWrite(stepPin,LOW);
          digitalWrite(stepPin2,LOW); 
          delayMicroseconds(300);
       
        }while ( abs(hoek) > 1.0);
      }
    }
    buttonState = digitalRead(buttonPin);
    if(buttonState == HIGH){
      if(flag == 0){
        flag = 1;
        digitalWrite(dirPin, LOW);
        digitalWrite(dirPin2, LOW);
        for(int x = 0; x < 800; x++) {
          digitalWrite(stepPin,HIGH);
          digitalWrite(stepPin2,HIGH); 
          delayMicroseconds(300); 
          digitalWrite(stepPin,LOW);
          digitalWrite(stepPin2,LOW); 
          delayMicroseconds(300);
      }
      }
      else{
        flag = 0;
      }
      Serial.println(buttonState);
      }
  } 
}
'''
        do
        {
          hoek = OrientationSensor.getY();
          digitalWrite(stepPin, HIGH);
          digitalWrite(stepPin2, HIGH);
          delayMicroseconds(300);
          digitalWrite(stepPin, LOW);
          digitalWrite(stepPin2, LOW);
          delayMicroseconds(300);
        }
        while ( abs(hoek) > 1.0);

Is the code actually ever executing the do/while loop ?
What do yo see if you print the value of hoek after reading it ?

Hello, yes it enters die do/while loop. I tested it by adding a serial print in the loop and it shows the same value over and over again, so the first one ( i think the value it read a few lines above when entering the if loop about voice recognition . So it is not updating the sensor value in the loop.

Rather than create loops within loop(), just let it do what it is designed to do. Every time through loop(), read the value, determine which way you need to go and take 1 step. Repeat.

You also have a lot of duplicate code within your if/else clauses that could be moved outside the if/else and only done once.

both 'hoek' and '1.0' are floating point values, abs() is an integer function.

Your code is a "Mensa puzzle", very hard to understand. Are you sure you want a do-while and not just a 'while'? Your do-while will always run at least once, so the stepper will always step once, no matter what value 'hoek' has.

No it can indeed be a while but I tried that also and that was not the problem. But i will change it back thanks :slight_smile:

I have to clean up a bit indeed, it is the first time I am using an Arduino so alot to learn :slight_smile: