Hi all,
I have been struggling with this for days. I cannot get my stepper motor to start and stop. using my IR remote. as soon as I press the motoOn = False statement everything stops permanently.
#include <Ucglib.h>
#include <DIYables_IRcontroller.h>
#include <Stepper.h>
// Define pins for SPI TFT display
#define TFT_CS 10 // Chip select pin
#define TFT_RST 9 // Reset pin
#define TFT_DC 8 // Data/Command pin
// Create an instance of the ST7735 display using Ucglib
Ucglib_ST7735_18x128x160_HWSPI ucg(TFT_DC, TFT_CS, TFT_RST);
// Define pins for Stepper Motor
#define IN1 3
#define IN2 4
#define IN3 5
#define IN4 6
// Define the number of steps per revolution for your stepper motor
const int stepsPerRevolution = 2048; // For 28BYJ-48 stepper motor
// Create an instance of the Stepper class
Stepper myStepper(stepsPerRevolution, IN1, IN3, IN2, IN4);
// Define pin for IR receiver
#define IR_RECEIVER_PIN 2 // The Arduino pin connected to IR controller
// Create an instance of the IR receiver
DIYables_IRcontroller_21 irController(IR_RECEIVER_PIN, 200); // debounce time is 200ms
// Speed variables
int speedLevels[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; // Speed levels for buttons 1 to 9
int currentSpeedIndex = 1; // Start at a medium speed
bool motorOn = true; // Motor starts on
void setup() {
Serial.begin(9600);
// Initialize the TFT display
ucg.begin(UCG_FONT_MODE_TRANSPARENT);
ucg.clearScreen();
// Display the initial message
ucg.setFont(ucg_font_ncenR12_tr);
ucg.setColor(255, 255, 255);
ucg.setPrintPos(0, 15);
ucg.print("Press KEY 0 to turn on motor!");
// Initialize the stepper motor speed
myStepper.setSpeed(speedLevels[currentSpeedIndex]);
// Initialize the IR receiver
irController.begin();
}
void loop() {
Key21 command = irController.getKey();
if (command != Key21::NONE) {
switch (command) {
// case Key21::KEY_CH_MINUS:
// break;
case Key21::KEY_NEXT:
if (currentSpeedIndex < 8 && motorOn) currentSpeedIndex++; // Increase speed
break;
case Key21::KEY_PREV:
if (currentSpeedIndex > 0 && motorOn) currentSpeedIndex--; // Decrease speed
// TODO: YOUR CONTROL
break;
case Key21::KEY_1:
Serial.print("button1-speed1");
if (motorOn) currentSpeedIndex = 0;
break;
case Key21::KEY_2:
Serial.print("button2-speed2");
if (motorOn) currentSpeedIndex = 1;
break;
case Key21::KEY_3:
Serial.print("button3-speed3");
if (motorOn) currentSpeedIndex = 2;
break;
case Key21::KEY_4:
Serial.print("button4-speed4");
if (motorOn) currentSpeedIndex = 3;
break;
case Key21::KEY_5:
Serial.print("button5-speed5");
if (motorOn) currentSpeedIndex = 4;
break;
case Key21::KEY_6:
if (motorOn) currentSpeedIndex = 5;
break;
case Key21::KEY_7:
if (motorOn) currentSpeedIndex = 6;
break;
case Key21::KEY_8:
if (motorOn) currentSpeedIndex = 7;
break;
case Key21::KEY_9:
if (motorOn) currentSpeedIndex = 8;
break;
case Key21::KEY_0:
motorOn = true;
break;
case Key21::KEY_CH_MINUS:
motorOn = False
break;
default:
Serial.print("WARNING: undefined command:");
break;
}
// Update stepper motor speed
if (motorOn) {
myStepper.setSpeed(speedLevels[currentSpeedIndex]);
ucg.clearScreen();
ucg.setPrintPos(0, 15);
ucg.setColor(0, 255, 0); // Green color for speed display
ucg.setFont(ucg_font_ncenR12_tr);
ucg.print("Speed: ");
ucg.print(speedLevels[currentSpeedIndex]);
ucg.print(" RPM");
} else {
myStepper.setSpeed(0);
ucg.clearScreen();
ucg.setPrintPos(0, 15);
ucg.setColor(255, 0, 0); // Red color for motor off
ucg.setFont(ucg_font_ncenR12_tr);
ucg.print("Motor off");
}
delay(200); // Debounce delay
}
// Run the stepper motor at the current speed
if (motorOn) {
myStepper.step(stepsPerRevolution / 100); // Adjust steps to simulate continuous rotation
}
}