我不知道该怎么办

时间:2019-05-22 19:11:09

标签: arduino

我有一个小项目,我想使用Arduino Nano,我也有一些代码。现在,我想将这两个代码加在一起并加以改进,因此我不必使用浮点数据类型,因为它必须确实准确。

我想通过底部的两个按钮来增加重量。双击增加按钮(Button2),我想启动void Zeit,然后再次双击该按钮,我要阻止它再次更改重量。

我不知道该如何解决这个问题,所以我希望有人能帮助我解决这个问题。

#define Button1 4
#define Button2 5
unsigned long Gewicht = 20;
unsigned long elapsedtime;
unsigned long starttime;
const double Milli = 1000.0;
const double Micro = 1000000.0;
const double Faktor1 = 100000.0;
const double Faktor2 = 100.0;
const double Multiplikator = 0.5;
const double Abstand = 50.0;
const double fps = 3.2808399;
double Time;
double Speed;
double Energie;
double FPS;
double Weight1;
double Weight2;
boolean rightweight = false;

struct Taster
{
  byte pin;
  bool read = true;
  bool oldRead = true;
  bool state = false;
};

Taster taster2;
Taster taster3;

void setup() {

  taster2.pin = 2;
  taster3.pin = 3;

  pinMode(taster2.pin, INPUT_PULLUP);
  pinMode(taster3.pin, INPUT_PULLUP);
  pinMode(Button1, INPUT);
  pinMode(Button2, INPUT);
  Serial.begin(115200);
}


void loop() {

  update_Taster();

  if (taster2.state) {
    Gewicht++;
  }

  if (taster3.state) {
    Gewicht--;
    if (Gewicht < 20) {
      Gewicht = 20;
    }
  }
  Weight1 = Gewicht / Faktor1;
  Weight2 = Gewicht / Faktor2;
  anzeige(Gewicht);
  zeit();
}


void update_Taster ()
{
  static unsigned long last_ms = 0;
  unsigned long ms = millis();

  taster2.state = false;
  taster3.state = false;

  if (ms - last_ms >= 40) {
    last_ms = ms;

    taster2.read = digitalRead(taster2.pin);
    taster3.read = digitalRead(taster3.pin);

    if (!taster2.read && (taster2.oldRead) ) {
      taster2.state = true;
    }

    if (!taster3.read && (taster3.oldRead) ) {
      taster3.state = true;
    }

    taster2.oldRead = taster2.read;
    taster3.oldRead = taster3.read;
  }
}


void anzeige (unsigned long gewicht)
{
  static unsigned long altesGewicht = 0;

  if (gewicht != altesGewicht)
  {
    Serial.println("");
    Serial.println(String("Gewicht: ")+Weight2+String("g"));
    Serial.println("");
    altesGewicht = gewicht;
  }
}

void zeit ()
{ 
  if (rightweight == true)
  {
    while (rightweight == false)
    {   
      while (digitalRead(Button1) == false){
      }

      starttime = micros();

      while (digitalRead(Button2) == false){
      }

      elapsedtime = (micros() - starttime);
      Time = elapsedtime / Micro;
      Speed = Abstand / Time;
      FPS = Speed * fps;
      Energie = Multiplikator * Weight1 * (Speed * Speed);

      Serial.println("");
      Serial.println(String("Gewicht: ")+Weight2+String("g"));
      Serial.println("");
      Serial.println(String("Geschwindigkeit: ")+Speed+String("m/s"));
      Serial.println("");
      Serial.println(String("Geschwindigkeit: ")+FPS+String("fps"));
      Serial.println("");
      Serial.println(String("Energie: ")+Energie+String("J"));
      Serial.println("");
    }
  }
}

0 个答案:

没有答案