Help in removing the TFT screen code and changing buttons to user input

Hey, I want to remove the TFT screen and change the buttons to user input can someone help? I tried but kept getting errors.
Here is the code

#include <TFT.h>
#include <SPI.h>

//define pins for screen
#define LCD_RESET 8
#define CS   10
#define DC   9

//define pins for grating control
#define DIRECTION 3
#define STEP  5
#define LSWL 6//Limit switch low
#define LSWH 7//Limit switch high

//define pins for inputs
#define UP 0
#define DOWN 1
#define GO 2

TFT myScreen = TFT(CS, DC, LCD_RESET);
int wl_now = 666;
int wl_set = 666;
char print_now[4];
char print_set[4];
String str;



const int wl_max = 750;//?????????????
const int wl_min = 550;//?????????????
const int wl_step = 49;//0.1;// steps needed to move 1nm according to the rotary display

int calibModeLow = 0;
int calibModeHigh = 0;
int calibratedLow = 0;
int calibratedHigh = 0;

void updateNowVal() {
  str = String(wl_now);
  str.toCharArray(print_now,4);
  myScreen.stroke(255,255,255);
  myScreen.fill(255,255,255); //white
  myScreen.rect(5,72,40,15); // draw white box to clear text
  myScreen.stroke(0,0,0);
  myScreen.text(print_now,5,72); 
}

void updateSetVal() {
  str = String(wl_set);
  str.toCharArray(print_set,4);
  myScreen.stroke(255,255,255);
  myScreen.fill(255,255,255); //white
  myScreen.rect(5,109,40,15); // draw white box to clear text
  myScreen.stroke(0,0,0);
  myScreen.text(print_set,5,109); 
}

void checkButtons() {
  int upState = digitalRead(UP);
  int downState = digitalRead(DOWN);
  int goState = digitalRead(GO);
  myScreen.fill(255,255,255); // white
  myScreen.stroke(255,255,255);
  
  if(!upState && !downState && !goState){
    calibrationDecider();//enter calibration mode low, press 3 buttons a gain to go to calibration mode high, and again to exit calibration mode
  }
  
  if(upState==0){
    wl_set++;
    //myScreen.text("UP!",80,109);
    delay(100);
    //myScreen.stroke(255,255,255);
    //myScreen.rect(80,109,30,15);
    //myScreen.stroke(0,0,0);
    updateSetVal();
  }
  if(downState==0){
    wl_set--;
    //myScreen.text("DOWN!",80,109);
    delay(100);
    //myScreen.stroke(255,255,255);
    //myScreen.rect(80,109,30,15);
    //myScreen.stroke(0,0,0);
    updateSetVal();

  }
  if(goState==0){
    int higherLower = 0;
    if(wl_set>wl_now){
      higherLower = 1;
    }
    moveGrating(higherLower);
  }
}

void moveGrating(int upDown)  {
  //make the circle red to say it's moving
  myScreen.fill(255,0,0); // red
  myScreen.circle(135,100,20);
  // activate the grating movement then turn off the red circle
  digitalWrite(DIRECTION,upDown);//set direction pin (1 is up, 0 is down)
  // move up
  if(upDown){
   for(int i=wl_now; i<=wl_set; i++){
    for(int j=0; j <=wl_step; j++){
       // if (checkLimitsOK()){
          digitalWrite(STEP,1);
          //delay(50);
          digitalWrite(STEP,0);
          //delay(50);
           //wl_now = (i/wl_step);
          //updateNowVal();
          //Serial.print(i);
         //Serial.print('/n');
       }
       wl_now = i;
       updateNowVal();
    }
  }
    if(!upDown){
    for(int i=wl_now; i>=wl_set; i--){
      for(int j=0;j <=wl_step; j++){
    // if (checkLimitsOK()){
        digitalWrite(STEP,1);
        //delay(50);
        digitalWrite(STEP,0);
        //delay(50); 
        //wl_now = i/wl_step;
        //updateNowVal();
        //Serial.print(i);
        //Serial.print('/n');
      }
     wl_now = i;
     updateNowVal();
    }
  }
  //delay(50);
  //make the circle green to say it's OK
  myScreen.fill(0,255,0); // green
  myScreen.circle(135,100,20);
}

int checkLimitsOK(){
  int high = digitalRead(LSWH);
  int low = digitalRead(LSWL);  
  if(high || low){
    return(0);//return 0 if not ok
    if(high){
      wl_set = wl_max;
      wl_now = wl_max;
      myScreen.text("LS high",10,25);
      updateSetVal();
      updateNowVal();
      delay(1000);
  }
    if(low){
      wl_set = wl_min;
      wl_now = wl_min;
      myScreen.text("LS low",10,35);
      updateSetVal();
      updateNowVal();
      delay(1000);
    }
    
  }
  else{
    //myScreen.text("LS OK",10,45);
    return(1);//return 1 if ok
  }
}


void homing(){
  myScreen.text("Homing to min",20,15);
  digitalWrite(DIRECTION,0);
  while(checkLimitsOK()){
    digitalWrite(STEP,1);
    delay(10);
    digitalWrite(STEP,0);
    delay(10);
  }
 /* while(checkLimitsOK()){
    moveGrating(0);
  }*/
  wl_set = wl_min;
  wl_now = wl_min;
  delay(1000);
  myScreen.text("Homing to max",20,15);
    while(checkLimitsOK()){
    moveGrating(1);
  }
  wl_set = wl_max;
  wl_now = wl_max;
  myScreen.text("Homing complete",20,15);
  }

void calibrationDecider(){ // activate this by holding all three buttons
  /* these are my flags
  int calibModeLow = 0;
  int calibModeHigh = 0;
  int calibratedLow = 0;
  int calibratedHigh = 0;*/
  if (!calibModeLow && !calibModeHigh){ // if neither flag set, go to calibModeLow
    calibModeLow=1;
    calibScreen(0);
    return;
  } 
  if (calibModeLow && !calibModeHigh){ // if in low mode go to high mode & set that low calib has been done
    wl_set = wl_min;//set to 550 nm or whatever it is
    wl_now = wl_min;
    calibScreen(1);
    calibModeLow=0;
    calibratedLow=1;
    calibModeHigh=1;
    return;
  }
  if (!calibModeLow && calibModeHigh){ // if in high mode set that high calib has been done and go back to normal mode
    normalScreen();
    wl_set = wl_max;//set to 750 nm or whatever it is
    wl_now = wl_max;
    calibModeHigh=0;
    calibratedLow=1;
    calibratedHigh=1;
    return;
  }
  
}

void calibScreen(int mode){
  if(!mode){ // calib mode low
  myScreen.fill(255,255,255); 
  myScreen.rect(0,0,150,20); // clear the top with white
  myScreen.stroke(0,0,0);
  myScreen.setTextSize(2);
  myScreen.text("Calibration mode!",5,1);
  myScreen.stroke(255,255,255);
  myScreen.rect(0,55,150,20);
  myScreen.stroke(0,0,0);
  myScreen.text("find 550 nm",5,55);
  }
  if(mode){ // calib mode high
    myScreen.fill(255,255,255); 
  myScreen.rect(0,0,150,20); // clear the top with white
  myScreen.stroke(0,0,0);
  myScreen.setTextSize(2);
  myScreen.text("Calibration mode!",5,1);
  myScreen.stroke(255,255,255);
  myScreen.rect(0,55,150,20);
  myScreen.stroke(0,0,0);
  myScreen.text("find 750 nm",5,55);
  }
  return;
}

void normalScreen(){
  myScreen.background(255,255,255);  // clear the screen with white
  myScreen.stroke(0,0,0);
  myScreen.setTextSize(2);
  delay(500);  // pause for dramatic effect
 
  myScreen.text("MONOCHROMATOR!!!",5,1);
  myScreen.fill(200,0,200); // violet
  myScreen.rect(0,20,22,22);
  myScreen.fill(100,0,200); // indigo
  myScreen.rect(23,20,22,22);
  myScreen.fill(0,0,255); // blue
  myScreen.rect(46,20,22,22);
  myScreen.fill(0,255,0); // green
  myScreen.rect(69,20,22,22);
  myScreen.fill(255,255,0); // yellow
  myScreen.rect(92,20,22,22);
  myScreen.fill(255,66,0); // orange
  myScreen.rect(115,20,22,22);
  myScreen.fill(255,0,0); // red
  myScreen.rect(138,20,22,22);
}

void dramaticIntro(){
  myScreen.background(255,255,255);  // clear the screen with black
  myScreen.stroke(0,0,0);
  myScreen.setTextSize(2);
  delay(1000);  // pause for dramatic effect
 
  myScreen.text("MONOCHROMATOR!!!",5,1);
  delay(1000);
  myScreen.fill(200,0,200); // violet
  myScreen.rect(0,20,22,22);
  delay(100);
  myScreen.fill(100,0,200); // indigo
  myScreen.rect(23,20,22,22);
  delay(100);
  myScreen.fill(0,0,255); // blue
  myScreen.rect(46,20,22,22);
  delay(100);
  myScreen.fill(0,255,0); // green
  myScreen.rect(69,20,22,22);
  delay(100);
  myScreen.fill(255,255,0); // yellow
  myScreen.rect(92,20,22,22);
  delay(100);
  myScreen.fill(255,66,0); // orange
  myScreen.rect(115,20,22,22);
  delay(100);
  myScreen.fill(255,0,0); // red
  myScreen.rect(138,20,22,22);
  delay(1000);

  myScreen.text("WAVELENGTH:",5,55);
  myScreen.text("SET TO:",5,92);
  
  myScreen.text("nm",50,72);
  myScreen.text("nm",50,109);  
}

void setup() {
  pinMode(DIRECTION, OUTPUT);
  pinMode(STEP, OUTPUT);
  pinMode(LSWL, INPUT);
  pinMode(LSWH, INPUT);
  pinMode(UP, INPUT_PULLUP);
  pinMode(DOWN, INPUT_PULLUP);
  pinMode(GO, INPUT_PULLUP);
  
  Serial.begin(9600);
  Serial.print("hello");
  
  myScreen.begin();  
  dramaticIntro();

 // myScreen.stroke(255,255,255);
 // myScreen.fill(255,255,255); // white

  updateNowVal();
  
  // homing();
  delay(1000);
}

void loop() {
  // put your main code here, to run repeatedly:
  
  checkButtons();
  

  
}

What errors?

When you answer that, please copy and paste the entire compiler error message here.

It could be related to not having any loop() or setup() functions. :wink:

@Railroader @anon57585045 Okay, I successfully removed the code for the TFT screen, but now I want to change buttons to user input. How can I do it?

What have you tried?

Explain what "user input" is. Make a little schematics.

Please don't edit previous posts to update code. It makes nonsense out of the replies. Put it in a new message.

@anon57585045 I haven't much tried yet but trying.
@Railroader Can I make the user inputs as

  1. move grating to "any number" steps UP and DOWN.
  2. It shows the grating is moving or stopping.
  3. Can turn on the calibration mode.

Then please show us what you have done. Code and a drawing showing how the buttons are connected...

What is the "grating"?
What is the "calibration mode"?

@aarg So the Buttons are connected as in the code above as Pin for input UP 0 DOWN 1 GO 2 which is I think not good. As someone told me to not use 0, 1 and serial input simultaneously. So the buttons control a stepper motor to move the grating (it's an optical component).
Till now I got this code (i don't know if I successfully removed the TFT screen or not but it verifying and compiling).

#include <SPI.h>


//define pins for grating control
#define DIRECTION 3
#define STEP  5
#define LSWL 6//Limit switch low
#define LSWH 7//Limit switch high

//define pins for inputs
#define UP 0
#define DOWN 1
#define GO 2

int wl_now = 666;
int wl_set = 666;
char print_now[4];
char print_set[4];
String str;



const int wl_max = 750;//?????????????
const int wl_min = 550;//?????????????
const int wl_step = 49;//0.1;// steps needed to move 1nm according to the rotary display

int calibModeLow = 0;
int calibModeHigh = 0;
int calibratedLow = 0;
int calibratedHigh = 0;

void updateNowVal() {
  str = String(wl_now);
  str.toCharArray(print_now,4);

}

void updateSetVal() {
  str = String(wl_set);
  str.toCharArray(print_set,4);
  
}

void checkButtons() {
  int upState = digitalRead(UP);
  int downState = digitalRead(DOWN);
  int goState = digitalRead(GO);
  
  
  if(!upState && !downState && !goState){
    calibrationDecider();//enter calibration mode low, press 3 buttons a gain to go to calibration mode high, and again to exit calibration mode
  }
  
  if(upState==0){
    wl_set++;
   
    delay(100);
    
    updateSetVal();
  }
  if(downState==0){
    wl_set--;
    delay(100);
    updateSetVal();

  }
  if(goState==0){
    int higherLower = 0;
    if(wl_set>wl_now){
      higherLower = 1;
    }
    moveGrating(higherLower);
  }
}

void moveGrating(int upDown)  {
  // activate the grating movement then turn off the red circle
  digitalWrite(DIRECTION,upDown);//set direction pin (1 is up, 0 is down)
  // move up
  if(upDown){
   for(int i=wl_now; i<=wl_set; i++){
    for(int j=0; j <=wl_step; j++){
       // if (checkLimitsOK()){
          digitalWrite(STEP,1);
          //delay(50);
          digitalWrite(STEP,0);
          //delay(50);
           //wl_now = (i/wl_step);
          //updateNowVal();
          //Serial.print(i);
         //Serial.print('/n');
       }
       wl_now = i;
       updateNowVal();
    }
  }
    if(!upDown){
    for(int i=wl_now; i>=wl_set; i--){
      for(int j=0;j <=wl_step; j++){
    // if (checkLimitsOK()){
        digitalWrite(STEP,1);
        //delay(50);
        digitalWrite(STEP,0);
        //delay(50); 
        //wl_now = i/wl_step;
        //updateNowVal();
        //Serial.print(i);
        //Serial.print('/n');
      }
     wl_now = i;
     updateNowVal();
    }
  }
  //delay(50);
 } 

int checkLimitsOK(){
  int high = digitalRead(LSWH);
  int low = digitalRead(LSWL);  
  if(high || low){
    return(0);//return 0 if not ok
    if(high){
      wl_set = wl_max;
      wl_now = wl_max;
      updateSetVal();
      updateNowVal();
      delay(1000);
  }
    if(low){
      wl_set = wl_min;
      wl_now = wl_min;
      updateSetVal();
      updateNowVal();
      delay(1000);
    }
    
  }
  else{
    return(1);//return 1 if ok
  }
}


void homing(){
  digitalWrite(DIRECTION,0);
  while(checkLimitsOK()){
    digitalWrite(STEP,1);
    delay(10);
    digitalWrite(STEP,0);
    delay(10);
  }
 /* while(checkLimitsOK()){
    moveGrating(0);
  }*/
  wl_set = wl_min;
  wl_now = wl_min;
  delay(1000);
    while(checkLimitsOK()){
    moveGrating(1);
  }
  wl_set = wl_max;
  wl_now = wl_max;
  }

void calibrationDecider(){ // activate this by holding all three buttons
  /* these are my flags
  int calibModeLow = 0;
  int calibModeHigh = 0;
  int calibratedLow = 0;
  int calibratedHigh = 0;*/
  if (!calibModeLow && !calibModeHigh){ // if neither flag set, go to calibModeLow
    calibModeLow=1;
    return;
  } 
  if (calibModeLow && !calibModeHigh){ // if in low mode go to high mode & set that low calib has been done
    wl_set = wl_min;//set to 550 nm or whatever it is
    wl_now = wl_min;
    calibModeLow=0;
    calibratedLow=1;
    calibModeHigh=1;
    return;
  }
  if (!calibModeLow && calibModeHigh){ // if in high mode set that high calib has been done and go back to normal mode
    wl_set = wl_max;//set to 750 nm or whatever it is
    wl_now = wl_max;
    calibModeHigh=0;
    calibratedLow=1;
    calibratedHigh=1;
    return;
  }
  
}

void setup() {
  pinMode(DIRECTION, OUTPUT);
  pinMode(STEP, OUTPUT);
  pinMode(LSWL, INPUT);
  pinMode(LSWH, INPUT);
  pinMode(UP, INPUT_PULLUP);
  pinMode(DOWN, INPUT_PULLUP);
  pinMode(GO, INPUT_PULLUP);
  
  Serial.begin(9600);
  Serial.print("hello");
  
  updateNowVal();
  
  // homing();
  delay(1000);
}

void loop() {
  // put your main code here, to run repeatedly:
  
  checkButtons();
  

  
}

How are your switch buttons wired? Have you tested the buttons using a simple test sketch so you know that they work?

It is true that you should not use pins 0 and 1 for I/O, they are attached to the serial port. So what is your plan for pin connections?

Is this project the same one that you asked about in this previous thread?
https://forum.arduino.cc/t/can-anyone-help-me-understanding-the-code-to-controlling-a-stepper-motor-by-arduino-interfacing-with-micromanager/1017263/33

@aarg I tried testing the buttons one by one on the breadboard but they are not giving any output.
yeah, it is somewhat similar but in that, I wanted to control it using a micromanager but couldn't find any answers so first, I wanted to try if I can control it just by user input. Should I combine this topic with that?

It's okay to continue this thread as it is. However you are not providing the details we have asked for. If the buttons don't work at all, we need:

  1. The button test sketch you are using
  2. A wiring diagram of the switch connections

@anon57585045 So I tried testing the buttons like this for UP button (is it wrong)


And then connected the pins of the stepper motor (from monochromator) as in the code
DIRECTION 3
STEP 5
LSWL 6
LSWH 7
Someone tried it with the TFT screen with three buttons as the original code. And it is not working and there are no free pins to rewire for the buttons as it is connected to a TFT screen and it covers all the pins. That's why I wanted to remove the screen and try with either button or by user input. Can you tell if anything wrong with the original code except the buttons?

You provided only

A wiring diagram of the switch connections

but not

The button test sketch you are using

and you provided no useful feedback about the result, only this

I tried testing the buttons like this for UP button

and asked another question

(is it wrong)

With those kinds of responses, it would take me almost forever to help you. So good luck with your project.

@anon57585045 Sorry, I don't know how can I describe things. So here is what I am trying to do


In the picture, you can see that the 4 wires coming from the monochromator which are
Direction, step and limit switch high and low. And I just want to control the motor but don't know the coding part. The only spec about the motor is max. 500steps/sec.

There must also be some common (ground) connection. It would never work without that.