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();
}

