multiple if and elses. i cant add some conditions.

this is the code. this is for my ebike project. i finally managed to made it work problem is, i cant add input voltage conditions.

here is the code

int val = 0;
int panel = 11; //panel voltage input pin 2 3 4 volts preset.
int thr = 6; //voltage output
int THR1 = 150;  // output preset voltage. approx 1.7 volts 8 bit. RC filter for PWM to DC.
int THR2 = 190; // 2.7 volts
int THR3 = 230; // 4.3 volts
int PNLLO = 400; // constant signal from control panel 2 volt. 10 bit
int PNLMD = 630; // 3 volt. 10 bit
int PNLHI = 800; // 4 volt. 10 bit
int ledPin = 13;  //integrated led
int hallsensor = 2; // hall sensor pin.
int firsttime = 1;
unsigned long startTime;
unsigned long hallTime;
void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(hallsensor, INPUT);
  pinMode(hallsensor, INPUT_PULLUP);
  digitalWrite(hallsensor, HIGH);
  Serial.begin(9600);
}
void loop()
{

  if ((digitalRead(hallsensor) == LOW) ) {  //if panel gives 2 volts.
    if ((firsttime == 1) && (panel > PNLLO)) {
      startTime = millis();
      firsttime = 0;
      analogWrite(thr, THR1); // arduino has 1.7 volts output
      val = digitalRead(panel);
      Serial.println(val);
      delay (400);
    } else {
      if ((firsttime == 1) && (panel < PNLLO)) { //adjustment required
        startTime = millis();
        firsttime = 0;
        analogWrite(thr, THR1); //
        val = digitalRead(panel);
        delay (400);

      } else {
        if ((firsttime == 1) && (panel > PNLLO)) { //adjustment required
          startTime = millis();
          firsttime = 0;
          analogWrite(thr, THR3); //
          val = digitalRead(panel);
          delay (400);

        } else


          hallTime = millis() - startTime;
        if (hallTime >= 1) {
          Serial.print("Time: ");
          Serial.print(hallTime);
          Serial.print(" milliseconds ");
          Serial.print(int(hallTime / 1000));
          Serial.println(" seconds");
        }
        if (hallTime > 500) {  // if hall sensor matches to the magnet for more dan 0.5 sec
          digitalWrite(ledPin, HIGH);
          analogWrite(thr, 0); // cut power
        }
      }
    }
  }  else if (firsttime == 0) { // if no pulse on hall sensor
    firsttime = 1;
    Serial.println("Time: 0 milleseconds; 0 seconds");
    digitalWrite(ledPin, LOW);
    delay (500);
    analogWrite(thr, 0); // more than delay, cut power.
    Serial.println("idle");
  }
}

there is a section which millis takes place and it outputs pwm on digital pin. problem is there are two or more perset panel voltages. but conditions do not care them. it is always THR1. if i make < sign, > nothing happens. it seems analogwrite has priority. i want "thr" gives THR2 or THR3 when i set panel to preset voltages to PNLMD and PNLHI.

i am lost for two days and can't find a way to solve.

last else if and else conditions are for hall sensor and for its conditions. one of them is for when sensor is low for some time other is when it is high all the times. both are to stop senting voltage out.

this code works very good, it gives power to motor when pedals are rotating and it stops when rider stops pedalling. but it is single speed only now. PNLLO only. so it is thr1 on output pin.

could you please tell me the problematic part?

int panel = 11;

It will always be less than PNLLO = 400.

 if ((firsttime == 1) && (panel > PNLLO)) {
int panel = 11; //panel voltage input pin 2 3 4 volts preset.
....
int PNLLO = 400;
{
        if ((firsttime == 1) && (panel > PNLLO)) {

In my admittedly limited experience, eleven is never greater than four hundred

ops... there must be a pin number for analog input , not the value for analog input,right? i will let you know later.

int val = 0;
int panel = A5; //panel voltage input pin 2 3 4 volts preset.
int thr = 6; //voltage output
int THR1 = 90;  // output preset voltage. approx 1.7 volts 8 bit. RC filter for PWM to DC.
int THR2 = 150; // 2.7 volts
int THR3 = 220; // 4.3 volts
int PNLLO = 420; // constant signal from control panel 2 volt. 10 bit
int PNLMD = 640; // 3 volt. 10 bit
int PNLHI = 810; // 4 volt. 10 bit
int ledPin = 13;  //integrated led
int hallsensor = 2; // hall sensor pin.
int firsttime = 1;
unsigned long startTime;
unsigned long hallTime;
void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(hallsensor, INPUT);
  pinMode(hallsensor, INPUT_PULLUP);
  digitalWrite(hallsensor, HIGH);
  Serial.begin(9600);
}
void loop()
{
val = analogRead(panel); 
                
                 
 if ((digitalRead(hallsensor) == LOW) ) {  //if panel gives 2 volts.
  if ((firsttime == 1) && (val <= PNLLO)) {
  startTime = millis();
  firsttime = 0;
  analogWrite(thr, THR1); // arduino has 1.7 volts output
  val = digitalRead(panel);
  Serial.println(val);
  delay (400);
  } else {
  if ((firsttime == 1) && (val > PNLLO) && (val < PNLHI)) 
  startTime = millis();
  firsttime = 0;
  analogWrite(thr, THR2); //
  val = digitalRead(panel);
  delay (400);

  } else {
  if ((firsttime == 1) && (val >= PNLHI)) 
  startTime = millis();
  firsttime = 0;
  analogWrite(thr, THR3); //
  val = digitalRead(panel);
  delay (400);

  } else


  hallTime = millis() - startTime;
  if (hallTime >= 1) {
  Serial.print("Time: ");
  Serial.print(hallTime);
  Serial.print(" milliseconds ");
  Serial.print(int(hallTime / 1000));
  Serial.println(" seconds");
  }
  if (hallTime > 500) {  // if hall sensor matches to the magnet for more dan 0.5 sec
  digitalWrite(ledPin, HIGH);
  analogWrite(thr, 0); // cut power
  }
  }
  }
  }  else if (firsttime == 0) { // if no pulse on hall sensor
  firsttime = 1;
  Serial.println("Time: 0 milleseconds; 0 seconds");
  digitalWrite(ledPin, LOW);
  delay (500);
  analogWrite(thr, 0); // more than delay, cut power.
  Serial.println("idle");
  }
}

this seems ok? i will try this on bike tomorrow.

another question. think about a hall sensor. original controller somehow detects reverse pedal movement and does not activate motor. how to do this? any ideas?

original controller somehow detects reverse pedal movement and does not activate motor. how to do this?

If you go from point A to point B to point C, can you see how that is different from going from point C to point B to point A?

With a single hall effect sensor and a single trigger, knowing the direction is not possible. With two sensor and one trigger, knowing the direction is possible. With one sensor, and two triggers that are NOT equally spaced, knowing the direction is possible.

We'd need to know more about your setup.

how about transition part of PWM signal? if i can find a way to find exact moment of signal coming from hall sensor, i think i can use this info. i need to learn about sampling i guess.