Peacepirate
New Member
One question remains:
What do you connect to ELECTROMAGNET_PIN 4?
What do you connect to ELECTROMAGNET_PIN 4?
Riding a tuned or deristricted EMTB is not a trivial offence and can have serious legal consequences. Also, many manufacturers can detect the use of a tuning device or deristricting method and may decline a repair under warranty if it was modified from the intended original specification. Deristricting EMTB's can also add increased loads for motors and batteries. Riding above the local law limit may reclassify the bike as a low-powered bike, requiring insurance, registration and a number plate.
Be aware of your local country laws. Many laws prohibit use of modified EMTB's. It is your responsibility to check local laws. Ignoring it, has potential implications to trail access, and risk of prosecution in the event of an accident.
We advise members great caution. EMTB Forums accepts no liability for any content or advice given here.
You can leave that line of code in place and simply make no connection to Pin4.One question remains:
What do you connect to ELECTROMAGNET_PIN 4?
Hey there,You can leave that line of code in place and simply make no connection to Pin4.
Hey there,
when i tried the first time to test your code it didnt worked out but i wanted to check everything before proclaiming that and be sure that this wasnt caused by a wrong edited code. i dont need a variable multiplier but just a fixed value of 2.0 and i dont need a setup mode either so i deleted that part. To exclude these potential sources of errors i uploaded your code again with the changes you talked about:
#define REED_SENSOR_PIN 2
#define SETUP_PIN 6
#define ELECTROMAGNET_PIN 7
#define REDLED_LOWPRESSCOUNT_PIN 5
#define A1A 4
#define A1B 3
byte ButtonState;
byte LastButtonState;
byte Trigger = HIGH;
byte SetupState;
double LastPress = 0;
double CurrentPress = 0;
double EndMagnet = 0;
double MagnetDuration = 0;
double Delta;
double DeltaStatic;
double ActivateMagnetStart = 0;
double ActivateMagnetTemp = 0;
double MultiplierOut = 1;
double MagnetOnTime = 0;
double MultiplierCutOff = 1398; //1398 = 6kph/3.5mph
int Pressed = 0;
int PressCount = 0;
int MagnetSwitch = 0;
int MagnetOn = 0;
////////////////////
//change this MultiplierOut to alter max assistance speed. MultiplierOut 1.4 gives: 25kph * 1.4 = 35kph new max assitance
double MultiplierIn = 2.0;
double SetupDelay = 513;
double SetupWidth = 47;
void setup() {
pinMode(ELECTROMAGNET_PIN, OUTPUT);
pinMode(REED_SENSOR_PIN, INPUT_PULLUP);
pinMode(SETUP_PIN, INPUT_PULLUP);
pinMode(REDLED_LOWPRESSCOUNT_PIN, OUTPUT);
pinMode(A1A, OUTPUT);
pinMode(A1B, OUTPUT);
LastButtonState = digitalRead(REED_SENSOR_PIN);
SetupState = digitalRead(SETUP_PIN);
}
void loop() {
SetupState = digitalRead(SETUP_PIN);
//if we're in setup mode, run the consistent pulsing
if (SetupState == LOW) {
delay(SetupDelay);
//activate coil
digitalWrite(ELECTROMAGNET_PIN, HIGH);
digitalWrite(REDLED_LOWPRESSCOUNT_PIN, HIGH);
digitalWrite(A1A, HIGH);
digitalWrite(A1B, LOW);
delay(SetupWidth); //pulse width
//deactivate coil
digitalWrite(ELECTROMAGNET_PIN, LOW);
digitalWrite(REDLED_LOWPRESSCOUNT_PIN, LOW);
digitalWrite(A1A, LOW);
digitalWrite(A1B, LOW);
}
//outside of setup mode, function as normal
if (SetupState == HIGH) {
ButtonState = digitalRead(REED_SENSOR_PIN);
if ((ButtonState != LastButtonState) && (millis() - CurrentPress > 100)) {
//Button pressed
if (ButtonState == LOW) {
LastPress = CurrentPress;
CurrentPress = millis();
Delta = CurrentPress - LastPress;
Pressed = 2;
digitalWrite(REDLED_LOWPRESSCOUNT_PIN, HIGH);
if ((PressCount < 5) && ((CurrentPress - ActivateMagnetStart >= Delta) || PressCount > 0)) {
digitalWrite(ELECTROMAGNET_PIN, HIGH);
digitalWrite(A1A, HIGH);
digitalWrite(A1B, LOW);
Trigger = HIGH;
ActivateMagnetStart = CurrentPress;
MultiplierOut = MultiplierIn;
MagnetOnTime = millis();
MagnetOn = 1;
}
}
//Button released
if (ButtonState == HIGH) {
EndMagnet = millis();
MagnetDuration = EndMagnet - CurrentPress;
digitalWrite(REDLED_LOWPRESSCOUNT_PIN, LOW);
if (PressCount < 5) {
digitalWrite(ELECTROMAGNET_PIN, LOW);
digitalWrite(A1A, LOW);
digitalWrite(A1B, LOW);
MagnetOn = 0;
}
PressCount = PressCount + 1;
}
LastButtonState = ButtonState;
}
//if travelling slowly, fire coil 1:1
if (millis() - ActivateMagnetStart > MultiplierCutOff) {
PressCount = 0;
}
//If wheel stops on magnet after travelling slowly, turn off the coil after 2seconds
if ((MagnetOn == 1) && (millis() - MagnetOnTime > 2000)) {
digitalWrite(ELECTROMAGNET_PIN, LOW);
digitalWrite(REDLED_LOWPRESSCOUNT_PIN, LOW);
digitalWrite(A1A, LOW);
digitalWrite(A1B, LOW);
}
//Record one Delta to avoid continual changes to interval during multiplied interval below
if ((Trigger == HIGH) && (Delta > 0)) {
DeltaStatic = Delta;
Trigger = LOW;
}
//setup multiplier ramping
if (PressCount >= 5) {
if (PressCount < 10) {
MultiplierOut = (((MultiplierIn - 1) / 5) * (PressCount - 4)) + 1;
} else {
MultiplierOut = MultiplierIn;
}
if (millis() >= ActivateMagnetStart + DeltaStatic * MultiplierOut) {
if ((millis() < ActivateMagnetStart + DeltaStatic * MultiplierOut + MagnetDuration * MultiplierOut) && (Pressed > 0 )) {
//Magnet On
if (MagnetSwitch == 0) {
ActivateMagnetTemp = millis();
}
MagnetSwitch = 1;
digitalWrite(ELECTROMAGNET_PIN, HIGH);
digitalWrite(A1A, HIGH);
digitalWrite(A1B, LOW);
MagnetOnTime = millis();
MagnetOn = 1;
} else {
//Magnet Off
digitalWrite(ELECTROMAGNET_PIN, LOW);
digitalWrite(A1A, LOW);
digitalWrite(A1B, LOW);
ActivateMagnetStart = ActivateMagnetTemp;
Trigger = HIGH;
Pressed = Pressed - 1;
MagnetSwitch = 0;
MagnetOn = 0;
}
}
}
}
}
i tested the L9110 before without an arduino connected to it and could measure with my multimeter either +12v and GND on the motor out pins when i put on GND on A1A or -12v and GND if put gnd on A1B. So the h-bridge works as expected.
When i connect arduino and hbridge together AND DO ANYTHING ELSE after powering on this is the output of l9110:
Tuning device
youtube.com
Here you see voltages of the arduino pins 3/4 View attachment 141256
View attachment 141257
The World's largest electric mountain bike community.