MKS Gen 1.4 Output pins react weird

Hi everyone there,

i am working on Stepper motor which is driven by MKS Gen 1.4 Board (its Arduino Mega chip) and graphical display

My code is bit bulky and having unnecessary stuff as well, as i have make it with the help of various codes available on internet.

however its working as required till the date but now after putting optocoupler for inputs.

for inputs i have Optical diffusion type sensor

all the button mention in code are above device only, following is the issue i am facing with this code compare to earlier.

  1. earlier the Output change as soon as input get change but now output change only if input change and again back to initial stage. (here output is used to drive steppers motors)

one issue having since starting as below.

  1. every time its updating to text file instead of taking directly refence from the text file stored in SD Card. its note there in void loop but still its updating at each cycle
//Display library

#include <GEM_u8g2.h>
U8G2_ST7920_128X64_1_SW_SPI u8g2(U8G2_R0, 23 , 17, 16 );


// SD Library

#include <SPI.h>
#include <SD.h>

//Keypad

#define B1 40
#define B2 44
#define B3 59
#define B4 63
#define B5 42
#define B6 65



#define MOSI 51
#define MISO 50
#define CLK 66
#define CS 53


File myFile;

//Steppers

//stepper-1

#define EN1 38
#define X_DIR 55
#define X_STP 54
const int buttonPin1 = 2;
int buttonState1 = 0;


//stepper-2

#define EN2 56
#define Y_DIR 61
#define Y_STP 60
const int buttonPin2 = 3;
int buttonState2 = 0;


const int buttonPin3 = 14;
int buttonState3 = 0;

// Create variables that will be editable through the menu and assign them initial values

int Length1 = 0;
int Speed1 = 0;
int Length2 = 0;
int Speed2 = 0;
int Delay1 = 0;
int Delay2 = 0;
int Rot1 = 0;
int Rot2 = 0;

boolean CW = false;
//boolean Clock2 = false;

//M1 motor setting
GEMItem menuItemIntervalM1("Length:", Length1);
GEMItem menuItemSpeedM1("Speed:", Speed1);
GEMItem menuItemDelayM1("Delay:", Delay1);
GEMItem menuItemRotM1("Rotation:", Rot1);
//GEMItem menuItemBool("Rotatioin:", CW);
//M2 motor setting
GEMItem menuItemIntervalM2("Length:", Length2);
GEMItem menuItemSpeedM2("Speed:", Speed2);
GEMItem menuItemDelayM2("Delay:", Delay2);
GEMItem menuItemRotM2("Rotation:", Rot2);
//GEMItem menuItemBool("Rotatioin:", Clock2);
// Motor steps codes

void step(boolean dir, byte dirPin, byte stepperPin, int steps, int delayTime)
{
  digitalWrite(dirPin, dir);
  delay(100);
  for (int i = 0; i < steps; i++)
  {
    digitalWrite(stepperPin, HIGH);
    delayMicroseconds(delayTime);
    digitalWrite(stepperPin, LOW);
    delayMicroseconds(delayTime);
  }
}


// Main Menu List

GEMPage menuPageMain("Select Motor"); // Main page
GEMPage menuPageM1("M1"); // Settings submenu
GEMPage menuPageM2("M2");

// Create menu item linked to Settings menu page

GEMItem menuItemMainM1("M1", menuPageM1);
GEMItem menuItemMainM2("M2", menuPageM2);

// Create menu object of class GEM_u8g2. Supply its constructor with reference to u8g2 object we created earlier

GEM_u8g2 menu(u8g2);

// one time runing items in setup menu

void setup() {
  Serial.begin(115200);

  Serial.println("Initializing SD card... ");

  if (!SD.begin(53)) {
    Serial.println("Card initialization failed!");
    while (true);
  }

  Serial.println("Card Initialization Success.....");

  Length1 = readFile("Length1.txt"); //read full content of file
  Speed1 = readFile("Speed1.txt"); //read full content of file
  Delay1 = readFile("Delay1.txt"); //read full content of file
  Rot1 = readFile("Rot1.txt"); //read full content of file
  Length2 = readFile("Length2.txt"); //read full content of file
  Speed2 = readFile("Speed2.txt"); //read full content of file
  Delay2 = readFile("Delay2.txt"); //read full content of file
  Rot2 = readFile("Rot2.txt"); //read full content of file

  //M1 pin define
  pinMode(buttonPin1, INPUT);
  pinMode(X_DIR, OUTPUT); pinMode(X_STP, OUTPUT);
  pinMode(EN1, OUTPUT);
  digitalWrite(EN1, LOW);

  //M2 Pin define
  pinMode(buttonPin2, INPUT);
  pinMode(Y_DIR, OUTPUT); pinMode(Y_STP, OUTPUT);
  pinMode(EN2, OUTPUT);
  digitalWrite(EN2, LOW);

  // limit switch
  pinMode(buttonPin3, INPUT);

  //Keypad
  pinMode(B1, INPUT);
  pinMode(B2, INPUT);
  pinMode(B3, INPUT);
  pinMode(B4, INPUT);
  pinMode(B5, INPUT);
  pinMode(B6, INPUT);



  //u8g2.begin(/*Select/OK=*/ 35, /*Right/Next=*/ 31, /*Left/Prev=*/ 15, /*Up=*/ U8X8_PIN_NONE, /*Down=*/ 33, /*Home/Cancel=*/ 18); // without Remote
  u8g2.begin(/*Select/OK=*/ B1, /*Right/Next=*/ B2, /*Left/Prev=*/ B3, /*Up=*/ B4, /*Down=*/ B5, /*Home/Cancel=*/ B6); //with Remote
  // Menu init, setup and draw
  menu.init();
  setupMenu();
  menu.drawMenu();
}

void setupMenu() {

  // Add menu items to Settings menu page
  menuPageM1.addMenuItem(menuItemIntervalM1);
  menuPageM1.addMenuItem(menuItemSpeedM1);
  menuPageM1.addMenuItem(menuItemDelayM1);
  menuPageM1.addMenuItem(menuItemRotM1);

  menuPageM2.addMenuItem(menuItemIntervalM2);
  menuPageM2.addMenuItem(menuItemSpeedM2);
  menuPageM2.addMenuItem(menuItemDelayM2);
  menuPageM2.addMenuItem(menuItemRotM2);

  // Add menu items to Main Menu page

  menuPageMain.addMenuItem(menuItemMainM1);
  menuPageMain.addMenuItem(menuItemMainM2);

  // Specify parent menu page for the Settings menu page

  menuPageM1.setParentMenuPage(menuPageMain);
  menuPageM2.setParentMenuPage(menuPageMain);

  // Add Main Menu page to menu and set it as current
  menu.setMenuPageCurrent(menuPageMain);
}

int steps1;
int steps2;
int delTime1;
int delTime2;
int DelayTime1;
int DelayTime2;
int RotTime1;
int RotTime2;



void loop() {

  // If menu is ready to accept button press...

  if (menu.readyForKey())
    // ...detect key press using U8g2 library and pass pressed button to menu
    menu.registerKeyPress(u8g2.getMenuEvent());

  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);

  if (buttonState1 == LOW) {
    delay (10);

    buttonState1 = digitalRead(buttonPin1);

    if (buttonState1 == LOW && buttonState2 == HIGH && buttonState3 == LOW) {

      if (steps1 != Length1) {
        steps1 = validateIntervalM1();
      }
      else {
        steps1 = Length1;
      }

      if (delTime1 != Speed1) {
        delTime1 = validateSpeedM1();
      }
      else {
        delTime1 = Speed1;
      }

      if (RotTime1 != Rot1) {
        RotTime1 = validateRotM1();
      }
      else {
        RotTime1 = Rot1;
      }

      if (RotTime1 == 1) {
        step(true,  X_DIR, X_STP, steps1, delTime1);
        // Serial.println("Rotate Clockwise... ");
      }
      else {
        if (RotTime1 == 0) {
          step(true, -X_DIR, X_STP, steps1, delTime1);
          //Serial.println("Rotate Anti Clockwise... ");
        }
      }
      if (DelayTime1 != Delay1) {
        DelayTime1 = validateDelayM1();
      }
      else {
        DelayTime1 = Delay1;
      }
      delay(DelayTime1);
    }

    else if (buttonState1 == LOW && buttonState2 == LOW && buttonState3 == LOW) {

      if (steps2 != Length2) {
        steps2 = validateIntervalM2();
      }
      else {
        steps2 = Length2;
      }

      if (delTime2 != Speed2) {
        delTime2 = validateSpeedM2();
      }
      else {
        delTime2 = Speed2;
      }

      if (RotTime2 != Rot2) {
        RotTime2 = validateRotM2();
      }
      else {
        RotTime2 = Rot2;
      }

      if (RotTime2 == 1) {
        step(true, Y_DIR, Y_STP, steps2, delTime2);
        // Serial.println("Rotate Clockwise... ");
      }
      else {
        if (RotTime2 == 0) {
          step(true, -Y_DIR, Y_STP, steps2, delTime2);
          //Serial.println("Rotate Anti Clockwise... ");
        }
      }

      if (DelayTime2 != Delay2) {
        DelayTime2 = validateDelayM2();
      }
      else {
        DelayTime2 = Delay2;
      }

      delay(DelayTime2);
    }
  }
}

//Validation routine of interval variable
int validateIntervalM1() {
  if (Length1 < 00) {
    Length1 = 0;
  }
  Serial.print("Interval set: ");
  Serial.println(Length1);

  writeFile("Length1.txt", Length1);

  return Length1;
}


int validateSpeedM1() {
  if (Speed1 < 0) {
    Speed1 = 0;
  }
  Serial.print("Interval set: ");
  Serial.println(Speed1);

  writeFile("Speed1.txt", Speed1);

  return Speed1;
}

int validateDelayM1() {
  if (Delay1 < 0) {
    Delay1 = 0;
  }
  Serial.print("Interval set: ");
  Serial.println(Delay1);

  writeFile("Delay1.txt", Delay1);

  return Delay1;
}


int validateIntervalM2() {
  if (Length2 < 0) {
    Length2 = 0;
  }
  Serial.print("Interval set: ");
  Serial.println(Length2);

  writeFile("Length2.txt", Length2);
  return Length2;
}

int validateSpeedM2() {
  if (Speed2 < 0) {
    Speed2 = 0;
  }
  Serial.print("Interval set: ");
  Serial.println(Speed2);

  writeFile("Speed2.txt", Speed2);
  return Speed2;
}

int validateDelayM2() {
  if (Delay2 < 0) {
    Delay2 = 0;
  }
  Serial.print("Interval set: ");
  Serial.println(Delay2);

  writeFile("Delay2.txt", Delay2);
  return Delay2;
}

int validateRotM1() {
  if (Rot1 < 0) {
    Rot1 = 0;
  }

  Serial.print("Rot1 : ");
  Serial.println(Rot1);

  writeFile("Rot1.txt", Rot1);

  return Rot1;
}

int validateRotM2() {
  if (Rot2 < 0) {
    Rot2 = 0;
  }

  Serial.print("Rot2 : ");
  Serial.println(Rot2);

  writeFile("Rot2.txt", Rot2);

  return Rot2;
}



int readFile(String fileName) {
  myFile = SD.open(fileName);
  if (myFile) {

    // read from the file until there's nothing else in it:
    String received = "";
    char ch;
    while (myFile.available()) {
      ch = myFile.read();

      if (ch == '\n')
      {
        break;
      }
      else
      {
        received += ch;
      }
    }

    myFile.close();
    return received.toInt();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening file...");
  }
}

void writeFile(String FileName, int NewValue) {
  SD.remove(FileName);
  myFile = SD.open(FileName, FILE_WRITE);
  if (myFile) {

    myFile.println(NewValue);

    myFile.close();
    Serial.println("File update successfully...");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening in " + FileName);
  }
}

if someone can guide me to resolve this issue then it will be great help to me, i have already damage one MKS gen board, display, optical sensors and many more devices for trial and error.

thanks in advance for your valuable time

You should really post a before and after schematic. However, are you aware that putting an optocoupler between a signal and an input inverts the signal. So where as before you might be looking for a logic one to indicate a button pressed you are now looking for a logic zero to indicate a press.

1 Like

thanks @Grumpy_Mike for your valuable time and feedback, will look into this and try to upload the schematic as well.
i was not aware about that inverting phenomenon

hi @Grumpy_Mike thanks for your suggestion, i have made the changes as per inverted inputs signal getting from optocoupler.

requesting you to please help me into SD writing and reference as well, i ma really unable to get it corrected though its working.

  1. files getting updated if values is not the same but checking every time by opening the file from SD card.

what i actually required is.

  1. at the time power on it should run from the SD card and then should keep that value in floating until the value not changed manually

big thanks in advance

OK So exactly what do you mean by this?

In the setup function you could open the SD file for writing.

But the problem is that the file needs to be closed before power is removed from the Arduino. Otherwise it is likely to be corrupted, or worst still the SD card may be damaged to such an extent that it doesn't work any more.

This means that to do what you want safely then you must have some hardware that will monitor the power at some point and generate an interrupt that gives you enough warning time to allow you to close the SD file.

This is a big problem on the Raspberry Pi with people treating it like an Arduino and just yanking out to power down. This leads to a very short life on the SD card.

I have even had problems with this myself when some code refuses to quit and you can't close the machine down correctly the you have to remove the power. The result is an SD card that you can read but that you can't program or even format.

apologies for the delay in reply and thanks a lot @Grumpy_Mike ...most precious gift is time and simple thanks may not be sufficient... but still would like to say big thanks for your reply

1 Like

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