полный код
// define motorPotiton class to tell you wher the moter
enum class MotorPosition
{
one,
two,
unknown_positon
};
// define connections location passed on your hardware
const int switch_one_pin = 4;
const int switch_two_pin = 5;
const int motor_forward_Pin = 7;
const int motor_backward_pin = 6;
void setup()
{
// define direction of connection
pinMode(motor_backward_pin, OUTPUT);
pinMode(motor_forward_Pin, OUTPUT);
pinMode(switch_one_pin, INPUT);
pinMode(switch_two_pin, INPUT);
// set motor in second location at start
changeMotorPosition(MotorPosition::two);
}
void loop()
{
changeMotorPosition(MotorPosition::one);
delay(10800000); //Delay 3 Hours
changeMotorPosition(MotorPosition::two);
delay(10800000); //Delay 3 hours
}
//******************************************************************
// **** helper functions ****
// ******************************************************************
void changeMotorPosition(MotorPosition newPosition)
{
// define a static variable to save cuerrnet location
// if you don't know what is static means .. you could think of it as a global variable but just inside this function ...means only ceareated one time
static MotorPosition motorCurrentPosition = MotorPosition::unknown_positon;
// if motor is alerady in the correct place then do nothing
if(getActualPosition() == newPosition ){
// just make sure that you update the current poition
motorCurrentPosition = newPosition;
return;
}
if(getActualPosition() == MotorPosition::unknown_positon){
// motor is not in one of it's the position
// this will happen if motor dose note have selfLock system
// so we will check on last saved position
if(motorCurrentPosition == newPosition){
// if you like you could rest the motor to correct place
// but i will let it not so important
// TO DO : make motor go back to correct place
return;
}
}
// now let see were you like to go
switch (newPosition)
{
case MotorPosition::one:
// go forward untill you reach the second position
while (digitalRead(switch_two_pin) == LOW)
{
digitalWrite(motor_forward_Pin,HIGH);
}
// then stop motor
digitalWrite(motor_forward_Pin,LOW);
//change the currnt location Programmaticlly
motorCurrentPosition = MotorPosition::one;
break;
case MotorPosition::two:
// go backword untill you reach the first position
while (digitalRead(switch_one_pin) == LOW)
{
digitalWrite(motor_backward_pin,HIGH);
}
// then stop motor
digitalWrite(motor_forward_Pin,LOW);
//change the currnt location programmaticlly
motorCurrentPosition = MotorPosition::two;
break;
default:
// that means newPosition == unknown position
// do nothing this will be error in calling function
break;
}
}
// the follloiwing function will return the acuall location of motor
MotorPosition getActualPosition(){
if(digitalRead(switch_one_pin) == HIGH) return MotorPosition::one;
if(digitalRead(switch_two_pin)==HIGH) return MotorPosition::two;
return MotorPosition::unknown_positon;
}
некоторые пояснения
сначала вам нужно знать, что код arduino - это всего лишь код c++
, поэтому, если вы уже знаете c++
, то вам было легко чтобы понять
enum class MotorPosition
{
one,
two,
unknown_positon
};
, как вы видите, я создал enum class
с именем MotorPosition
, чтобы использовать его в качестве переменной позиции, это даст вам возможность сделать переменную типа MotorPosition
и только принять одно из трех значений (одно, два, unkonwn)
// look at int type can take any number value
int a = 5;
// look at MotorPositon type can take one of it's value like this
MotorPosition b = MotorPosition::one;
подробнее о классе enum в cpp
затем я использую changeMotorPosition
функцию, которая поможет вам изменить местоположение двигателя, просто укажите его местоположение, и оно будет go для него вы можете назвать это так
changeMotorPosition(MotorPosition::one); // go for first location
changeMotorPosition(MotorPosition::two); // go for second location
теперь прочитайте весь код и получайте удовольствие:)