Need help with code for my project

Hello everyone. I'm working on a project right now, but had to make a few changes. Some background information. I'm using an arduino uno, with 2 28byj-48 stepper motors and associated ULN2003 driver boards. There is a rotary encoder and an 8 segment LED display that shows the speed. Due to the steppers in factory form not having enough torque, I decided to do the modification to make them bipolar instead of unipolar. So now I'm using an adafruit motor shield V2 to drive the steppers in bipolar mode. WAY MORE TORQUE now.
So the issue. The program I have is for using the original 28byj-48 stepper and ULN2003 driver. I need to fix this program so that it uses the V2 motor shield instead. I want the same features with the rotary encoder and the LED display. So basically I'm just changing the drier board.
I have no idea how to fix the code to do this. Please help. I posted the original code.

Astro_Motorized_Stepper_V2.ino (5.7 KB)

First post your code here so that we do not have to download it. I, among others, will not download code. It is just too much of a PITA. Read the forum guidelines to see how to properly post code and some information on how to get the most from this forum.

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

A L293 driver is not appropriate for modern bipolar stepper motors. The L293 is an inefficient, outdated and frankly poor DC motor driver, by today's standards, and even worse stepper driver. If you post the data sheet for the steppers that you want to use, we can help you choose an appropriate modern driver. Then we can work on your program.

What power supply do you have for the stepper? Volts? Amps?

Thanks for the help. I'll post the code when I get back to my desk. However I feel that even when I do post the code, I'm going to get the same answers. I'll keep looking for my answer elsewhere.

Thanks again.

Or you could just have simply posted the code

/*
  Astronomia Motorized Stepper code
  Using the Arduino Serial Monitor I was able to see that 22.00
    was the optimum initial Speed value for my build.
  Astro_Motorized_Stepper.ino
*/

//Include the Arduino Stepper Library
#include <Stepper.h>
#include <EEPROM.h>

// Define Constants
const int MAX_SPEED = 100;
const int STEPS_PER_REV = 32;


// Define 7-segment display variables
int num_array[10][7] = {  { 1,1,1,1,1,1,0 },    // 0
                          { 0,1,1,0,0,0,0 },    // 1
                          { 1,1,0,1,1,0,1 },    // 2
                          { 1,1,1,1,0,0,1 },    // 3
                          { 0,1,1,0,0,1,1 },    // 4
                          { 1,0,1,1,0,1,1 },    // 5
                          { 1,0,1,1,1,1,1 },    // 6
                          { 1,1,1,0,0,0,0 },    // 7
                          { 1,1,1,1,1,1,1 },    // 8
                          { 1,1,1,0,0,1,1 }};   // 9

int segmentPins[7] = {13, 12, 7, 6, 5, A4, A3};

int pwrLED = A5;

// Define Variables

// Create Instance of Stepper Class
// Specify Pins used for motor coils
// The pins used are 8,9,10,11
// Connected to ULN2003 Motor Driver In1, In2, In3, In4
// Pins entered in sequence 1-3-2-4 for proper step sequencing

Stepper steppermotor(STEPS_PER_REV, 8, 10, 9, 11);
int pValue; //knob value
int idxDigit;
int digitDisplayCount;
float speed; 
float savedEepromSpeed;
bool isOn;
bool canToggle;
bool isStartup;
bool flashBlank;
unsigned long time;
unsigned long lastTime;
unsigned long releaseTime;
unsigned long lastDigitDisplayTime;
unsigned long lastTurnDetected;

// Rotary Encoder
volatile boolean TurnDetected;
volatile boolean up;

const int PinCLK=2;                   // Used for generating interrupts using CLK signal
const int PinDT=3;                    // Used for reading DT signal
const int PinSW=4;                    // Used for the push button switch

const int DEFAULT_SPEED = 22;
const int DEBOUNCE_TIME = 250;


void isr ()  {                    // Interrupt service routine is executed when a HIGH to LOW transition is detected on CLK
 if (digitalRead(PinCLK))
   up = digitalRead(PinDT);
 else
   up = !digitalRead(PinDT);
 TurnDetected = true;
}

void setup()
{
  pinMode(PinCLK,INPUT);
  pinMode(PinDT,INPUT);  
  pinMode(PinSW,INPUT_PULLUP);
  
  attachInterrupt (0,isr,FALLING);   // interrupt 0 is always connected to pin 2 on Arduino UNO
  Serial.begin(115200);
  
  EEPROM.get(0, speed);
  savedEepromSpeed = speed;
  Serial.println(speed);

  if(isnan(speed)){
    speed = DEFAULT_SPEED;
    EEPROM.put(0, speed); 
    savedEepromSpeed = speed;   
  }  

  pinMode(pwrLED, OUTPUT);

  for(int i = 0; i < 7; i++){
    pinMode(segmentPins[i], OUTPUT);
  }

  isOn = true;
  canToggle = true;
  isStartup = true;
  lastTurnDetected = 0;

  resetDigitDisplay();
  
}

void loop()
{

  digitalWrite(pwrLED,isOn);
  
  if (isOn) {
    steppermotor.setSpeed(speed);
    steppermotor.step(1);
  }
  else {
    steppermotor.setSpeed(0);
  }

  static long virtualPosition=0;    // without STATIC it does not count correctly!!!

  if (digitalRead(PinSW))
  {
    releaseTime = millis();
    canToggle = true;
  }

  if (!(digitalRead(PinSW))) {      // check if pushbutton is pressed

   time = millis();

   if (time > releaseTime + 5000) {
    Serial.println("Speed Reset");
    speed = DEFAULT_SPEED;
    releaseTime = millis();
    resetDigitDisplay();
    blink_led(5);
    
   }

   if(canToggle and time > (lastTime + DEBOUNCE_TIME))
   {
     
     isOn = !isOn;
     canToggle = false;
     lastTime = millis();
  
     if(isOn)
     {
      Serial.println("Motor On");
      resetDigitDisplay();
     }
     else
     {
      Serial.println("Motor Off");
     }
   }
   
  }  
 
  if (TurnDetected and isOn)  {        // do this only if rotation was detected
   if (up)
     speed -= 1;
   else
     speed += 1;

   if (speed < 10){
     speed = 5;
   }
   else if (speed > MAX_SPEED){
     speed = MAX_SPEED;
   }
     
   TurnDetected = false;          // do NOT repeat IF loop until new rotation detected
   Serial.println (speed);
   lastTurnDetected = millis();
   
   resetDigitDisplay();

  }
  else if (TurnDetected) {
    TurnDetected = false;
  }

  writeSpeedToDisplay();

  //write to EEPROM if turn was detected
  //and has not been touched for 5 seconds
  if(lastTurnDetected > 0 && millis() > lastTurnDetected + 5000 && savedEepromSpeed != speed)
  {
    Serial.println("Speed Saved");
    EEPROM.put(0, speed);
    savedEepromSpeed = speed;
  }

}

void blink_led(int len){

  for (int i = 0; i < len; i++) {
    digitalWrite(pwrLED,1);
    delay(50);
    digitalWrite(pwrLED,0);
    delay(50);
  }
  
}

void writeNumber(int number) 
{
 
  for (int j=0; j < 7; j++) {
   digitalWrite(segmentPins[j], num_array[number][j]);
  
  }
}

void writeBlank()
{
   for (int j=0; j < 7; j++) {
      digitalWrite(segmentPins[j], LOW);
   }
}

void writeSpeedToDisplay()
{

    time = millis();

    if(time > lastDigitDisplayTime + 1000 && digitDisplayCount < 3 && isOn && time > lastTurnDetected + 1000){

       char b[5];
       String str;
       str=String(speed);
       str.toCharArray(b,5);

       if(b[idxDigit] == '.' || b[idxDigit] == NULL || flashBlank){
        writeBlank();
        flashBlank = false;
       }
       else{        
        writeNumber(b[idxDigit] - '0');
        flashBlank = true;
       }

       //display next digit
       if(!flashBlank) idxDigit += 1;

       //if next index is outside of array bounds, reset to zero
       if(idxDigit == 5){
        idxDigit = 0;
        digitDisplayCount++;  //increment display count to only show 3 times
       }
       
       lastDigitDisplayTime = time;
         
    }
    else if(!isOn || time <= lastTurnDetected + 1000)
    {
      writeBlank();
    }

}

void resetDigitDisplay(){

  idxDigit = 0;
  digitDisplayCount = 0;
  flashBlank = false;

}

In the past, we have been scammed, so most of us won't download code. Plus it's easier to read on the forum if it's properly formatted.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.