Displaying data

Hello everyone, this is my first post :slight_smile:

And here is my problem:

I am building a car gear position indicator, and would like to display the gear on a TFT display.
I would like to make it work so that the gear doesn't constantly "flash" or is written every time the program goes thru the loop, but that the indicated gear stays the same until changed.

In the attachment you can see the setup, when a button is pushed, it should indicate the corresponding gear, and if no buttons are pushed, it should indicate neutral ( N ).

Here is the code :

int first = 0;
int second = 0;
int third = 0;
int fourth = 0;
int fifth = 0;
int reverse = 0;

#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9340.h"

#if defined(__SAM3X8E__)
    #undef __FlashStringHelper::F(string_literal)
    #define F(string_literal) string_literal
#endif

// These are the pins used for the UNO

#define _sclk 13
#define _miso 12
#define _mosi 11
#define _cs 10
#define _dc 9
#define _rst 8


//Adafruit_ILI9340 tft = Adafruit_ILI9340(_cs, _dc, _mosi, _sclk, _rst, _miso);

Adafruit_ILI9340 tft = Adafruit_ILI9340(_cs, _dc, _rst);


void setup() {
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  pinMode(6, INPUT);
  pinMode(7, INPUT);
  
  Serial.begin(9600);
  
   
    tft.begin();

}

void loop() {

 tft.fillScreen(ILI9340_BLACK);
  tft.setRotation(1);
  tft.setCursor(0, 0);
  tft.setTextColor(ILI9340_GREEN);
  tft.setTextSize(14);
  
  first = digitalRead(2);
  second = digitalRead(3);
  third = digitalRead(4);
  fourth = digitalRead(5);
  fifth = digitalRead(6);
  reverse = digitalRead(7);
 /////////////////////////////////////////////////////////////
  do 
   { tft.println("1st");
   
   } while (first == HIGH);

 
  //////////////////////////////////////////////////////////
  
  if (second == HIGH)

  {
    tft.println("2nd");
  }

  //////////////////////////////////////////////////////////
  
 else if (third == HIGH)

  {
    tft.println("3rd");
  }

  //////////////////////////////////////////////////////////
  
  else if (fourth == HIGH)

  {
    tft.println("4th");
  }

  //////////////////////////////////////////////////////////
  
 else if (fifth == HIGH)

  {
    tft.println("5th");
  }

  
  //////////////////////////////////////////////////////////
  
 else if (reverse == HIGH)

  {
    tft.println("R");
  }
  
  //if (first== LOW,second== LOW,third== LOW,fourth== LOW,fifth== LOW,reverse == LOW) 
else 
  {
    tft.println("N");
  }
  //////////////////////////////////////////////////////////
    
//delay();

}

Did I approach the problem in the right way ? How to keep the gear from "flashing" until there is a change in gear ?

Any suggestrion, comments, ideas are welcome!

Thanks

You will have a difficult time unless you separate the inputs from the actions. What I mean is, the gears are exclusive (can not be in 3rd and reverse at the same time!), true for all the states. But you handle them all independently with separate if clauses for each one. That won't do if you want to test for a change, because you will have to repeat it so many times. It can be made to work, but it's messy. What you need to do is have the program first decide which gear you're in... distill the inputs down to a single variable, say "gear". If gear = 3, that's third gear. Maybe reverse can be -1. Then it's easy to test for a change and decide whether to update the display based on that. It's also easier to structure the test conditions.

Okay, I got this far, I just can't keep the indicated gear printed on the display..... It shows the correct gear and dissapears.

Thanks

int first;
int second;
int third;
int fourth;
int fifth;
int reverse;
int gear = 0;
int last_gear = 0;

#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9340.h"

#if defined(__SAM3X8E__)
#undef __FlashStringHelper::F(string_literal)
#define F(string_literal) string_literal
#endif

// These are the pins used for the UNO

#define _sclk 13
#define _miso 12
#define _mosi 11
#define _cs 10
#define _dc 9
#define _rst 8


//Adafruit_ILI9340 tft = Adafruit_ILI9340(_cs, _dc, _mosi, _sclk, _rst, _miso);

Adafruit_ILI9340 tft = Adafruit_ILI9340(_cs, _dc, _rst);


void setup() {
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  pinMode(6, INPUT);
  pinMode(7, INPUT);

  Serial.begin(9600);


  tft.begin();

}

void loop() {

  tft.fillScreen(ILI9340_BLACK);
  tft.setRotation(1);
  tft.setCursor(0, 0);
  tft.setTextColor(ILI9340_GREEN);
  tft.setTextSize(14);

  first = digitalRead(2);
  second = digitalRead(3);
  third = digitalRead(4);
  fourth = digitalRead(5);
  fifth = digitalRead(6);
  reverse = digitalRead(7);
  /////////////////////////////////////////////////////////////
  if (first == HIGH)

  {
    gear = 1 ;
  }

  //////////////////////////////////////////////////////////

  else if (second == HIGH)

  {
    gear = 2 ;
  }

  //////////////////////////////////////////////////////////

  else if (third == HIGH)

  {
    gear = 3;
  }

  //////////////////////////////////////////////////////////

  else if (fourth == HIGH)

  {
    gear = 4;
  }

  //////////////////////////////////////////////////////////

  else if (fifth == HIGH)

  {
    gear = 5;
  }


  //////////////////////////////////////////////////////////

  else if (reverse == HIGH)

  {
    gear = 6;
  }

  /////////////////////////////////////////////////////////////////
  else

  {
    gear = 7;
  }
  /////////////////////////////////////////////////////////

  

  

  if (last_gear != gear)

  { tft.write(gear);

  }
 last_gear = gear;
 
 Serial.println(gear);
 Serial.println(last_gear);
}

What happens if you change this:

  if (last_gear != gear)

  { tft.write(gear);

  }
 last_gear = gear;

to:

  if (last_gear != gear)

  { 
     tft.write(gear);
     last_gear = gear;
  }

I'm not familiar with the library, but wouldn't you want to do the tft.write() every loop?

tft.write(gear);

if (last_gear != gear)
{
    last_gear = gear;
}

neither solve the initial problem, the indicated gear " flashes" or repeats itself every loop, I want it to stay there until replaced by another

Something else to try. Move this code block:

  tft.fillScreen(ILI9340_BLACK);
  tft.setRotation(1);
  tft.setCursor(0, 0);
  tft.setTextColor(ILI9340_GREEN);
  tft.setTextSize(14);

out of loop() and into setup() and then continue to use:

  if (last_gear != gear)

  {
     tft.setCursor(0, 0);
     tft.write(gear);
     last_gear = gear;
  }

Does that have any impact?

Yeah, it does! But now since there is no tft.fillScreen(ILI9340_BLACK); the number stays printed on the screen and gets overwritten when i push another button :grin: , thanks everyone for your help ! I'm gonna keep trying

if (last_gear != gear)
{ 
tft.fillScreen(ILI9340_BLACK);
tft.write(gear);
}

Thanks man !!! That did the trick !! :smiley: ;D