How do I make my text flash using mills?

Status
Not open for further replies.

j_dunavin

Well-known member
My code works as shown, but I want to be able to have my println, flash when button State1 is low and go away when the pin is not low. (flash on for one sec and off for one sec)
I was able to get it to work using delay, but then my other inputs and println's were of course also delayed.
I also tried using mills, but my test would only flash very quickly and then go away.
Here is the basic set up... so far.

Thanks for any help!

PS the PWM is there so i can dim my lcd.

Code:
#include "SPI.h"
#include "ILI9341_t3.h"
#include <font_ArialBold.h>

#define TFT_DC 9
#define TFT_CS 10
#define TFT_RST 8

ILI9341_t3 tft = ILI9341_t3(TFT_CS, TFT_DC, TFT_RST);

const int pwm = A6 ;    //naming pin ‘pwm’ variable out
const int adc = A5 ;   //naming pin analog input side as ‘adc’

int buttonPin1= 14;    //sets pins buttons are connected to
int buttonPin2= 15;
int buttonPin3= 16;
int buttonPin4= 17;
int buttonPin5= 7;
int buttonPin6= 6;
int buttonPin7= 5;
int buttonPin8= 4;

void setup() {

  tft.begin();
  tft.setRotation(1);
  tft.fillScreen(ILI9341_BLACK);

  pinMode(pwm,OUTPUT) ;  //setting pin as output

  pinMode(buttonPin1, INPUT);  //assigns button pins as inputs //currently pulled high
  pinMode(buttonPin2, INPUT);   //currently pulled low
  pinMode(buttonPin3, INPUT);   //currently pulled low
  pinMode(buttonPin4, INPUT);   //currently pulled low
  pinMode(buttonPin5, INPUT);   //currently pulled high
  pinMode(buttonPin6, INPUT);   //currently pulled high
  pinMode(buttonPin7, INPUT);   //currently pulled high
  pinMode(buttonPin8, INPUT);   //currently pulled high
}

void loop(void) {

  int adc  = analogRead(A5) ;    //reading analog voltage and storing it in an integer 
     adc = map(adc, 0, 1023, 10, 250);  
     analogWrite(pwm,adc) ;


  int buttonState1 = digitalRead(buttonPin1);
  int buttonState2 = digitalRead(buttonPin2);
  int buttonState3 = digitalRead(buttonPin3);
  int buttonState4 = digitalRead(buttonPin4);
  int buttonState5 = digitalRead(buttonPin5);
  int buttonState6 = digitalRead(buttonPin6);
 
  if(buttonState1== LOW)     
  {
  tft.setCursor(0, 12);
  tft.setFont(Arial_24_Bold);
  tft.setTextColor(ILI9341_RED); 
  tft.println("HELLO");
  }
  else
  {
  tft.setCursor(0, 12);
  tft.setFont(Arial_24_Bold);
  tft.setTextColor(ILI9341_BLACK); 
  tft.println("HELLO");
  }


heres what it looks like when it flashes really quickly.

Code:
#include "SPI.h"
#include "ILI9341_t3.h"
#include <font_ArialBold.h>

#define TFT_DC 9
#define TFT_CS 10
#define TFT_RST 8

ILI9341_t3 tft = ILI9341_t3(TFT_CS, TFT_DC, TFT_RST);

const int pwm = A6 ;    //naming pin ‘pwm’ variable out
const int adc = A5 ;   //naming pin analog input side as ‘adc’

unsigned long interval=1000; // the time we need to wait
unsigned long previousMillis=0; // millis() returns an unsigned long.
bool buttonState = false; // state variable
 

int buttonPin1= 14;    //sets pins buttons are connected to
int buttonPin2= 15;
int buttonPin3= 16;
int buttonPin4= 17;
int buttonPin5= 7;
int buttonPin6= 6;
int buttonPin7= 5;
int buttonPin8= 4;

void setup() {

  tft.begin();
  tft.setRotation(1);
  tft.fillScreen(ILI9341_BLACK);

  pinMode(pwm,OUTPUT) ;  //setting pin as output

  pinMode(buttonPin1, INPUT);  //assigns button pins as inputs //currently pulled high
  pinMode(buttonPin2, INPUT);   //currently pulled low
  pinMode(buttonPin3, INPUT);   //currently pulled low
  pinMode(buttonPin4, INPUT);   //currently pulled low
  pinMode(buttonPin5, INPUT);   //currently pulled high
  pinMode(buttonPin6, INPUT);   //currently pulled high
  pinMode(buttonPin7, INPUT);   //currently pulled high
  pinMode(buttonPin8, INPUT);   //currently pulled high

}

void loop(void) {

  int adc  = analogRead(A5) ;    //reading analog voltage and storing it in an integer 
     adc = map(adc, 0, 1023, 10, 250);  
     analogWrite(pwm,adc) ;
     

  int buttonState1 = digitalRead(buttonPin1);
  int buttonState2 = digitalRead(buttonPin2);
  int buttonState3 = digitalRead(buttonPin3);
  int buttonState4 = digitalRead(buttonPin4);
  int buttonState5 = digitalRead(buttonPin5);
  int buttonState6 = digitalRead(buttonPin6);

 unsigned long currentMillis = millis(); // grab current time
 
  if ((buttonState1== LOW) && (unsigned long)(currentMillis - previousMillis) >= interval) 
  {
  tft.setCursor(0, 12);
  tft.setFont(Arial_24_Bold);
  tft.setTextColor(ILI9341_RED); 
  tft.println("HELLO");
  buttonState1 = !buttonState1; // "toggles" the state
  previousMillis = millis();
  }
  else
  {
  tft.setCursor(0, 12);
  tft.setFont(Arial_24_Bold);
  tft.setTextColor(ILI9341_BLACK); 
  tft.println("HELLO");
  }
 
Maybe a better way to ask is, what method would work here to make my text blink on and off when my pin goes low?
Delay works, but causes problems down stream, so that's out.
Suggestions?
 
Looks like your not doing anything with the global variable buttonState. Perhaps this was meant to hold the blinking interval state and buttonState1 is an additional test?
 
no... this, originally, was just meant to read the buttonPin, IF low then print red else print in black.
If i understand your question correctly.....I'm new!
 
after a little research i believe that the very fast blink MAY be ok for the given code, and that i need to actually tell it how long i want it ON and OFF ?
 
Maybe you should look at the code in the Arduino menu File/Examples/02.Digital/BlinkWithoutDelay and see if that helps. (Maybe you have since some of the variable names look similar). If you can combine that simple example with an input, to work like you want it will be easier to get the more complex code going.

I would avoid using a single variable ( buttonState1 ) as an input, part of an if statement and then inverting as a toggle. These are separate tasks. Remember the toggle state has to be remembered the next time through loop() so a static or global variable is needed.
 
Status
Not open for further replies.
Back
Top