0

I've just followed a tutorial so that when a button is pressed it goes through a menu. Click once, next slide. Only issue is it isn't appearing on my screen at all. It has uploaded but I'm a total novice when it comes to breadboards etc. Here is my code:

    #include "Ucglib.h" // library for this display
#include <TFT.h> // Hardware-specific library
#include <SPI.h>

#define CS   10
#define DC   8
#define RESET  9 

#define up 5
#define down 6

TFT TFTscreen = TFT(CS, DC, RESET);
const int menuSize = 4;
String menuItems(menuSize);

bool upLastState = HIGH;
bool downLastState = HIGH;

int currentMenu = 0;
String temp;
char currentPrintOut(10);


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

  pinMode(up, INPUT_PULLUP);
  pinMode(down, INPUT_PULLUP);

  menuItems[0] = ":)";
  menuItems[1] = ":(";
  menuItems[2] = "X(";
  menuItems[3] = ":S";
  menuItems[4] = ":T";

  //initialize the library
  TFTscreen.begin();

  MenuChanged();
}

void loop() {
  if (digitalRead(up) != upLastState) {
    upLastState = !upLastState;
  }

  if (!upLastState) {
    if (currentMenu > 0) {
      currentMenu--;
    } else {
      currentMenu = menuSize - 1;
    }
    MenuChanged();
  } else {
  }


 if (digitalRead(down) !=downLastState) {
  downLastState = !downLastState;

  if (!downLastState) {
    if (currentMenu < menuSize - 1) {
      currentMenu++;
    } else {
      currentMenu = 0;
    }
    //Serial.printIn(menuItems[currentMenu]);
    MenuChanged();
  } else {
    //Release
  }
 }

   delay(50);
 }

 void MenuChanged() {
  Serial.println(menuItems[currentMenu]);

// clear the screen with a black background
  TFTscreen.background(0,0,0);
  TFTscreen.setTextSize(9);
  TFTscreen.stroke(255,255,255);

  temp = String(menuItems[currentMenu]);
  temp.toCharArray(currentPrintOut, 10);

  TFTscreen.text(currentPrintOut, 40, 32);
}

Edited back to original as per request

  • remove #define up 5 and #define down 6, we already defined pin 5 for the button. also remove TFTscreen.text(":)", 40, 32); which is in front of defualt:. the serial.begin() and serial.println() and delay(100) are just for testing the code, you can remove them as well. – Hamed Oct 6 at 16:03
  • Hi! I've done all that now. It's still racing through every single face so I'm going to try the de bounce and see if it's that. I can't even press a button, it just does it itself haha. Sorry I wasn't sure if I needed to write anything for default! After this I only have one major thing left to do in my code (that you'll probably wind up helping me again im so sorry I'm so new at this). I'll try my best to fix the face speed through – Ijustneedtopassprobs Oct 6 at 16:09
  • Please revert back your original code so others can see what was the problem, you basically cleared the question... – Hamed Oct 6 at 16:34
  • sure thing, I also had another problem now though haha. I tried to debounce but the faces are even faster now. It's going inbetween faces and won't actually stop on one and still wont let me click the button to activate it :/ – Ijustneedtopassprobs Oct 6 at 16:37
  • 1
    Edit: I managed to fix my problem by putting the wires diagonally (ground left pin, 5 right side) if anyone in future has my problem. Back to the victory dance =) – Ijustneedtopassprobs Oct 6 at 17:36
0

You can make things much simpler:

int count = 0;
int button = 5;
int buttonStatus;
unsigned long timer = 0;

void setup() {
  Serial.begin(9600);
  pinMode(button, INPUT_PULLUP);
}

void loop() {
  buttonStatus = digitalRead(button); // read the button status
  if (buttonStatus == LOW) {
    if (millis() - timer > 200) { //de-bouncing the button
      count++; // increase +1 each time button is pressed
      // clear screen function should be here
      timer = millis();
    }
  }
  if (count > 3){ // if count is bigger than 3 start over.
    count = 0;
    // clear screen function should be here
  }
  switch (count){
    case 1: // if count == 1
    // show charecter :)
    break; // break out of this case!
    case 2:
    // show charecter :(
    break;
    case 3:
    // show charecter :D
    break;
    default: // if none of above cases is valid
    count = 0;
    // clear screen function should be here
  }
  Serial.println(count);
  delay(100);
}

schematic

simulate this circuit – Schematic created using CircuitLab

So each time button is pressed count value will increase by 1. you can also add another button to decrease this value...

You might also notice when pushing the button it'll count more than once, you'll also need to De-Bounce the button. tutorial.

Edit: added de-bounce function.

  • Hi again newbie. So with this, I say like Case 1: TFTscreen.stroke(0,0,0); TFTscreen.text(":)"); with all my colour settings? Do I also put in void set up that I want the background to be black? I just want to make sure this is what you meant by your thing you sent me, or do you mean to still have the button in the middle but the wires on the other side? link. My TFT is just blank and wont show up anything before so I wan't to clarify before I start. Thanks again – Ijustneedtopassprobs Oct 6 at 15:36
  • @Ijustneedtopassprobs Only two wire goes to the button (what's the third wire in that picture?), ground and pin 5 in this case. yes define your libraries at top like you did in your original code and the sittings you want in void setup. also read the comments in the code that i posted. – Hamed Oct 6 at 15:51
  • I did remove that one after, sorry I figured that out eventually. It's going through all the faces at the moment. I made up to 5 cases. It's pretty much spamming. I haven't touched the button yet. I'm going to edit my code so you can see my new code to see if I'm extra dumb or not =), once again ty for this – Ijustneedtopassprobs Oct 6 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.