SunFounder 3in1 Kit
(continued from previous page)
{
int currentState = digitalRead(irPin); // 0:obstacle 1:no-obstacle
if (currentState == 0 && lastState == 1) {
count=(count+1)%10;
Blynk.virtualWrite(V8, count);
showNumber(count);
operateGate(true);
} else if ((currentState == 1 && lastState == 0)) {
operateGate(false);
}
lastState = currentState;
}
The function showNumber(int num) is used to make the 7-segment display show the value.
void showNumber(int num)
{
digitalWrite(STcp, LOW); //ground ST_CP and hold low for as long as you are␣
˓→transmitting
shiftOut(DS, SHcp, MSBFIRST, datArray[num]);
digitalWrite(STcp, HIGH); //pull the ST_CPST_CP to save the data
}
The function operateGate(bool openGate) slowly opens the door when the reference is True, and slowly closes
the door when the reference is False.
void operateGate(bool openGate) {
if (openGate == true)
{
// open gate
while (angle <= 90) {
angle++;
myservo.write(angle);
delay(5);
}
} else {
// close gate
while (angle >= 0){
angle--;
myservo.write(angle);
delay(5);
}
}
}
322 Chapter 6. IoT Projects