So I'm working on a project that is using actuators to raise and lower parts of a bed. i want to use the pot on the actuator as a limit switch so i don't over extend or break the bed when everything is at home position. i do have limits switches in the code those are for servo motors that i have yet to add to the code.
any help would be greatly appreciated
// constants won't change. They're used here to set pin numbers:
const int HeadUpPin = 53;
const int HeadDownPin = 52;
const int FootUpPin = 51;
const int FootDownPin = 50;
const int TiltUpPin = 49;
const int TiltDownPin = 48;
const int BedUpPin = 47;
const int BedDownPin = 46;
const int RotateStandPin = 45;
const int RotateBedPin = 44;
const int Macro1Pin = 43;
const int Macro2Pin = 42;
const int RotateBedStopPin=41;
const int RotateStandStopPin=40;
const int BedHeadUpPin=39;
const int BedHeadDownPin=38;
const int BedFootUpPin=37;
const int BedFootDownPin=36;
const int HeadUpRelayPin = 35; // the number of the Realy1 pin
const int HeadDownRelayPin = 34; // the number of the Relay2 pin
const int FootUpRelayPin = 33; // the number of the Relay3 pin
const int FootDownRelayPin = 32; // the number of the Relay 4 pin
const int TiltUpRelayPin = 31; // the number of the Realy 5 pin
const int TiltDownRelayPin = 30; // the number of the Relay6 pin
// variables will change:
int HeadUpButtonState = 0; // variable for reading the pushbutton status
int HeadDownButtonState = 0; // variable for reading the pushbutton status
int FootUpButtonState = 0; // variable for reading the pushbutton status
int FootDownButtonState = 0; // variable for reading the pushbutton status
int TiltUpButtonState = 0; // variable for reading the pushbutton status
int TiltDownButtonState = 0; // variable for reading the pushbutton status
const int HeadSensorPin = A0; // select the input pin for the potentiometer
const int FootSensorPin = A1; // select the input pin for the potentiometer
const int TiltSensorPin = A2; // select the input pin for the potentiometer
int HeadValue = 0; // variable to store the value coming from the sensor
int FootValue = 0; // variable to store the value coming from the sensor
int TiltValue = 0; // variable to store the value coming from the sensor
void setup() {
//start serial connection
Serial.begin(9600);
// initialize the pushbutton pin as an input:
pinMode(HeadUpPin, INPUT);
pinMode(HeadDownPin, INPUT);
pinMode(FootUpPin, INPUT);
pinMode(FootDownPin, INPUT);
pinMode(TiltUpPin, INPUT);
pinMode(TiltDownPin, INPUT);
// initialize the relay pin as an output:
pinMode(HeadUpRelayPin, OUTPUT);
pinMode(HeadDownRelayPin, OUTPUT);
pinMode(FootUpRelayPin, OUTPUT);
pinMode(FootDownRelayPin, OUTPUT);
pinMode(TiltUpRelayPin, OUTPUT);
pinMode(TiltDownRelayPin, OUTPUT);
}
void loop() {
// read the value from the sensor:
HeadValue = analogRead(HeadSensorPin);
FootValue = analogRead(HeadSensorPin);
TiltValue = analogRead(HeadSensorPin);
//print out the value of the pushbutton
Serial.println(HeadValue);
// read the state of the pushbutton values:
HeadUpButtonState= digitalRead(HeadUpPin);
HeadDownButtonState = digitalRead(HeadDownPin);
FootUpButtonState= digitalRead(FootUpPin);
FootDownButtonState = digitalRead(FootDownPin);
TiltUpButtonState = digitalRead(TiltUpPin);
TiltDownButtonState= digitalRead(TiltDownPin);
// check if the pushbutton1 is pressed.
// if it is, the buttonState is HIGH:
// we also ensure tha the other button is not pushed to avoid conflict
if (HeadUpButtonState == HIGH && HeadDownButtonState == LOW FootUpButtonState == LOW && FootDownButtonState == LOW TiltUpButtonState == LOW && TiltDownButtonState == LOW) {
// turn relay1 on:
digitalWrite(HeadUpRelayPin, HIGH);
}
// When we let go of the button, turn off the relay
else if (digitalRead(HeadUpRelayPin) == HIGH) {
// turn relay1 off:
digitalWrite(HeadUpRelayPin, LOW);
}
// repeat the same procedure for the second pushbutton
if (HeadUpButtonState == LOW && HeadDownButtonState == HIGH FootUpButtonState == LOW && FootDownButtonState == LOW TiltUpButtonState == LOW && TiltDownButtonState == LOW) {
// turn relay2 on:
digitalWrite(HeadDownRelayPin, HIGH);
}
// When we let go of the button, turn off the relay
else if (digitalRead(HeadDownRelayPin) == HIGH) {
// turn relay2 off:
digitalWrite(HeadDownRelayPin, LOW);
}
// we also ensure tha the other button is not pushed to avoid conflict
if (HeadUpButtonState == LOW && HeadDownButtonState == LOW FootUpButtonState == HIGH && FootDownButtonState == LOW TiltUpButtonState == LOW && TiltDownButtonState == LOW) {
// turn relay1 on:
digitalWrite(FootUpRelayPin, HIGH);
}
// When we let go of the button, turn off the relay
else if (digitalRead(FootUpRelayPin) == HIGH) {
// turn relay1 off:
digitalWrite(FootUpRelayPin, LOW);
}
// repeat the same procedure for the second pushbutton
if (HeadUpButtonState == LOW && HeadDownButtonState == LOW FootUpButtonState == LOW && FootDownButtonState == HIGH TiltUpButtonState == LOW && TiltDownButtonState == LOW) {
// turn relay2 on:
digitalWrite(FootDownRelayPin, HIGH);
}
// When we let go of the button, turn off the relay
else if (digitalRead(FootDownRelayPin) == HIGH) {
// turn relay2 off:
digitalWrite(FootDownRelayPin, LOW);
}
// we also ensure tha the other button is not pushed to avoid conflict
if (HeadUpButtonState == LOW && HeadDownButtonState == LOW FootUpButtonState == LOW && FootDownButtonState == LOW TiltUpButtonState == High && TiltDownButtonState == LOW) {
// turn relay1 on:
digitalWrite(TiltUpRelayPin, HIGH);
}
// When we let go of the button, turn off the relay
else if (digitalRead(TiltUpRelayPin) == HIGH) {
// turn relay1 off:
digitalWrite(TiltUpRelayPin, LOW);
}
// repeat the same procedure for the second pushbutton
if (HeadUpButtonState == LOW && HeadDownButtonState == LOW FootUpButtonState == LOW && FootDownButtonState == LOW TiltUpButtonState == LOW && TiltDownButtonState == HIGH) {
// turn relay2 on:
digitalWrite(TiltDownRelayPin, HIGH);
}
// When we let go of the button, turn off the relay
else if (digitalRead(TiltDownRelayPin) == HIGH) {
// turn relay2 off:
digitalWrite(TiltDownRelayPin, LOW);
}
}
