Loop braucht manchmal 7ms und manchmal auch 14ms

einstellen - dann sehen, was passiert.

Hinweis: Mach nen Klick auf den Beitrag - die Vorschau verschweigt die Grafik.

1 Like

1 Like

Okay habs haha "System == 3".

hab noch zwei weitere Fehler so gefunden.
Muss ich die lösen oder kann das seinm, dass es halt ein Fehler ist aber trotzdem problemlos klappt?

Fehler: In function 'void storeData()':
warning: comparison between signed and unsigned integer expressions [-Wsign-compare]

Ja.
mit = weist Du der Variablen einen Wert zu.
mit == vergleichst Du die Variable mit dem Wert.
Das heisst, das die Variable nach dem Durchlauf dort einen anderen Wert hat.

Das mit dem signed und unsigned ist ebenso zu behandeln.

1 Like

Okay habe:
int timer = millis();

und dann:

if (timer -  last_data_save >= flushtimer)

Denke, das sollte so passen.

Ist von der logik her meine Verwendung von if aber richtig gewesen bei:

  if (system == 3) {

    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(1000);                       // wait for a second
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
    delay(1000);
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(1000);                       // wait for a second
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
    delay(1000);
    Serial.print("calibrated");

Ich mag sicher stellen ,dass es einmal auf 3 war und dann erst aus dem setup raus kommt.
Also aktuell checkt er kurz obs 3 ist. Falls es unter 3 ist macht er einfach mit dem Rest weiter.
Brauch ich da eine Loope mit while?

Da sind noch paar Schnitzer bei.

Das sollte dringend unsigned long timer=millis() werden.

Wenn Du tatsĂ€chlich 4 Sekunden das System anhalten kannst, mag das ok sein - ich wĂŒrde das aber ganz anders lösen.

bool calibriermarker = false;
int System = 3;
void setup()
{
  Serial.begin(115200);
  Serial.println(F("Start...."));
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
  if ((System == 3) && (calibriermarker == false))
  {
    calibriermarker = true;
  }
  if (calibriermarker)
  {
    calibrate();
  }
}

void calibrate()
{
  enum {warten, an1, warten1, aus1, warten2, an2, warten3, aus2, warten4};
  static int schritt = warten;
  const unsigned long waitTime = 1000; // in ms
  static unsigned long lastmillis = 0;

  switch (schritt)
  {
    case warten:
      schritt++;
      break;
    case an1:
      digitalWrite(LED_BUILTIN, HIGH);
      lastmillis = millis();
      schritt++;
      break;
    case warten1:
      if (millis() - lastmillis > waitTime)
      {
        schritt++;
      }
      break;
    case aus1:
      digitalWrite(LED_BUILTIN, LOW);
      lastmillis = millis();
      schritt++;
      break;
    case warten2:
      if (millis() - lastmillis > waitTime)
      {
        schritt++;
      }
      break;
    case an2:
      digitalWrite(LED_BUILTIN, HIGH);
      lastmillis = millis();
      schritt++;
      break;
    case warten3:
      if (millis() - lastmillis > waitTime)
      {
        schritt++;
      }
      break;
    case aus2:
      digitalWrite(LED_BUILTIN, LOW);
      lastmillis = millis();
      schritt++;
      break;
    case warten4:
          if (millis() - lastmillis > waitTime)
      {
        schritt = warten;
        Serial.println("calibrated");
        calibriermarker = false;
      }
      break;
  }
}

Und da bei mir system (klien geschrieben) rot markiert ist und es da zu einem Konflikt kommt, nimm mal ne andere Varaiablenbezeichnung.

Na dann...

danke @my_xy_projekt , werde schauen, wie ich das implementiere. Ein haufen an Code ist das auf jeden Fall. Dachte mir eigentlich, dass das mit ein paar Zeilen getan ist.

Seit dem ich die Fehlermeldung: "vergleich zwischen unsigned long (millis()) und int" Hab ich dann die millis zum int gemacht. Nun funktioniert aber mein Code nimmer ganz. Er kommt nicht mehr in den void saveData.

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BNO055.h>
#include <utility/imumaths.h>
#include <SPI.h>
#include <SD.h>
#include <avr/dtostrf.h>
#include "BNO055_support.h"
#include <SdFat.h>
#include "SdFat.h"

// Check I2C device address and correct line below (by default address is 0x29 or 0x28)
//                                   id, address
Adafruit_BNO055 bno = Adafruit_BNO055(55, 0x28);

//creating a File
File file;

// declaring global variables
const int chipSelect = 28; // CS Pin for SD
int last_data_save = 0; // variable for the last time data was read
int counter = 0; //counts the columns with values (samples)
int timer = millis(); //convert unsigned long(millis) to int (timer)

//sensor data global
struct bno055_t myBNO;
imu::Quaternion quat;
imu::Vector<3> acc;
imu::Vector<3> gyr;
imu::Vector<3> euler;

// Name of the filename__________________________________
const char dataFileName[14] = "test11.txt";

//How often should be flushed?
int flushtimer = 100; //milliseconds

//managing sample timing
int last_data_read = 0; // variable for the last time data was read
const int SAMPLE_RATE = 100; //100 Hz
const int SAMPLE_TIME = 1000 / SAMPLE_RATE; // 1000 = 1 sec divided by the Sample Rate = Time between samples

//_________________________________________________________________________________________
// ------------------ initializeSD&Sensor ----------------------------------
void setup(void)
{
  //Initialize I2C communication
  Wire.begin();

  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);

  //Initialization of the BNO055
  BNO_Init(&myBNO); //Assigning the structure to hold information about the device

  //Configuration to NDoF mode
  bno055_set_operation_mode(OPERATION_MODE_NDOF);

  delay(1);
  Serial.begin(19200);

  //wait for the serial monitor
  //delete line below for real measurements
  //  while (!Serial) {}

  // SD-Card Initialise
  Serial.print("Initializing SD card...");
  if (!SD.begin(28)) {
    Serial.println("initialization failed!");
    while (1);
  }
  file = SD.open(dataFileName, O_CREAT | O_WRITE); //Opens file - SDFAT library

  uint8_t system, gyro, accel, mag = 0;
  bno.getCalibration(&system, &gyro, &accel, &mag);

}

void loop(void)
{
  if (millis() -  last_data_read >= SAMPLE_TIME) {
    last_data_read = millis();


    counter = counter + 1;
    storeData();

    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  }
}

void storeData() {

  /*___Sample Counter___*/

  file.print(counter);
  file.print(";");

  /*__Tracking time/millis__*/

  file.print(millis());
  file.print(";");

  /*____Calibration____*/
  uint8_t system, gyro, accel, mag = 0;
  bno.getCalibration(&system, &gyro, &accel, &mag);
  file.print(system);
  file.print(";");

  /*______Euler Angles_____*/
  imu::Vector<3> euler = bno.getVector(Adafruit_BNO055::VECTOR_EULER);

  file.print(euler.x());
  file.print(";");
  file.print(euler.y());
  file.print(";");
  file.print(euler.z());
  file.print(";");

  /*______Quaternion_____*/
  quat = bno.getQuat();

  file.print(quat.w());
  file.print(";");
  file.print(quat.y());
  file.print(";");
  file.print(quat.x());
  file.print(";");
  file.print(quat.z());
  file.print(";");

  /*______ACCELEROMETER_____*/
  acc = bno.getVector(Adafruit_BNO055::VECTOR_ACCELEROMETER);

  file.print(acc.x());
  file.print(";");
  file.print(acc.y());
  file.print(";");
  file.print(acc.z());
  file.print(";");

  /*______GYROSCOPE_____*/
  gyr = bno.getVector(Adafruit_BNO055::VECTOR_GYROSCOPE);

  file.print(gyr.x());
  file.print(";");
  file.print(gyr.y());
  file.print(";");
  file.println(gyr.z());

  if (timer -  last_data_save >= flushtimer) {
    last_data_save = millis();
    saveData();
  }
  //delay(1000);
}

void saveData() {
  stopWatch(); //starts stopwatch
  file.flush(); //writing data to SD
  stopWatch(); //stopps stopwatch
  Serial.println("Writing to SD");
}

// ------------------ Stoppuhr ----------------------------------
boolean stopWatch_started = false; // function to measure time
int stopWatch_time;
void stopWatch() {
  if (stopWatch_started == false) {
    stopWatch_time = millis();
    stopWatch_started = true;
  }
  else {
    int duration = millis() - stopWatch_time;
    Serial.print("Duartion: ");
    Serial.print(duration);
    Serial.println(" ms");

    stopWatch_started = false;
  }
}

Hier nochmal der Code.

Ich denke irgendwas bei:

  if (timer -  last_data_save >= flushtimer) {
    last_data_save = millis();
    saveData();

Haut nicht ganz hin mit den neuen int millis.

Das kann auch nicht hinhauen.
Die Funktion millis() liefert ein unsigned long zurĂŒck - also mĂŒssen timer und last_data_save vom gleichen Typ sein.

@wno158 danke fĂŒr die flotte Antwort, ich stehe grad bissl auf der Leitung.
Ich dachte, dass ich das eig ganz oben global gemacht habe:

// declaring global variables
const int chipSelect = 28; // CS Pin for SD
int last_data_save = 0; // variable for the last time data was read
int counter = 0; //counts the columns with values (samples)
int timer = millis(); //convert unsigned long(millis) to int (timer)

Hast Du leider nicht:

// declaring global variables
const int chipSelect = 28; // CS Pin for SD
unsigned long last_data_save = 0; // variable for the last time data was read
int counter = 0; //counts the columns with values (samples)
unsigned long timer = millis(); //convert unsigned long(millis) to int (timer)

Das Problem ist, dass int (nutzt zwei Byte) nur von -32768 bis +32767 zĂ€hlen kann, unsigned long (vier Byte) aber von 0 bis 4294967295. Deine angedachte Konversion kann also nicht funktionieren - alle Beteiligten mĂŒssen die gleiche und zwar die grĂ¶ĂŸte GrĂ¶ĂŸe haben.
2l Milch passen auch nicht in einen 1l Tetrapak :slight_smile:

Edit (Nachtrag):
Ich sehe da auch noch ein Namensproblem (last_data_save vs. last_data_read) und in der stopWatch() solltest Du den Typ der stopWatch_time ebenfalls anpassen.


Gruß Walter

Naja, es handelt sich wohl um einen MKR Zero:

Das ist ein 32-Bit-Ding und auch int hat da 32 Bit.

https://www.arduino.cc/reference/en/language/variables/data-types/int/:

On the Arduino Due and SAMD based boards (like MKR1000 and Zero), an int stores a 32-bit (4-byte) value.

Aber auch dort ist int signed, Zeiten sollten aber unsigned sein damit Vergleiche funktionieren.

Stimmt natĂŒrlich.

Oh - entschuldigt; das mit dem MKR hatte ich vedrÀngt.

Das sieht nur so schllimm aus, weil ich auch anders formatiere als die Standardeinstellungen der IDE sind....

Leider ist Dein Code jetzt irgendwie davon komplett befreit - da kann ich also nicht mal so nebenbei sagen, ich setzt das passend ein.

Aber das wird....

Ansonsten:
Du hast nicht das gemacht, was ich Dir vorgegeben habe. Schau Dir nochmal an, was ich Dir DRINGEND ans Herz gelegt hatte...
Das aus jedem int machen, der irendwas mit dem "ticken" des Controlers zu tun hat.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.