Author Topic: Urjent help needed  (Read 1861 times)

0 Members and 1 Guest are viewing this topic.

Offline radhikaTopic starter

  • Regular Contributor
  • *
  • Posts: 105
  • Country: in
Urjent help needed
« on: March 23, 2020, 08:11:27 am »
Hi, i have used this code. Things I can't resolve is:
1. Button is not pressed.
2. It is not working with button, I am quickly getting last output
Code: [Select]
// Arduino ST7735 1.8" TFT example
 
#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <TimeLib.h>

#define TFT_CS     10
#define TFT_DC     9


Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS,  TFT_DC);
     

float p = 3.1415926;
const int buzzerPin = 8;
const int resetButtonPin = 7;
const int startStopButtonPin = 6;
const int downButtonPin = 5;
const int upButtonPin = 4;

int setupHours = 0;     // How many hours will count down when started
int setupMinutes = 0;   // How many minutes will count down when started
int setupSeconds = 0;   // How many seconds will count down when started
time_t setupTime = 0;

int currentHours = 0;
int currentMinutes = 0;
int currentSeconds = 0;
time_t currentTime = 0;

time_t startTime = 0;
time_t elapsedTime = 0;

int resetButtonState = LOW;
long resetButtonLongPressCounter = 0;
int startStopButtonState = LOW;
int upButtonState = LOW;
int downButtonState = LOW;
int resetButtonPrevState = LOW;
int startStopButtonPrevState = LOW;
int upButtonPrevState = LOW;
int downButtonPrevState = LOW;
bool resetButtonPressed = false;
bool resetButtonLongPressed = false;
bool startStopButtonPressed = false;
bool upButtonPressed = false;
bool downButtonPressed = false;

const int MODE_IDLE = 0;
const int MODE_SETUP = 1;
const int MODE_RUNNING = 2;
const int MODE_RINGING = 3;

int currentMode = MODE_IDLE;    // 0=idle 1=setup 2=running 3=ringing
                                // Power up --> idle
                                // Reset --> idle
                                //  Start/Stop --> start or stop counter
                                //  Up / Down --> NOP
                                // Reset (long press) --> enter setup
                                //   Start/Stop --> data select
                                //   Up --> increase current data value
                                //   Down --> decrease current data value
                                //   Reset --> exit setup (idle)

int dataSelection = 0;  // Currently selected data for edit (setup mode, changes with Start/Stop)
                        // 0=hours (00-99) 1=minutes (00-59) 2=seconds (00-59)


void drawmessage1()
{
   
    tft.drawFastHLine(0, 40,  tft.width(), ST7735_YELLOW);
    tft.setCursor(5,12);
    tft.setTextColor(ST7735_RED);
    tft.println("Set Time using button and press Start");   
}

void setup(void) {
  Serial.begin(9600);
  // Use this initializer if you're using a 1.8" TFT
  tft.initR(INITR_BLACKTAB);   // initialize a ST7735S chip, black tab
 


  // Welcome text
  tft.fillScreen(ST7735_BLACK);
  tft.setTextSize(2);
  tft.setCursor(40, 70);
  tft.setTextColor(ST7735_WHITE);
  tft.setTextSize(1);
  tft.println("Welcome!");
  delay(500);
  pinMode(resetButtonPin, INPUT);
  pinMode(startStopButtonPin, INPUT);
  pinMode(upButtonPin, INPUT);
  pinMode(downButtonPin, INPUT);
  pinMode(buzzerPin, OUTPUT);
  tft.fillScreen(ST7735_BLACK);
  drawmessage1();
 
 
  }
 

void loop() {

  // put your main code here, to run repeatedly:
  startStopButtonPressed = false;
  upButtonPressed = false;
  downButtonPressed = false;

  /*
   * Reset button management
   */
  resetButtonPressed = false;
  resetButtonLongPressed = false;
  resetButtonState = digitalRead(resetButtonPin);
  if(resetButtonState != resetButtonPrevState)
  {
    resetButtonPressed = resetButtonState == HIGH;
    resetButtonPrevState = resetButtonState;
  }
  else  // Long press management...
  {
    if(resetButtonState == HIGH)
    {
      resetButtonLongPressCounter++;
      if(resetButtonLongPressCounter == 100)
      {
        resetButtonPressed = false;
        resetButtonLongPressed = true;
        resetButtonLongPressCounter = 0;
      }
    }
    else
    {
      resetButtonLongPressCounter = 0;
      resetButtonPressed = false;
      resetButtonLongPressed = false;
    }
  }

  /*
   * Start/Stop button management
   */
  startStopButtonPressed = false;
  startStopButtonState = digitalRead(startStopButtonPin);
  if(startStopButtonState != startStopButtonPrevState)
  {
    startStopButtonPressed = startStopButtonState == HIGH;
    startStopButtonPrevState = startStopButtonState;
  }

  /*
   * Down button management
   */
  downButtonPressed = false;
  downButtonState = digitalRead(downButtonPin);
  if(downButtonState != downButtonPrevState)
  {
    downButtonPressed = downButtonState == HIGH;
    downButtonPrevState = downButtonState;
  }

  /*
   * Up button management
   */
  upButtonPressed = false;
  upButtonState = digitalRead(upButtonPin);
  if(upButtonState != upButtonPrevState)
  {
    upButtonPressed = upButtonState == HIGH;
    upButtonPrevState = upButtonState;
  }

  /*
   * Mode management
   */
  switch(currentMode)
  {
    case MODE_IDLE:
      if(resetButtonPressed)
      {
        Reset();
      }
      if(resetButtonLongPressed)
      {
        currentMode = MODE_SETUP;
      }
      if(startStopButtonPressed)
      {
        currentMode = currentMode == MODE_IDLE ? MODE_RUNNING : MODE_IDLE;
        if(currentMode == MODE_RUNNING)
        {
          // STARTING TIMER!
          startTime = now();
        }
      }
      break;

    case MODE_SETUP:
      if(resetButtonPressed)
      {
        // Exit setup mode
        setupTime = setupSeconds + (60 * setupMinutes) + (3600 * setupHours);
        currentHours = setupHours;
        currentMinutes = setupMinutes;
        currentSeconds = setupSeconds;
        dataSelection = 0;
        currentMode = MODE_IDLE;
      }
      if(startStopButtonPressed)
      {
        // Select next data to adjust
        dataSelection++;
        if(dataSelection == 3)
        {
          dataSelection = 0;
        }
      }
      if(downButtonPressed)
      {
        switch(dataSelection)
        {
          case 0: // hours
            setupHours--;
            if(setupHours == -1)
            {
              setupHours = 99;
            }
            break;
          case 1: // minutes
            setupMinutes--;
            if(setupMinutes == -1)
            {
              setupMinutes = 59;
            }
            break;
          case 2: // seconds
            setupSeconds--;
            if(setupSeconds == -1)
            {
              setupSeconds = 59;
            }
            break;
        }
      }
      if(upButtonPressed)
      {
        switch(dataSelection)
        {
          case 0: // hours
            setupHours++;
            if(setupHours == 100)
            {
              setupHours = 0;
            }
            break;
          case 1: // minutes
            setupMinutes++;
            if(setupMinutes == 60)
            {
              setupMinutes = 0;
            }
            break;
          case 2: // seconds
            setupSeconds++;
            if(setupSeconds == 60)
            {
              setupSeconds = 0;
            }
            break;
        }
      }
      break;
   
    case MODE_RUNNING:
      if(startStopButtonPressed)
      {
        currentMode = MODE_IDLE;
      }
      if(resetButtonPressed)
      {
        Reset();
        currentMode = MODE_IDLE;
      }
      break;

    case MODE_RINGING:
      if(resetButtonPressed || startStopButtonPressed || downButtonPressed || upButtonPressed)
      {
        currentMode = MODE_IDLE;
      }
      break;
  }

  /*
   * Time management
   */
  switch(currentMode)
  {
    case MODE_IDLE:
    case MODE_SETUP:
      // NOP
      break;
    case MODE_RUNNING:
      currentTime = setupTime - (now() - startTime);
      if(currentTime <= 0)
      {
        currentMode = MODE_RINGING;
      }
      break;
    case MODE_RINGING:
      analogWrite(buzzerPin, 20);
      delay(20);
      analogWrite(buzzerPin, 0);
      delay(40);
      break;
  }

  /*
   * LCD management
   */
 
  switch(currentMode)
  {
    case MODE_IDLE:
      tft.setCursor(30,50);
      tft.setTextColor(ST7735_RED);
      tft.println("Set Timer");
      tft.setCursor(40,60);
      tft.setTextColor(ST7735_YELLOW);
      tft.println(currentHours);
      tft.println(" ");
      tft.setCursor(60,60);
      tft.setTextColor(ST7735_YELLOW);
      tft.println(currentMinutes);
      tft.println(" ");
      tft.setCursor(80,60);
      tft.setTextColor(ST7735_YELLOW);
      tft.println(currentSeconds);
      tft.println("    ");
      break;
    case MODE_SETUP:
      tft.setCursor(30,20);
      tft.setTextColor(ST7735_YELLOW);
      tft.println("Setup mode: ");
      switch(dataSelection)
      {
        case 0:
        tft.setCursor(30,30);
         tft.setTextColor(ST7735_YELLOW);
          tft.println("HRS ");
          break;
        case 1:
        tft.setCursor(30,30);
      tft.setTextColor(ST7735_YELLOW);
          tft.println("MINS");
          break;
        case 2:
        tft.setCursor(30,30);
      tft.setTextColor(ST7735_YELLOW);
          tft.println("SECS");
          break;
      }
      tft.setCursor(30, 40);
      tft.println(setupHours);
      tft.println(" ");
      tft.println(setupMinutes);
      tft.println(" ");
      tft.println(setupSeconds);
      tft.println("    ");
      break;
    case MODE_RUNNING:
      tft.println("Counting down...");
      tft.setCursor(30, 70);
      if(hour(currentTime) < 10) tft.println("0");
      tft.println(hour(currentTime));
      tft.println(":");
      if(minute(currentTime) < 10) tft.println("0");
      tft.println(minute(currentTime));
      tft.println(":");
      if(second(currentTime) < 10) tft.println("0");
      tft.println(second(currentTime));
      break;
    case MODE_RINGING:
      tft.println("   RING-RING!   ");
      tft.setCursor(30, 70);
      tft.println("                ");
      break;
  }
  delay(10);
}

void Reset()
{
  currentMode = MODE_IDLE;
  currentHours = setupHours;
  currentMinutes = setupMinutes;
  currentSeconds = setupSeconds;
}
 
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 12786
  • Country: nz
Re: Urjent help needed
« Reply #1 on: March 23, 2020, 08:34:13 am »
How do the buttons connect to the arduino?

Which of the 4 buttons has the problem?

You are not enabling any internal pull-ups. You need to have external pullup or down on buttons, do you have any?
(To use internal pullups change pinMode(x, INPUT);  to pinMode(x, INPUT_PULLUP);)

Keep in mind that using pullups inverts the button logic,  0 = button-press,  high = no press
« Last Edit: March 23, 2020, 08:41:44 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline radhikaTopic starter

  • Regular Contributor
  • *
  • Posts: 105
  • Country: in
Re: Urjent help needed
« Reply #2 on: March 23, 2020, 09:32:33 am »
How do the buttons connect to the arduino?

Which of the 4 buttons has the problem?

You are not enabling any internal pull-ups. You need to have external pullup or down on buttons, do you have any?
(To use internal pullups change pinMode(x, INPUT);  to pinMode(x, INPUT_PULLUP);)

Keep in mind that using pullups inverts the button logic,  0 = button-press,  high = no press
Here is the atttached image. Please check
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 12786
  • Country: nz
Re: Urjent help needed
« Reply #3 on: March 23, 2020, 10:41:38 am »
External pull-downs all look ok  :-+

But you are missing a ground connection to the Arduino

« Last Edit: March 23, 2020, 10:47:59 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 10094
  • Country: us
Re: Urjent help needed
« Reply #4 on: March 23, 2020, 04:11:01 pm »
Nobody wants to read through 401 lines of code looking for a switch.  It is far better to delete all code not related to the problem before posting.

 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->