Hello I have a question
ik want to make my gear selector elektronic with some switches and a linear actuator
i have come so far that my actuator has 4 states ( park reverse neutral drive) when I apply the brake
but I want a delay on the neutral preset
so if I press the neutral that de presset is going to act after 2 sec en not when I press short
here is my code so far
</> (<-- you're supposed to Click that button and then paste your code in - Moderator)
// Add button library
#include <Button.h>
// Use the jumpers on the board to select which pins will be used
int EnablePin = 8; // use pin 8 to enable board
int PWMPinA1 = 3; // motor pin A for actuator
int PWMPinB1 = 11; // motor pin B for actuator
const int buttonPin = 2; //remsignaal
int buttonState = 0;
// Declare our buttons and specify the correponding pins
Button ManualUp(9); // use pin 9 to move elevator up manually
Button ManualDown(10); // use pin 10 to move elevator down manually
Button PresetPark(6); // use pin 6 to move elevator to park level
Button PresetDrive(7); // use pin 7 to move elevator to drive level
Button PresetReverse(4); // use pin 4 to move elevator to drive level
Button PresetNeutral(5); // use pin 5 to move elevator to drive level
Button Contact(12); // use pin 12 to contact
// Specify our input pin for actuator position
int SensorPin = A3;
// Set other globally used variables
int delaytime = 5*1000; // 5 seconds times 1,000 to convert to milliseconds
int ParkLevel = 1800; // will store/read these values from EEPROM for production
int DriveLevel = -80; // will store/read these values from EEPROM for production
int ReverseLevel = 700; // will store/read these values from EEPROM for production
int NeutralLevel = 400; // will store/read these values from EEPROM for production
int CurrentPos = 0; // use to read the current position of actuator
int TargetPos = 0; // use to set the target position of actuator
int Accuracy = 10; // use to make sure we stop the actuator within the ballpark of target
bool AutoPilot = false; // if true, we are on autopilot
void setup() {
pinMode(buttonPin, INPUT);
pinMode(2, INPUT_PULLUP);
// put your setup code here, to run once:
// initialize our buttons
ManualUp.begin();
ManualDown.begin();
PresetPark.begin();
PresetDrive.begin();
PresetReverse.begin();
PresetNeutral.begin();
Contact.begin();
// initialize serial port
Serial.begin(9600);
// initialize MegaMoto board
pinMode(EnablePin, OUTPUT); //Enable the board
pinMode(PWMPinA1, OUTPUT);
pinMode(PWMPinB1, OUTPUT); //Set motor outputs
// set our sensor pin to read
pinMode(SensorPin, INPUT);
// make sure we don't move the actuator upon start
analogWrite(PWMPinA1, 0);
analogWrite(PWMPinB1, 0);
// enable the megamoto shield
Serial.println("Enabling MegaMoto shield");
// delay(delaytime);
digitalWrite(EnablePin, HIGH);
Serial.println("MegaMoto shield is now enabled");
}
void loop() {
buttonState = digitalRead(buttonPin);
CurrentPos = ReadPos();
if ((AutoPilot) && (ReachedTarget(CurrentPos))) {
ElevatorStop();
AutoPilot = false;
}
if ((PresetPark.pressed () && (buttonState == LOW ))) {
AutoPilot = true;
TargetPos=ParkLevel;
GoToTarget();
Serial.print("PARK ");
}
if ((PresetDrive.pressed() && (buttonState == LOW ))) {
AutoPilot = true;
TargetPos=DriveLevel;
GoToTarget();
Serial.print("DRIVE ");
}
if ((PresetReverse.pressed() && (buttonState == LOW ))) {
AutoPilot = true;
TargetPos=ReverseLevel;
GoToTarget();
Serial.print("REVERSE ");
}
if ((PresetNeutral.pressed() && (buttonState == LOW ))) {
AutoPilot = true;
TargetPos=NeutralLevel;
GoToTarget();
Serial.print("NEUTRAL ");
}
if (ManualUp.pressed()) {
AutoPilot = false;
ElevatorUp();
}
if (ManualDown.pressed()) {
AutoPilot = false;
ElevatorDown();
}
if (Contact.pressed()) {
AutoPilot = true;
TargetPos=ParkLevel;
GoToTarget();
Serial.print("Contact Uit Going in Park position ");
}
if (Contact.released()) {
Serial.print("Contact Aan ");
}
if (ManualUp.released() || ManualDown.released()) {
ElevatorStop();
}
}
void ElevatorUp() {
// Activate pin A and deactivate pin B
analogWrite(PWMPinA1, 255);
analogWrite(PWMPinB1, 0); // speed 0-255
}
void ElevatorDown() {
// Activate pin B and deactivate pin A
analogWrite(PWMPinA1, 0);
analogWrite(PWMPinB1, 255); // speed 0-255
}
void ElevatorStop() {
// Deactivate both pins
analogWrite(PWMPinA1, 0);
analogWrite(PWMPinB1, 0); // speed 0-255
}
int ReadPos() {
return analogRead(SensorPin);
}
void GoToTarget() {
if (ReachedTarget(CurrentPos)) {
Serial.println("selector already at target.");
}
else if (TargetPos < CurrentPos) {
ElevatorDown();
}
else if (TargetPos > CurrentPos) {
ElevatorUp();
}
}
bool ReachedTarget(int CheckPos) {
int LimitLow = CheckPos - Accuracy;
int LimitHigh = CheckPos + Accuracy;
if ((TargetPos < LimitHigh) && (TargetPos > LimitLow)) {
return true;
}
else {
return false;
}
}
</>