Brauche Hilfe beim Code (Toleranz für Interger)

int trimmertoset = round(targetvaluetrim);

  
  const int DEADBAND = 2; 

  if (abs(trimmer - trimmertoset) <= DEADBAND) {
    stepdirection = 0; 
  } 
  else {    
    if (trimmer > trimmertoset) {
      digitalWrite(DIR_PIN, LOW);
      digitalWrite(EN_PIN, LOW);
      stepdirection = 1;
    }
    if (trimmer < trimmertoset) {
      digitalWrite(DIR_PIN, HIGH);
      digitalWrite(EN_PIN, LOW);
      stepdirection = 2;
    } 
  }

  if (stepdirection == 1 || stepdirection == 2) {
   digitalWrite(STEP_PIN, HIGH);
   digitalWrite(STEP_PIN, LOW);
  }

Wie kann man den Motor solange drehen lassen bis er die wirkliche trimmertoset Position erreicht hat aber dann nicht dreht wenn der Wert 2 höher oder 2 niedriger ist, zurzeit stopt der Motor sobald der Wert innerhalb der Toleranz liegt, bedeutet er stopt nicht auf dem richtigem trimmertoset Position. Wäre dankbar für jede hilfe

Da Du Deinen Code bis ins absolut unlesbare verstümmelt hast, hier ein Versuch meiner eingestaubten Glaskugel.

{
  if (trimmer > trimmertoset && stepdirection == 0)
  {
    digitalWrite(DIR_PIN, LOW);
    digitalWrite(EN_PIN, LOW);
    stepdirection = 1;
  }
  if (trimmer < trimmertoset && stepdirection == 0)
  {
    digitalWrite(DIR_PIN, HIGH);
    digitalWrite(EN_PIN, LOW);
    stepdirection = 2;
  }
}

(Du willst bestimmt einen endlichen Automaten bauen)

Bitte immer den ganzen Sketch posten. Programmschnipsel sind wertlos ( Erfahrungswert: in 90% der Fälle liegt der Fehler in dem nicht gezeigten Code). Ausserdem ist ein Schaltbild und eine Beschreibung, was Du eigentlich machen willst sehr wertvoll um Unterstützung zu leisten.

#define INTERCEPTOR_PIN A0
#define THROTTLE1_PIN A1
#define THROTTLE2_PIN A2
#define FLAPS_PIN A10
#define TRIMMER_PIN A3

#define ENGINE1_PIN 5
#define ENGINE2_PIN 6
#define ENGINE_MODE_CRANK_PIN 8
#define ENGINE_MODE_IGN_PIN 9
#define BRAKE_STOP_PIN 4
#define AUTOTHROTTLE_PIN 7

#define STEP_PIN 15
#define DIR_PIN 14
#define EN_PIN 16

//change these to change trim and limits. Calibrating your joystick in Windows achieves the same thing
#define INTERCEPTOR_MAX 613
#define INTERCEPTOR_MIN 459
#define INTERCEPTOR_TRIM 0
#define INTERCEPTOR_INVERT 1

//to invert an axis, change 1 to -1
#define THROTTLE1_MAX 805
#define THROTTLE1_REVERSE 438
#define THROTTLE1_MIN 292
#define THROTTLE1_TRIM 0
#define THROTTLE1_INVERT 1

#define THROTTLE2_MAX 898
#define THROTTLE2_REVERSE 517
#define THROTTLE2_MIN 363
#define THROTTLE2_TRIM 0
#define THROTTLE2_INVERT -1

#define FLAPS_MAX 345
#define FLAPS_MIN 188
#define FLAPS_TRIM 0
#define FLAPS_INVERT -1

#define TRIMMER_MIN 84
#define TRIMMER_MAX 898
#define TRIMMER_TRIM 0
#define TRIMMER_INVERT 1
#define TRIMMER_MIN_POSITION 80
#define TRIMMER_MAX_POSITION 900


#define ENGINE1_PIN_INVERTED false
#define ENGINE2_PIN_INVERTED false
#define ENGINE_MODE_CRANK_PIN_INVERTED false
#define ENGINE_MODE_IGN_PIN_INVERTED false
#define BRAKE_STOP_PIN_INVERTED false
#define AUTOTHROTTLE_PIN_INVERTED true

#include <Joystick.h>
#include "timer-api.h"
#include <math.h>

#define ADC_MEASUREMENTS 10
int adc_buffer[5][ADC_MEASUREMENTS];



Joystick_ Joystick(0x04, JOYSTICK_TYPE_JOYSTICK,
                   24, 0,                // Button Count, Hat Switch Count
                   false, false, false,  // X and Y, but no Z Axis
                   true, true, false,    //   Rx, Ry, or Rz
                   true, true,           // Yes rudder, yes throttle
                   false, false, true);  //  accelerator, brake, or steering




void setup() {
  // Initialize Button Pins
  Serial.begin(115200);
  //while(!Serial);
  Serial.println("Start");

  pinMode(ENGINE1_PIN, INPUT_PULLUP);
  pinMode(ENGINE2_PIN, INPUT_PULLUP);
  pinMode(ENGINE_MODE_CRANK_PIN, INPUT_PULLUP);
  pinMode(ENGINE_MODE_IGN_PIN, INPUT_PULLUP);
  pinMode(BRAKE_STOP_PIN, INPUT_PULLUP);
  pinMode(AUTOTHROTTLE_PIN, INPUT_PULLUP);

  pinMode(STEP_PIN, OUTPUT);
  pinMode(DIR_PIN, OUTPUT);
  pinMode(EN_PIN, OUTPUT);


  timer_init_ISR_2KHz(TIMER_DEFAULT);
  // Initialize Joystick Library
  Joystick.begin(false);  //false = dont send automatically. We will sendState() at the end of the loop
  Joystick.setRxAxisRange(-100, 100);
  Joystick.setRyAxisRange(-100, 100);
  Joystick.setRudderRange(-100, 100);
  Joystick.setThrottleRange(-100, 100);

  Joystick.setSteeringRange(-1350, 1350);

}

int interceptor;
int throttle1;
int throttle2;
int flaps;
int trimmer;

int interceptor_joy;
int throttle1_joy;
int throttle2_joy;
int flaps_joy;
int trimmer_joy;

bool engine1;
bool engine2;
bool engine_mode_crank;
bool engine_mode_ign;
bool brake_stop;
bool autothrottle;
bool interceptor_arm;

bool engine1_master_joy;
bool engine2_master_joy;

bool engine_mode_crank_joy;
bool engine_mode_ign_joy;
bool brake_stop_joy;
bool autothrottle_joy;
bool interceptor_arm_joy;

int trimmer_steps_set; 
int stepdirection = 1;
double targetvaluetrim = 0;
String aircraft = "ns";

void timer_handle_interrupts(int timer) {

  if (aircraft == "FBW") {
    if (trimmer_steps_set > 1271) targetvaluetrim = map(trimmer_steps_set, 1271, 1350, 866, 895);
    else if (trimmer_steps_set > 1157) targetvaluetrim = map(trimmer_steps_set, 1157, 1280, 819, 866);
    else if (trimmer_steps_set > 1060) targetvaluetrim = map(trimmer_steps_set, 1060, 1157, 770, 819);
    else if (trimmer_steps_set > 973) targetvaluetrim = map(trimmer_steps_set, 973, 1060, 723, 770);
    else if (trimmer_steps_set > 891) targetvaluetrim = map(trimmer_steps_set, 891, 973, 672, 723);
    else if (trimmer_steps_set > 808) targetvaluetrim = map(trimmer_steps_set, 808, 891, 626, 672);
    else if (trimmer_steps_set > 724) targetvaluetrim = map(trimmer_steps_set, 724, 808, 584, 626);
    else if (trimmer_steps_set > 633) targetvaluetrim = map(trimmer_steps_set, 633, 724, 540, 584);
    else if (trimmer_steps_set > 528) targetvaluetrim = map(trimmer_steps_set, 528, 633, 499, 540);
    else if (trimmer_steps_set > 400) targetvaluetrim = map(trimmer_steps_set, 400, 528, 450, 499);
    else if (trimmer_steps_set > 300) targetvaluetrim = map(trimmer_steps_set, 300, 400, 407, 450);
    else if (trimmer_steps_set > 200) targetvaluetrim = map(trimmer_steps_set, 200, 300, 359, 407);
    else if (trimmer_steps_set > 100) targetvaluetrim = map(trimmer_steps_set, 100, 200, 310, 359);
    else if (trimmer_steps_set > -1) targetvaluetrim = map(trimmer_steps_set, 0, 100, 263, 310);
    else if (trimmer_steps_set > -100) targetvaluetrim = map(trimmer_steps_set, 0, -100, 263, 215); 
    else if (trimmer_steps_set > -200) targetvaluetrim = map(trimmer_steps_set, -100, -200, 215, 167);
    else if (trimmer_steps_set > -300) targetvaluetrim = map(trimmer_steps_set, -200, -300, 167, 117);
    else if (trimmer_steps_set > -400) targetvaluetrim = map(trimmer_steps_set, -300, -400, 117, 67);
  }
  if (aircraft == "HEADWIND") {
    if (trimmer_steps_set > 1300) targetvaluetrim = map(trimmer_steps_set, 1300, 1350, 866, 895);
    else if (trimmer_steps_set > 1200) targetvaluetrim = map(trimmer_steps_set, 1200, 1300, 819, 866);
    else if (trimmer_steps_set > 1100) targetvaluetrim = map(trimmer_steps_set, 1100, 1200, 770, 819);
    else if (trimmer_steps_set > 1000) targetvaluetrim = map(trimmer_steps_set, 1000, 1100, 723, 770);
    else if (trimmer_steps_set > 900) targetvaluetrim = map(trimmer_steps_set, 900, 1000, 672, 723);
    else if (trimmer_steps_set > 800) targetvaluetrim = map(trimmer_steps_set, 800, 900, 626, 672);
    else if (trimmer_steps_set > 700) targetvaluetrim = map(trimmer_steps_set, 700, 800, 584, 626);
    else if (trimmer_steps_set > 600) targetvaluetrim = map(trimmer_steps_set, 600, 700, 540, 584);
    else if (trimmer_steps_set > 500) targetvaluetrim = map(trimmer_steps_set, 500, 600, 499, 540);
    else if (trimmer_steps_set > 400) targetvaluetrim = map(trimmer_steps_set, 400, 500, 450, 499);
    else if (trimmer_steps_set > 300) targetvaluetrim = map(trimmer_steps_set, 300, 400, 407, 450);
    else if (trimmer_steps_set > 200) targetvaluetrim = map(trimmer_steps_set, 200, 300, 359, 407);
    else if (trimmer_steps_set > 100) targetvaluetrim = map(trimmer_steps_set, 100, 200, 310, 359);
    else if (trimmer_steps_set > -1) targetvaluetrim = map(trimmer_steps_set, 0, 100, 263, 310);
    else if (trimmer_steps_set > -100) targetvaluetrim = map(trimmer_steps_set, 0, -100, 263, 215); 
    else if (trimmer_steps_set > -200) targetvaluetrim = map(trimmer_steps_set, -100, -200, 215, 167);
    else if (trimmer_steps_set > -300) targetvaluetrim = map(trimmer_steps_set, -200, -300, 167, 117);
    else if (trimmer_steps_set > -400) targetvaluetrim = map(trimmer_steps_set, -300, -400, 117, 67);
  }
  else if (aircraft == "ns"){
    if (trimmer_steps_set > -1) targetvaluetrim = map(trimmer_steps_set, 0, 100, 263, 310);
  }
  
  

  
  int trimmertoset = round(targetvaluetrim);

  
  const int DEADBAND = 2;  // Adjust this value based on your needs

  if (abs(trimmer - trimmertoset) <= DEADBAND) {
    stepdirection = 0;  // Within the deadband, don't change direction
  } 
  else {    
    if (trimmer > trimmertoset && stepdirection == 0)
    {
      digitalWrite(DIR_PIN, LOW);
      digitalWrite(EN_PIN, LOW);
      stepdirection = 1;
    }
    if (trimmer < trimmertoset && stepdirection == 0)
    {
      digitalWrite(DIR_PIN, HIGH);
      digitalWrite(EN_PIN, LOW);
      stepdirection = 2;
    }
  }

  if (stepdirection == 1 || stepdirection == 2) {
   digitalWrite(STEP_PIN, HIGH);
   digitalWrite(STEP_PIN, LOW);
  }




}


void read_values() {

  engine1 = ENGINE1_PIN_INVERTED ? !digitalRead(ENGINE1_PIN) : digitalRead(ENGINE1_PIN);
  engine2 = ENGINE2_PIN_INVERTED ? !digitalRead(ENGINE2_PIN) : digitalRead(ENGINE2_PIN);

  engine_mode_crank = ENGINE_MODE_CRANK_PIN_INVERTED ? !digitalRead(ENGINE_MODE_CRANK_PIN) : digitalRead(ENGINE_MODE_CRANK_PIN);
  engine_mode_ign = ENGINE_MODE_IGN_PIN_INVERTED ? !digitalRead(ENGINE_MODE_IGN_PIN) : digitalRead(ENGINE_MODE_IGN_PIN);
  brake_stop = BRAKE_STOP_PIN_INVERTED ? !digitalRead(BRAKE_STOP_PIN) : digitalRead(BRAKE_STOP_PIN);
  autothrottle = AUTOTHROTTLE_PIN_INVERTED ? !digitalRead(AUTOTHROTTLE_PIN) : digitalRead(AUTOTHROTTLE_PIN);


  static int index;
  uint8_t i = 0;
  adc_buffer[i++][index] = 512 - THROTTLE1_INVERT * (512 - analogRead(THROTTLE1_PIN));
  adc_buffer[i++][index] = 512 - THROTTLE2_INVERT * (512 - analogRead(THROTTLE2_PIN));
  adc_buffer[i++][index] = 512 - FLAPS_INVERT * (512 - analogRead(FLAPS_PIN));
  adc_buffer[i++][index] = 512 - TRIMMER_INVERT * (512 - analogRead(TRIMMER_PIN));

  int value_temp = analogRead(INTERCEPTOR_PIN);
  if (value_temp < 300) {
    interceptor_arm = true;
  } else {
    adc_buffer[i++][index] = 512 - INTERCEPTOR_INVERT * (512 - value_temp);
    interceptor_arm = false;
  }

  long value = 0;
  for (uint8_t j = 0; j < ADC_MEASUREMENTS; j++) {
    value += adc_buffer[0][j];
  }
  throttle1 = value / ADC_MEASUREMENTS;

  value = 0;
  for (uint8_t j = 0; j < ADC_MEASUREMENTS; j++) {
    value += adc_buffer[1][j];
  }
  throttle2 = value / ADC_MEASUREMENTS;

  value = 0;
  for (uint8_t j = 0; j < ADC_MEASUREMENTS; j++) {
    value += adc_buffer[2][j];
  }
  flaps = value / ADC_MEASUREMENTS;

  value = 0;
  for (uint8_t j = 0; j < ADC_MEASUREMENTS; j++) {
    value += adc_buffer[3][j];
  }
  trimmer = value / ADC_MEASUREMENTS;

  value = 0;
  for (uint8_t j = 0; j < ADC_MEASUREMENTS; j++) {
    value += adc_buffer[4][j];
  }
  interceptor = value / ADC_MEASUREMENTS;

  if (++index == ADC_MEASUREMENTS)
    index = 0;
}

void send_debug() {
  Serial.print("INTERCEPTOR=");
  Serial.print(interceptor);
  Serial.print(" THROTTLE1=");
  Serial.print(throttle1);
  Serial.print(" THROTTLE2=");
  Serial.print(throttle2);
  Serial.print(" FLAPS=");
  Serial.print(flaps);
  Serial.print(" TRIM=");
  Serial.print(trimmer);

  Serial.print(" ENGINE1=");
  Serial.print(engine1);
  Serial.print(" ENGINE2=");
  Serial.print(engine2);
  Serial.print(" ENGINE_MODE_CRANK=");
  Serial.print(engine_mode_crank);
  Serial.print(" ENGINE_MODE_IGN=");
  Serial.print(engine_mode_ign);
  Serial.print(" BRAKE_STOP=");
  Serial.print(brake_stop);
  Serial.print(" INTERCEPTOR_ARM=");
  Serial.print(interceptor_arm);
  Serial.print(" AUTOTHROTTLE=");
  Serial.print(autothrottle);

  Serial.println("");
}

float roundToOneDecimal(int value) {
  return round(value * 10.0) / 10.0;
}

void loop() {
  static bool set_info;
  read_values();
 

  send_debug();
  if (Serial.available()) {
    
    int serial = Serial.read();
    int aircraftindex;
    if (serial == 'i') {
      Serial.println("TU0");
    }
    if (serial == 'a') {
      aircraftindex = Serial.parseInt();
      if (aircraftindex == 1) {
        aircraft = "HEADWIND";
      }
      if (aircraftindex == 2) {
        aircraft = "FBW";
      }
    }
    if (serial == 's') {

    trimmer_steps_set = Serial.parseInt();
    //Serial.println(trimmer_steps_set);
  }


  }
  
  //read buttons. Change pins and button numbers here, if you want to have different number connected to different pin
  int i = 0;
  Joystick.setButton(i++, engine1);
  Joystick.setButton(i++, !engine1);
  Joystick.setButton(i++, engine2);
  Joystick.setButton(i++, !engine2);

  Joystick.setButton(i++, false);
  Joystick.setButton(i++, false);
  Joystick.setButton(i++, brake_stop);
  Joystick.setButton(i++, !brake_stop);

  Joystick.setButton(i++, engine_mode_crank);
  Joystick.setButton(i++, !(engine_mode_crank | engine_mode_ign));
  Joystick.setButton(i++, engine_mode_ign);

  Joystick.setButton(i++, autothrottle);
  Joystick.setButton(i++, interceptor_arm);
  Joystick.setButton(i++, !interceptor_arm);

  //read analog axes

  int value = map(interceptor + INTERCEPTOR_TRIM, INTERCEPTOR_MIN, INTERCEPTOR_MAX, -100, 100);
  Joystick.setRxAxis(value);
  value = map(flaps + FLAPS_TRIM, FLAPS_MIN, FLAPS_MAX, -100, 100);
  Joystick.setRyAxis(value);

  if (throttle1 >= (THROTTLE1_REVERSE + 30))
    value = map(throttle1 + THROTTLE1_TRIM, THROTTLE1_REVERSE, THROTTLE1_MAX, 0, 100);
  else if (throttle1 <= (THROTTLE1_REVERSE - 30))
    value = map(throttle1 + THROTTLE1_TRIM, THROTTLE1_MIN, THROTTLE1_REVERSE, -100 / 5, 0);
  else
    value = 0;
  if (value > 95) value = 100;
  else if (value > 85) value = 95;
  else if (value > 70) value = 90;
  Joystick.setThrottle(value);
  //Serial.println(value);
  if (throttle2 >= (THROTTLE2_REVERSE + 30))
    value = map(throttle2 + THROTTLE2_TRIM, THROTTLE2_REVERSE, THROTTLE2_MAX, 0, 100);
  else if (throttle2 <= (THROTTLE2_REVERSE - 30))
    value = map(throttle2 + THROTTLE2_TRIM, THROTTLE2_MIN, THROTTLE2_REVERSE, -100 / 5, 0);
  else
    value = 0;
  if (value > 95) value = 100;
  else if (value > 85) value = 95;
  else if (value > 70) value = 90;
  Joystick.setRudder(value);

  //Serial.println(trimmer_cur);

  

  Joystick.sendState();
  //delay(10);
  /*
  Serial.print(trimmer_steps_set);
  Serial.print(" ");
  Serial.print(trimmer_steps_cur);
  Serial.print(" ");
  Serial.println(trimmer_cur*STEPS_MULT);
  */
}

Ich weiß das der Code etwas unordentlich, erstmal danke für deine schnelle antwort, also der trimmertoset wert wird berechnet mit der map funktion ich möchte das der motor so lange in die richtung läuft das der trimmer wert (vom potionmeter) dann stehen bleibt sobald der trimmer wert auf der trimmertoset position ist da beim potionmeter die werte sich ändern zwischendurch möchte ich das sich der motor erst dann bewegt sobald die trimmertoset postion sich mehr als zwei geändert hat weil sonst kommt es vor das der motor vor und zurück geht.

Ichhab jetzt mal kurz drauf geschaut - schwere Kost.
Aber:
Wenn Du willst, das der stehen bleibt, wenn die Potiposition erreicht ist, dann muss das:

so werden:

  if (abs(trimmer - trimmertoset) == 0) {
    stepdirection = 0;  // Within the deadband, don't change direction
  } 

Der Wiederanlauf erst dann, wenn die Hysterese überschritten ist

wird dann

if (trimmer > (trimmertoset+DEADBAND) && stepdirection == 0)`

und

if (trimmer < (trimmertoset-DEADBAND) && stepdirection == 0)

Wenn ich das richtig verstanden habe.
Notfalls musst ein paar Serielle Ausgaben reinbauen und vergleichen.

Aber ich weiss, auf was Du raus willst.
Dein analoger INPUT pendelt um 2 digit. Das ist so. Steht im Datenblatt
Das kann man abfangen. Ich muss aber erstmal den Code aufdröseln.

Nochmals, vielen dank für die so schnelle Antwort, ich werde es jetzt ausprobieren im spiel es scheint schonmal richtig zu funktionieren.

Es funktioniert, danke dir :)))))

1 Like

Ich biete Dir mal noch eine Variante an:

  int trimmertoset = round(targetvaluetrim);
  const int DEADBAND = 2;  // Adjust this value based on your needs
  if (abs(trimmer - trimmertoset) == 0)
  {
    stepdirection = 0;  // Within the deadband, don't change direction
  }
  if (stepdirection == 0)
  {
    if (trimmer > trimmertoset + DEADBAND)
    {
      digitalWrite(DIR_PIN, LOW);
      digitalWrite(EN_PIN, LOW);
      stepdirection = 1;
    }
    if (trimmer < trimmertoset - DEADBAND)
    {
      digitalWrite(DIR_PIN, HIGH);
      digitalWrite(EN_PIN, LOW);
      stepdirection = 2;
    }
  }
  else
  {
    digitalWrite(STEP_PIN, HIGH);
    digitalWrite(STEP_PIN, LOW);
  }
}

Die ist übersichtlicher und selbsterklärend.
Und hoffentlich genauso funktional.

Du kannst die Prüfung auf stepdirektion auch raus nehmen und weiter einkürzen. (Nur die Variable setzen und die weitere Prüfung entfallen lassen.
Sieht dann so aus:

  int trimmertoset = round(targetvaluetrim);
  const int DEADBAND = 2;  // Adjust this value based on your needs
  if (abs(trimmer - trimmertoset) == 0)
  {
    stepdirection = 0;  // Within the deadband, don't change direction
  }
  else
  {
    if (trimmer > trimmertoset + DEADBAND)
    {
      digitalWrite(DIR_PIN, LOW);
      digitalWrite(EN_PIN, LOW);
      stepdirection = 1;
    }
    if (trimmer < trimmertoset - DEADBAND)
    {
      digitalWrite(DIR_PIN, HIGH);
      digitalWrite(EN_PIN, LOW);
      stepdirection = 2;
    }
    digitalWrite(STEP_PIN, HIGH);
    digitalWrite(STEP_PIN, LOW);
  }
}

Und dann kommt es schon fast in die Richtung Zustandsautomat.

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