Part 5 - Arduino Code
Use the following code with the schematic in the last post to give you a better idea of what does what.
There was much research, trial and error to get the code working, but here it is:
const int buttonPinF = 2; // Flywheel Rev microswitch pin number
const int buttonPinS = 5; // Solenoid microswitch pin number
int buttonStateF = 0; // Variable for reading the Flywheel Rev microswitch status
int buttonStateS = 0; // Variable for reading the Solenoid microswitch status
int solenoidPin = 4; // Solenoid MOSFET Gate pin number
#include <Servo.h>
Servo throttle;
int pos = 0;
int pin = 3; // ESC signal pin
void setup() {
pinMode(buttonPinF, INPUT); // Initialize the Flywheel microswitch pin as an input
pinMode(buttonPinS, INPUT); // Initialize the Solenoid microswitch pin as an input
throttle.attach(pin);
pinMode(solenoidPin, OUTPUT); // Sets Solenoid MOSFET Gate pin as an output
// ESC Arming Sequence
for (pos = 90; pos <= 91; pos += 1) {
throttle.write(pos);
delay(3700); // Wait for ESC to arm / Exit safety mode
// Increase this 3700 value depending on how long it takes for your ESC to arm
}
}
void loop() {
buttonStateF = digitalRead(buttonPinF); // Read state of Flywheel microswitch value
if (buttonStateF == HIGH) { // Check microswitch pressed, if so Flywheel buttonState is HIGH
throttle.write(92); // <(92) = Motor off / (92) = Idle speed
} else {
throttle.write(97); // Motor on (92) = Idle speed / ~(115) = Max speed
}
buttonStateS = digitalRead(buttonPinS); // Read state of Solenoid microswitch value
if (buttonStateF == LOW && buttonStateS == LOW) {
digitalWrite(solenoidPin, HIGH); // Switch Solenoid ON
delay(90); // ON duration
digitalWrite(solenoidPin, LOW); // Switch Solenoid OFF
delay(100); // OFF duration
} else {
digitalWrite(solenoidPin, LOW); // Switch Solenoid OFF
}
}



Find content












