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?