#include <Arduino.h>
#include <TM1637Display.h>
#include <Servo.h>
Servo myservo; // create servo object to control a servo
#define servoPin 10 //~
#define pushButtonPin 4
#define CLK 2
#define DIO 3
#define SWITCH_PIN_INC 7
#define SWITCH_PIN_RESET 8
#define SWITCH_PIN_DEC 9
#define TEST_DELAY 2000
TM1637Display display(CLK, DIO);
int angle =0; // initial angle for servo (beteen 0 and 180)
int angleStep =180;
const int minAngle = 0;
const int maxAngle = 180;
int buttonPushed =0;
int direction = 1; // 1 means incrementing, -1 means decrementing
void setup()
{
Serial.begin(9600); // setup serial
myservo.attach(servoPin); // attaches the servo on pin 3 to the servo object
pinMode(pushButtonPin,INPUT);
Serial.println("Robojax Servo Button ");
myservo.write(angle);
pinMode(SWITCH_PIN_INC, INPUT_PULLUP);
pinMode(SWITCH_PIN_RESET, INPUT_PULLUP);
pinMode(SWITCH_PIN_DEC, INPUT_PULLUP);
display.setBrightness(6);
}
int numb = 0;
int incSwitchState = 0;
int resetSwitchState = 0;
int decSwitchState = 0;
bool incSwitchPressed = false;
bool decSwitchPressed = false;
void loop()
{
display.setBrightness(0x0f);
display.showNumberDec(numb, false);
incSwitchState = digitalRead(SWITCH_PIN_INC);
resetSwitchState = digitalRead(SWITCH_PIN_RESET);
decSwitchState = digitalRead(SWITCH_PIN_DEC);
if (incSwitchState == HIGH && !incSwitchPressed)
{
numb++;
incSwitchPressed = true;
}
else if (incSwitchState == LOW)
{
incSwitchPressed = false;
}
if (decSwitchState == HIGH && !decSwitchPressed)
{
numb--;
decSwitchPressed = true;
}
else if (decSwitchState == LOW)
{
decSwitchPressed = false;
}
if (resetSwitchState == LOW)
{
numb = 0;
delay(100);
}
if(digitalRead(pushButtonPin) == HIGH){
buttonPushed = 1;
delay(50); // debounce
}
if(buttonPushed){
// change the angle for next time through the loop:
angle = angle + (angleStep * direction);
// reverse the direction of the moving at the ends of the angle:
if (angle >= maxAngle) {
direction = -1;
angle = maxAngle;
}
if (angle <= minAngle) {
direction = 1;
angle = minAngle;
}
myservo.write(angle); // move the servo to desired angle
Serial.print("Moved to: ");
Serial.print(angle); // print the angle
Serial.println(" degree");
delay(100); // waits for the servo to get there
buttonPushed = 0; // reset the flag to wait for next button press
}
}