//changing LED
#include <Stepper.h>
int speed;
int steps;
int delayy;
#define Light 13
#define Medium 12
#define Heavy 11
#define BUTTON_PIN 10
#define LED_NUMBER 3
byte LEDPinArray[LED_NUMBER] = { Light,
Medium,
Heavy };
unsigned long debounceDuration = 50; // millis
unsigned long lastTimeButtonStateChanged = 0;
byte lastButtonState = HIGH;
int LEDIndex = 0;
//Button switch
#include <ezButton.h>
#define SWITCH_OFF 0
#define SWITCH_ON 1
ezButton button(9); // create ezButton object that attach to pin 7;
int switch_state = SWITCH_OFF; // initial state
const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 4, 5, 6, 7);
//-------------------------------- not important
void setup(){ //-------------------setup
Serial.begin(9600);
button.setDebounceTime(10); // set debounce time to 50 milliseconds
initAllLEDs();
pinMode(BUTTON_PIN, INPUT_PULLUP);
digitalWrite(LEDPinArray[LEDIndex], HIGH);
}
void checkstuff(){ //-------------------checkstuff
if (switch_state = 1){
Serial.print("switchs state -> ");
Serial.println(switch_state);
}
}
void go(){ //-------------------go
if (switch_state == 0){
Serial.print("ON");
stepper();
}
else{
if (switch_state == 1){
Serial.print("OFF");
}
}
}
//-------------------------------- end of not important
//-------------------------------- dont touch!
void initAllLEDs()
{
for (int i = 0; i < LED_NUMBER; i++) {
pinMode(LEDPinArray[i], OUTPUT);
}
}
void powerOnSelectedLEDOnly(int index)
{
for (int i = 0; i < LED_NUMBER; i++) {
if (i == index) {
digitalWrite(LEDPinArray[i], HIGH);
}
else {
digitalWrite(LEDPinArray[i], LOW);
}
}
}
void toggle (){
unsigned long timeNow = millis();
if (timeNow - lastTimeButtonStateChanged > debounceDuration) {
byte buttonState = digitalRead(BUTTON_PIN);
if (buttonState != lastButtonState) {
lastTimeButtonStateChanged = timeNow;
lastButtonState = buttonState;
if (buttonState == HIGH) { // button has been released
LEDIndex++;
if (LEDIndex >= LED_NUMBER) {
LEDIndex = 0;
}
powerOnSelectedLEDOnly(LEDIndex);
}
}
}
}
//-------------------------------- you can touch!
void check(){ //------------------- checks if the button is pressed and changes it from 0-1/1-0
button.loop(); // MUST call the loop() function first
if (button.isPressed()) {
// change state of switch
if (switch_state == SWITCH_OFF)
switch_state = SWITCH_ON;
else
switch_state = SWITCH_OFF;
Serial.print("switch's state -> ");
Serial.println(switch_state);
}
}
void stepper (){ //starts the stepper
stepcheck(Light, 60, 200, 500); //Button for light
stepcheck(Medium, 120, 200, 350); //Button for medium
stepcheck(Heavy, 180, 200, 250); //Button for heavy
while(switch_state == 1){
myStepper.setSpeed(speed);
myStepper.step(steps);
delay(delayy);
//counterclockwise start
myStepper.step(-steps);
delay(delayy);
}
}
void stepcheck(int pin, int speed1, int steps1, int delay1){ //checks which light is on
if(pin == HIGH){
Serial.println(pin);
speed = speed1;
steps = steps1;
delayy = delay1;
}
}
void loop(){
// go();
check();
//stepper();
toggle();
stepcheck(Light, 60, 200, 500); //Button for light
stepcheck(Medium, 120, 200, 350); //Button for medium
stepcheck(Heavy, 180, 200, 250); //Button for heavy
}