#include <Stepper.h>
// Cisla pinu pro tlacitka
#define BTN_UP 2
#define BTN_DOWN 3
#define BTN_SET 4
int potpin = A0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
int previous = 0;
int count_value =0;
int prestate =0;
const int stepsPerRevolution = 100; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
pinMode(BTN_UP, INPUT_PULLUP);
pinMode(BTN_DOWN, INPUT_PULLUP);
pinMode(BTN_SET, INPUT_PULLUP);
}
void loop() {
//Serial.println(analogRead(potpin));
//step_one_revolution();
position_with_potentiometer();
//BTN_reader();
//Motor_BTN_control();
//button_counter();
//button_set_value_step();
}
void step_one_revolution() {
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(500);
// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
delay(500);
}
void position_with_potentiometer() {
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 90); // scale it to use it with the servo (value between 0 and 180)
myStepper.step(val - previous); // sets the servo position according to the scaled value
previous = val;
}
// Cteni hodnot BTN
void BTN_reader(){
Serial.println(digitalRead(BTN_UP));
//Serial.println(digitalRead(BTN_DOWN));
}
// 2x tlacitko na motor dopredu/dozadu
void Motor_BTN_control(){
if (digitalRead(BTN_UP) == LOW) {
myStepper.step(stepsPerRevolution);
Serial.println("Motor+");
}
else if (digitalRead(BTN_DOWN) == LOW) {
myStepper.step(-stepsPerRevolution);
Serial.println("Motor-");
}
else {
Serial.println("Motor STOP");
}
}
// Pocitadlo na tlacitka¨
//void button_counter2() {
// Serial.print(stepsPerRevolution);
// Serial.print("\n");
// if (digitalRead(BTN_UP) == LOW && prestate == 0) {
// stepsPerRevolution = stepsPerRevolution + 10;
// prestate = 1;
// }
// else if (digitalRead(BTN_DOWN) == LOW && prestate == 0) {
// stepsPerRevolution = stepsPerRevolution - 10;
// prestate = 1;
// }
// else if(digitalRead(BTN_SET) == LOW && prestate == 0) {
// myStepper.step(stepsPerRevolution);
// }
// else if(digitalRead(BTN_UP) == HIGH && digitalRead(BTN_DOWN) == HIGH && digitalRead(BTN_SET) == HIGH) {
// prestate = 0;
// }
//}
void button_counter() {
Serial.print(count_value);
Serial.print("\n");
if (digitalRead(BTN_UP) == LOW && prestate == 0) {
count_value = count_value + 10;
prestate = 1;
}
else if (digitalRead(BTN_DOWN) == LOW && prestate == 0) {
count_value = count_value - 10;
prestate = 1;
}
else if(digitalRead(BTN_SET) == LOW && prestate == 0) {
count_value = 0;
prestate = 1;
}
else if(digitalRead(BTN_UP) == HIGH && digitalRead(BTN_DOWN) == HIGH && digitalRead(BTN_SET) == HIGH) {
prestate = 0;
}
}
void button_set_value_step() {
Serial.print(count_value);
Serial.print("\n");
if (digitalRead(BTN_UP) == LOW && prestate == 0) {
count_value = count_value + 100;
prestate = 1;
}
else if (digitalRead(BTN_DOWN) == LOW && prestate == 0) {
count_value = count_value - 100;
prestate = 1;
}
else if(digitalRead(BTN_SET) == LOW && prestate == 0) {
myStepper.step(count_value);
}
else if(digitalRead(BTN_UP) == HIGH && digitalRead(BTN_DOWN) == HIGH && digitalRead(BTN_SET) == HIGH) {
prestate = 0;
}
}