Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to turn all led on on a stepper to run the stepper motor #23

Open
yoongteng opened this issue Jun 12, 2023 · 0 comments
Open

How to turn all led on on a stepper to run the stepper motor #23

yoongteng opened this issue Jun 12, 2023 · 0 comments

Comments

@yoongteng
Copy link

Hi guys my issue on this project is when i run the below code. The stepper led lights up one by one and cant function the stepper motor. The load cell works well with the oled display. Izit the interruption between virtual pin that cause this issue?

#include <TFT_eSPI.h>

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

#include <Stepper.h>
#include <HX711.h>
#include <Wire.h>
#include "soc/rtc.h"
#include <stdlib.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SimpleTimer.h>
#include "esp32-hal-cpu.h"

#include <WiFi.h>
#include <WiFiClient.h>
const char ssid[] = "4G CPE FF5A"; // WiFi name
const char password[] = "yuankai00"; // WiFi password

const int LOADCELL1_DOUT_PIN = 19;
const int LOADCELL_SCK_PIN = 23;

#define SENSORCNT 1
HX711 scale[SENSORCNT];

// Define the pins for the OLED display
const int OLED_SDA_PIN = 21;
const int OLED_SCL_PIN = 22;

int rbutton = 18; // this button will be used to reset the scale to 0.
String myString;
String cmessage; // complete message
char buff[10];
float weight;
float calibration_factor = 410.45; // for me this vlaue works just perfect 206140

SimpleTimer timer;

// for the OLED display

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#include <BlynkSimpleEsp32.h>
char auth[] = "lQkgSEGxVzm3SAfET_85JU7DE3tCP1v7"; // Blynk Authentication Token
WiFiServer server(80);

#include <SPI.h>
#include <TFT_eSPI.h>
TFT_eSPI tft = TFT_eSPI(); // Resolution 135 x 240
#define FF17 &FreeSans9pt7b
#define FF21 &FreeSansBold9pt7b
#define ROW1 0,16
#define ROW2 0,38
#define ROW3 0,60
#define ROW4 0,82
#define ROW5 0,104
#define ROW6 0,126

#define BUTTON1 35
#define BUTTON2 0

#define STEPPER_IN1 27
#define STEPPER_IN2 26
#define STEPPER_IN3 25
#define STEPPER_IN4 33

#define REVOLUTION_STEP 2048 // 1 revolution

boolean stepperDirection = false;
int stepperStep = 0;
int stepperStepCount = 0;
boolean stepperMove = false;
long prevMillisStepper = 0;
int intervalStepper = 4; // Minimum is 2

boolean button1Pressed = false;
boolean button2Pressed = false;

BLYNK_WRITE(V0) // Button Widget is writing to pin V0
{
stepperDirection = param.asInt();

tft.fillRect(120, 65, 120, 25, TFT_BLACK);
tft.setCursor(120, 82);
if (stepperDirection) {
tft.print("CCW");
}
else {
tft.print("CW");
}
}

BLYNK_WRITE(V1) // Button Widget is writing to pin V1
{
int stepperSpeed = param.asInt();

tft.fillRect(120, 87, 120, 25, TFT_BLACK);
tft.setCursor(120, 104);
if (stepperSpeed == 1) {
intervalStepper = 4;
tft.print("Low");
}
else if (stepperSpeed == 2) {
intervalStepper = 3;
tft.print("Medium");
}
else if (stepperSpeed == 3) {
intervalStepper = 2;
tft.print("High");
}
}

BLYNK_WRITE(V2) // Button Widget is writing to pin V2
{
stepperMove = true;
stepperStepCount = 2;
stepperStep = 4;

tft.fillRect(120, 109, 120, 25, TFT_BLACK);
tft.setCursor(120, 126);
tft.print("Run");
}

void setup()
{
pinMode(BUTTON1, INPUT_PULLUP);
pinMode(BUTTON2, INPUT_PULLUP);

pinMode(STEPPER_IN1, OUTPUT);
pinMode(STEPPER_IN2, OUTPUT);
pinMode(STEPPER_IN3, OUTPUT);
pinMode(STEPPER_IN4, OUTPUT);

Serial.begin(115200);
Serial.print("Initialize Blynk.");

tft.init();
tft.setRotation(3);
tft.fillScreen(TFT_BLACK);

tft.setFreeFont(FF21);
tft.setTextColor(TFT_BLUE);
tft.setCursor(ROW1);
tft.print("Blynk Status:");

tft.setFreeFont(FF17);
tft.setTextColor(TFT_WHITE);
tft.setCursor(120, 16);
tft.print("Initialize...");

WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());

Blynk.begin(auth, ssid, password);

tft.fillRect(120, 0, 120, 35, TFT_BLACK);
tft.setCursor(120, 16);
tft.print("Ready!");

tft.setFreeFont(FF21);
tft.setTextColor(TFT_RED);
tft.setCursor(ROW3);
tft.print("STEPPER");
tft.setTextColor(TFT_GREEN);
tft.setCursor(ROW4);
tft.print("Direction:");
tft.setCursor(ROW5);
tft.print("Speed:");
tft.setCursor(ROW6);
tft.print("Status:");

tft.setFreeFont(FF17);
tft.setTextColor(TFT_YELLOW);
tft.setCursor(120, 82);
tft.print("CW");
tft.setCursor(120, 104);
tft.print("Low");
tft.setCursor(120, 126);
tft.print("Stop");

Blynk.virtualWrite(0, 0);
Blynk.virtualWrite(1, 1);
Blynk.virtualWrite(2, 0);

Serial.begin(115200);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();

display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("Load Cell");
display.display();

scale[0].begin(LOADCELL1_DOUT_PIN, LOADCELL_SCK_PIN);
//scale[1]->begin(LOADCELL2_DOUT_PIN, LOADCELL_SCK_PIN);
//scale[2]->begin(LOADCELL3_DOUT_PIN, LOADCELL_SCK_PIN);

scale[0].set_scale(410.45);
scale[0].tare();
scale[0].read();
scale[0].read_average(20);
scale[0].get_value(5);
scale[0].get_units(5), 1;

//setCpuFrequencyMhz(RTC_CPU_FREQ_80M);
//pinMode(rbutton, INPUT_PULLUP);
//scale.set_scale();
//scale.tare(); //Reset the scale to 0
//long zero_factor = scale.read_average(); //Get a baseline reading

//display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
//timer.setInterval(1000L, getSendData);
//display.clearDisplay();
//display.setTextColor(WHITE);

}

void loop()
{
Blynk.run();
timer.run();

char str[12];
float units = 0.0;
display.setTextSize(1);
display.setTextColor(WHITE, BLACK);

display.setCursor(0, 16);
display.print("Weight : ");
units = scale[0].get_units();
display.println(units, 1);
display.display();

display.setCursor(0, 16);
display.print("Weight : ");
units = scale[0].get_units(10);
display.println(units, 1);
display.display();

scale[0].power_down();
delay(1000);
scale[0].power_up();

if (digitalRead(BUTTON1) == LOW &&
button1Pressed == false) {
button1Pressed = true;

stepperDirection = false;
stepperMove = true;
stepperStepCount = 0;
stepperStep = 1;

}
else if (digitalRead(BUTTON1) == HIGH &&
button1Pressed == true) {
button1Pressed = false;
}

if (digitalRead(BUTTON2) == LOW) {

stepperDirection = true;
stepperMove = true;
stepperStepCount = 0;
stepperStep = 1;

}
else if (digitalRead(BUTTON2) == HIGH &&
button2Pressed == true) {
button2Pressed = false;
}

if (millis() - prevMillisStepper > intervalStepper) {

if (stepperMove == true) {
  if (stepperDirection) {
    if (stepperStep++ >= 3) {
      stepperStep = 0;
    }
  }
  else {
    if (stepperStep-- == 0) {
      stepperStep = 3;
    }
  }

  if (stepperStepCount++ == REVOLUTION_STEP) {
    stepperMove = false;
    stepperStep = 4;

    Blynk.virtualWrite(2, 0);
    
    tft.fillRect(120, 109, 120, 25, TFT_BLACK);
    tft.setCursor(120, 126);
    tft.print("Stop");
  }

  switch (stepperStep) {
    case 0:
      digitalWrite(STEPPER_IN1, HIGH);
      digitalWrite(STEPPER_IN2, LOW);
      digitalWrite(STEPPER_IN3, LOW);
      digitalWrite(STEPPER_IN4, LOW);
      break;

    case 1:
      digitalWrite(STEPPER_IN1, LOW);
      digitalWrite(STEPPER_IN2, HIGH);
      digitalWrite(STEPPER_IN3, LOW);
      digitalWrite(STEPPER_IN4, LOW);
      break;

    case 2:
      digitalWrite(STEPPER_IN1, LOW);
      digitalWrite(STEPPER_IN2, LOW);
      digitalWrite(STEPPER_IN3, HIGH);
      digitalWrite(STEPPER_IN4, LOW);
      break;

    case 3:
      digitalWrite(STEPPER_IN1, LOW);
      digitalWrite(STEPPER_IN2, LOW);
      digitalWrite(STEPPER_IN3, LOW);
      digitalWrite(STEPPER_IN4, HIGH);
      break;

    default:
      digitalWrite(STEPPER_IN1, LOW);
      digitalWrite(STEPPER_IN2, LOW);
      digitalWrite(STEPPER_IN3, LOW);
      digitalWrite(STEPPER_IN4, LOW);
      break;
  }
}

prevMillisStepper = millis();

}
}

void getSendData()
{

//Blynk.virtualWrite(V3,"Weight:");
//Blynk.virtualWrite(V4,myString);

// Oled display
display.clearDisplay();
display.setTextSize(3);
display.setCursor(0,0); // column row
display.print("Weight:");

display.setTextSize(4);
display.setCursor(0,30);
display.print(myString);

display.display();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant