Go Down

Topic: Menu using 2 Buttons (Read 522 times) previous topic - next topic

Crebsyn

I am trying to setup a menu but am having some trouble. If i push the Index button I can bring up the menu. I then want to be able to push either the index or select button to do whatever task. Please have a look at the code and let me know what I should do.
Thanks
Code: [Select]
void menu (){
  delay (500);
  tft.fillScreen(0x62EC);
  tft.fillCircle(25, 25, 10, 0x57E0);
  tft.fillCircle(25, 75, 10, 0x57E0);
  tft.fillCircle(25, 125, 10, 0x57E0);
  tft.setCursor(50, 18);
  tft.setTextColor(BLACK);  tft.setTextSize(2);
  tft.println("RIDE SETTINGS");
  tft.setCursor(50, 68);
  tft.println("testing A");
  tft.setCursor(50, 118);
  tft.println("testing B");
  while ((digitalRead(Index) == HIGH)|| (digitalRead(Select) == HIGH) );
    if (digitalRead(Index) == HIGH) {
      tft.fillCircle(25, 25, 10, RED);
    }
    if (digitalRead(Select) == HIGH) {
      tft.fillCircle(25, 75, 10, RED);
    }
    if (digitalRead(Select) == LOW) {
      tft.fillCircle(25, 125, 10, RED);
    }
  delay (5000);
  Background();
  return ;
}

PaulS

Quote
let me know what I should do.

Two possibilities. Take your snippet over to snippets-r-us.com and see of anyone there can help you. Or, post all of your code.

Quote
I then want to be able to push either the index or select button to do whatever task.

So, what's the problem?

Crebsyn

Thre Problem is I can't seem to make the "if" commands turn the circles red when either button is pushed in the menu code.
Code: [Select]
#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include <SD.h>

#define SD_CS 5

#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

Adafruit_TFTLCD tft;
int8_t saved_spimode;

void disableSPI(void) {
  saved_spimode = SPCR;
  SPCR = 0;
}

void enableSPI(void) {
  SPCR = saved_spimode;
}

const int buttonPin = 2;
const int Index = A4;
const int Select = A5;
//int sensorPin = A4;
float Matchspeed = 0.0;
float rim = 2.170;
int cycles = 0;
unsigned long revtimer;
unsigned long revtimer2;
unsigned long serialTimer;
float Speed = 0;
float distance = 0;

unsigned long AvgTime2 = 0;
unsigned long AvgTime = 0;
unsigned long TempTime = 0;
int Avgspeed =0;
int cyclecount = 0;
float speeddif = 0;
float timedif = 0;
byte A = 10;
int B = 0;
File myFile1;
File myFile2;
File myFile3;
File myFile4;
int digit;
float totalavg = 0;
float timeavg = 0;

void setup()
{
  Serial.begin (9600);
  pinMode (buttonPin, INPUT);
  pinMode (Index, INPUT);
  pinMode (Select, INPUT);
  tft.reset();
  uint16_t identifier = tft.readID();
  tft.begin(identifier);

  Serial.print("Initializing SD card...");
  if (!SD.begin(SD_CS)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  disableSPI();
  Background();
  serialTimer =millis();
}


void loop ()
{
 
  if (digitalRead(Index) == HIGH){
  menu ();
  }
  if (digitalRead(buttonPin) == HIGH){
   
  cycles++;
  while (digitalRead(buttonPin) == HIGH);
  //while (digitalRead(buttonPin) == LOW);
  delay (10);
  Currentspeed ();
  TimeCompareText();
  sendstats();
  Speedtext();
 
  for (B=1; B<26000; B+=40){
  //if (cycles == B+39)
  //AvgspeedW(); 
  //if (cycles == B+19)
  //TimecompareW();
  if (cycles == B)
  AvgspeedR();
  if (cycles == B+20)
  TimeR();
  //if (cycles == B+21)
  //TimeCompareText();
  }
  } 
}


unsigned long Background() {
  tft.fillScreen(WHITE);
  tft.fillRect (0, 60, 240, 5, BLACK);
  tft.fillRect (0, 260, 240, 5, BLACK);
  //tft.setCursor(160, 190);
  //tft.setTextColor(BLACK);  tft.setTextSize(2);
  //tft.println("km/hr");
  //tft.setCursor(160, 110);
  //tft.setTextColor(BLACK);  tft.setTextSize(2);
  //tft.println("km/hr");
}

void Currentspeed () {
   serialTimer = millis();
   revtimer = (serialTimer);
   //Serial.print(revtimer);
   //Serial.println (" revtimer");
   Speed = ((rim * 3600)/(revtimer - revtimer2));
   //Serial.print(Speed);
   //Serial.println (" Speed");
   revtimer2 = (serialTimer);
   //Serial.print(revtimer2);
   //Serial.println (" revtimer2");
}

void AvgspeedR() {
  enableSPI();
  char AvgSR[27];
  sprintf(AvgSR, "/Read/Speed/speed%03d.txt", B+39);
  totalavg = 0;
  File myFile1 = SD.open(AvgSR);
    while (true)
        {
        if (myFile1.available() == 0)
            break;
        digit = myFile1.read();
        if (digit == '\n' || digit == '\r')
            break;
        totalavg = totalavg * 10 + (digit - '0');
        }
  Serial.println (totalavg);
  myFile1.close();
  disableSPI();
}

void TimeR() {
  enableSPI();
  char TimeSR[26];
  sprintf(TimeSR, "/Read/Times/time%03d.txt", B+19);
  timeavg = 0;
  File myFile4 = SD.open(TimeSR);
    while (true)
        {
        if (myFile4.available() == 0)
            break;
        digit = myFile4.read();
        if (digit == '\n' || digit == '\r')
            break;
        timeavg = timeavg * 10 + (digit - '0');
        }
  Serial.println (timeavg);
  myFile4.close();
  disableSPI();
}


void AvgspeedW() {
  enableSPI();
  char fileName[28];
  sprintf(fileName, "/Write/Speed/speed%03d.txt", B+39);
  myFile2 = SD.open(fileName, FILE_WRITE);
  if (myFile2) {
  AvgTime2 = (serialTimer);
  Avgspeed = (1800000*rim/(AvgTime2-TempTime));
  TempTime = (serialTimer);
  myFile2.println(Avgspeed);
  myFile2.close();
  Serial.println("done.");
  }
  disableSPI();
}


void TimecompareW() {
  enableSPI();
  char Times[27];
  sprintf(Times, "/Write/Times/time%03d.txt", B+19);
  myFile3 = SD.open(Times, FILE_WRITE);
  if (myFile3) {
  myFile3.println(serialTimer/100);
  myFile3.close();
  Serial.println("done.");
  }
  disableSPI();
}
 
 

  void sendstats() {
// Serial.print (Speed);
// Serial.println (" km/hr");
// serialTimer = millis();
// Serial.print (serialTimer/1000);
// Serial.println (" s Ride Time");
// Serial.println (distance);
  Serial.println (cycles);
// Serial.println (serialTimer);
  Serial.println (" ");
}

unsigned long Speedtext() {
  if (Speed < 10 || cycles == 1)
  {A = (77);
  }
  else { A=(47);
  }
  tft.setTextColor(BLACK);
  if (Speed < ((totalavg/10)-2))
  { tft.setTextColor(RED);
  tft.fillTriangle (120, 240, 50, 200, 190, 200, (RED));
  tft.fillRect (95, 180, 50, 20, (RED)); // x,y , x distance, y distance
  tft.fillRect (50, 80, 140, 60, WHITE);
   }
  else if (Speed > ((totalavg/10)+2))
  { tft.setTextColor(BLUE);
  tft.fillTriangle (120, 80, 50, 120, 190, 120, (BLUE));
  tft.fillRect (95, 120, 50, 20, (BLUE)); // x,y , x distance, y distance
  tft.fillTriangle (115, 90, 112, 109, 121, 107, (YELLOW));
  tft.fillTriangle (115, 90, 127, 87, 121, 107, (YELLOW));
  tft.fillTriangle (122, 103, 134, 101, 110, 136, (YELLOW));
  tft.fillRect (50, 180, 140, 60, WHITE);
  }
  else {
    tft.fillRect (50, 180, 140, 60, WHITE);
    tft.fillRect (50, 80, 140, 60, WHITE);
  }
  tft.setCursor(A, 142);
  tft.setTextSize(5);
  tft.fillRect (47, 142, 146, 40, WHITE);
  tft.println(Speed);
}


unsigned long TimeCompareText() {
  serialTimer = millis();
  timedif = (((timeavg*100)-(serialTimer))/1000);
  if (timedif < 10 && timedif > 0)
  {A = (151);
  tft.setTextColor(BLUE);
  }
  else if (timedif > 10)
  { A=(133);
  tft.setTextColor(BLUE);
  }
  else if (timedif <= (0-10) && timedif > -100)
  { A=(115);
  tft.setTextColor(RED);
  }
  else if (timedif < (0-100))
  { A=(97);
  tft.setTextColor(RED);
  }
  else if (timedif > 100)
  { A=(115);
  tft.setTextColor(BLUE);
  }
  else {
    A=(133);
    tft.setTextColor(RED);
  }
  if (timedif > 0) {
  tft.setCursor(10, 25);
  tft.setTextColor(BLUE);  tft.setTextSize(2);
  tft.fillRect (10, 25, 100, 30, WHITE);
  tft.println("AHEAD");
  }
  else {
  tft.setCursor(10, 25);
  tft.setTextColor(RED);  tft.setTextSize(2);
  tft.fillRect (10, 25, 100, 30, WHITE);
  tft.println("BEHIND");
  }
  tft.setCursor(A, 20);
  tft.setTextSize(3);
  tft.fillRect (105, 18, 120, 30, WHITE);
  tft.println(timedif); 
}

void menu (){
  delay (500);
  tft.fillScreen(0x62EC);
  tft.fillCircle(25, 25, 10, 0x57E0);
  tft.fillCircle(25, 75, 10, 0x57E0);
  tft.fillCircle(25, 125, 10, 0x57E0);
  //if (cycles==4) {
  tft.setCursor(50, 18);
  tft.setTextColor(BLACK);  tft.setTextSize(2);
  tft.println("RIDE SETTINGS");
  tft.setCursor(50, 68);
  tft.println("testing A");
  tft.setCursor(50, 118);
  tft.println("testing B");
  while ((digitalRead(Index) == HIGH)|| (digitalRead(Select) == HIGH) );
    if (digitalRead(Index) == HIGH) {
      tft.fillCircle(25, 25, 10, RED);
    }
    if (digitalRead(Select) == HIGH) {
      tft.fillCircle(25, 75, 10, RED);
    }
   
  delay (5000);
  Background();
  return ;
}

PaulS

Code: [Select]
  while ((digitalRead(Index) == HIGH)|| (digitalRead(Select) == HIGH) );
    if (digitalRead(Index) == HIGH) {
      tft.fillCircle(25, 25, 10, RED);
    }
    if (digitalRead(Select) == HIGH) {
      tft.fillCircle(25, 75, 10, RED);
    }


Does HIGH mean pressed or released? Apparently, it means pressed, because HIGH triggers getting into this function. So, the while loop then does nothing as long as either switch is pressed. The code that follows then only gets executed when both switches are released. You would have to release the switch(es) to get the while loop to end, and then be inhumanly fast to get either switch pressed again before the two digitalRead() statements finish.

Crebsyn

So how should this code look to do what i want it to.

Grumpy_Mike

This line of code:-
Code: [Select]
while ((digitalRead(Index) == HIGH)|| (digitalRead(Select) == HIGH) );
Has a ; on the end. Which means it will do nothing while either index or select is high.
Is that what you want it to do?

Quote
So how should this code look to do what i want it to.

It would not have a ; on the end of the first if but it could have an opening brace { that encloses the next two if statements. Mind you then you will repeatedly be calling the fillCircle routines while either button is held down which is not very efficient.

Crebsyn

Thanks for the help I changed the code so its working now.
Code: [Select]
while ((digitalRead(Index) == LOW)&& (digitalRead(Select) == LOW) );
    if (digitalRead(Index) == HIGH) {
      tft.fillCircle(25, 25, 10, RED);
    }
    if (digitalRead(Select) == HIGH) {
      tft.fillCircle(25, 75, 10, RED);
    }

Go Up