Go Down

Topic: I need help with stopping a loop when a condition is met (Read 219 times) previous topic - next topic

Kleesse

Hey. When I send a "1" with the Bluetooth I start my while loop. That works fine. Now I want the while loop to continue until I send a "0" via bluetooth. I have tried all sorts of things but do not get it to work. Can someone help me with this? Thanks in advance.

Here is the code of the void loop();

Code: [Select]

void loop()
{
  while (Serial1.available())           
  {
    char data_received;
    data_received = Serial1.read(); 
    while (data_received == '1')
    {
      Update_MPU6050();
      PositionCheck();
    }
  }
}

pert

Post the full sketch of your best attempt at making it work.

Kleesse

Here are 2 things I tried. I did not save all the others. Im programming in Energia because I'm using a Launchpad Tiva-C controller. In energia you dont need a header file for the bluetooth. The loop starting with "1" works fine. Only the stopping with "0" doesn't work yet.
Code: [Select]

void loop()
{
  while (Serial1.available())           
  {
    char data_received;
    data_received = Serial1.read(); 
    while (data_received == '1')
    {
      Update_MPU6050();
      PositieCheck();
    }
    if (data_received == '0')
    {
      break;
    }
  }
}

A other attempt:
Code: [Select]

void loop()
{
  while (Serial1.available())           
  {
    char data_received;
    data_received = Serial1.read(); 
    while (data_received == '1')
    {
      if (data_received == '0')
      {
        break;
      }
      Update_MPU6050();
      PositieCheck();
    }
  }
}


FULL SKETCH:
Code: [Select]

#include <Wire.h>
#include <math.h>
#include "I2Cdev.h"
#include "MPU6050.h"
const int MPU_addr = 0x68;
int16_t ax, ay, az;
int16_t gx, gy, gz;
int minVal = 265;
int maxVal = 402;
double x;
double y;
double z;
int c;
unsigned long alarm_time = 0L;
MPU6050 accelgyro;

void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
}

void Update_MPU6050()
{
  Wire.begin();
  accelgyro.initialize();
  accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  int xAng = map(ax, minVal, maxVal, -90, 90);
  int yAng = map(ay, minVal, maxVal, -90, 90);
  int zAng = map(az, minVal, maxVal, -90, 90);
  x = RAD_TO_DEG * (atan2(-yAng, -zAng) + PI); // Rad to degrees
  y = RAD_TO_DEG * (atan2(-xAng, -zAng) + PI);
  z = RAD_TO_DEG * (atan2(-yAng, -xAng) + PI);
}

void PositieCheck()
{
  if (!(y >= 110 || y <= 70))
  {
    Serial1.println("t"); delay(400);
    alarm_time = millis() + 5000;
  }
  else if (alarm_time < millis())
  {
    Serial1.println("f"); delay(400);
  }
}


void loop()
{
  while (Serial1.available())           
  {
    char data_received;
    data_received = Serial1.read(); 
    while (data_received == '1')
    {
      if (data_received == '0')
      {
        break;
      }
      Update_MPU6050();
      PositieCheck();
    }
  }
}

arduarn

Maybe something like this?

Code: [Select]
bool doStuff;

void loop()
{
  if (Serial1.available()) {
    char data_received = Serial1.read();
    switch (data_received) {
      case '0': doStuff = false; break;
      case '1': doStuff = true; break;
      default: // anything else
        break;
    }
  }

  if (doStuff) {
    Update_MPU6050();
    PositieCheck();
  }
}


BTW, the sub forum Programming Questions would have been a better location for this thread rather than Gigs and Collaborations, since the latter is more for requesting/paying for work to be done.

KASSIMSAMJI

#4
Jan 12, 2019, 04:27 pm Last Edit: Jan 12, 2019, 04:29 pm by KASSIMSAMJI
Code: [Select]


// char data_received must be global

void loop() {

serial_check();

if ( data_received == '1' ) {

your_functions1();

}

if ( data_received == '0' ) {

your_functions2();

}


}



void serial_check() {

  while (Serial1.available())   {   

   data_received = Serial1.read();   
 
  }

}

Expert To Be: Machine Learning And Computer/Machine Vision (ML/CV/MV)

Kleesse


Go Up