Hello everyone and thanks in advance taking the time to help a new b!
I have seen a couple of "if" statements but none of them seem to do the job I need, or maybe am I thinking all this trough wrong!
Here is the thing, I have a servo with the wiper voltage as feedback to lets say A7
a 1 pole 4 positions rotary commutator pined to A5 to A2 with there respective 20k pulldown resistors,
the switching pole to 5 V. And 4 OUTPUTs lets say 1 to 4!
I have also set 4 pre defined angular positions to where I want the servo to go depending on which input is
HIGH! my sketch will follow.
But I want to include some security calibration!
By exemple when calibrating in the "void setup ()", I send the servo to all 4 specific angular positions, read the wiper value and store it in 4 different variables for the loop
Now we are in the loop
I rotate my commutator to position 2 by exemple and in order to turn HIGH the respective OUTPUT some conditions must be achieved!
First I check what position exactly the commutator is in by adding some security check prior to write the value to the servo. Then if all clear the servo goes to the position, I then read the wiper value and compare it to the previous stored value for this specific position (stored in the void setup () variable) then if fine (within a specific range), only now we turn the respective OUTPUT to HIGH!!! if not we light an led by exemple!
So here comes multiple questions if by exemple
// Library, inputs outputs and other stuffs...
// wiper input pin
const int myServoWiper = A7;
// led
const int led = 13;
// pre located positions in degrees
const int angl1 = 20;
const int angl2 = 25;
const int angl3 = 30;
const int angl4 = 35;
const int safeAngle = 90;
// commutation input pins
const int pos1 = A5;
const int pos2 = A4;
const int pos3 = A3;
const int pos4 = A2;
// output pins
const int out1 = 1;
const int out2 = 2;
const int out3 = 3;
const int out4 = 4;
void setup () {
myservo.attach(9);
// pin modes etc..
// calibration
myservo.write(angl1);
delay(5);
int posVal1 = analogRead(myServoWiper);
delay(10);
// angl2
// angl3
myservo.write(angl4);
delay(5);
int posVal4 = analogRead(myServoWiper);
delay(10);
How to properly store the values without having posVal1 equals to posVal2 etc...?
And most importantly how do I use this "If" statement
void loop() {
digitalRead(pos1)
if ( (pos1 == HIGH) && (pos2 == LOW) && (pos3 == LOW) && (pos4 == LOW) ){
myservo.write(angl1);
} else if ( (pos1 == LOW) && (pos2 == LOW) && (pos3 == LOW) && (pos4 == LOW) ){ // if 5v wire brakes
myservo.write(safeAngle);
}
and now I want to include the comparison of my variables to switch the OUTPUT HIGH
if the difference is within a percentage range
how do I include this in the If statement Please