Combine calculator with remote control infrared detector failed using Mega 2560

Hi All,

I try to combine calculator and infrared detector code using Mega 2560 board. The combination codes compiled successfuly without any error. Calculator works but infrared detector not working.

When I touch & hold the calculator screen and then press the remote control, the infrared detector works. That's really weird. Could someone please review the codes and provide some help? Thanks in advance.

/*______Import Libraries_______*/

#include <SPFD5408_Adafruit_GFX.h>    // Core graphics library

#include <SPFD5408_Adafruit_TFTLCD.h> // Hardware-specific library

#include <SPFD5408_TouchScreen.h>

#include <IRremote.h>

/*______End of Libraries_______*/


/*______Define LCD pins (I have asigned the default values)_______*/

#define XM A1  // must be an analog pin, use "An" notation!

#define YP A2  // must be an analog pin, use "An" notation!

#define XP 7   // can be a digital pin

#define YM 6   // can be a digital pin

#define LCD_CS A3

#define LCD_CD A2

#define LCD_WR A1

#define LCD_RD A0

#define LCD_RESET A4

/*_______End of defanitions______*/


/*______Assign names to colors and pressure_______*/

#define WHITE   0x0000 //Black->White

#define YELLOW    0x001F //Blue->Yellow

#define CYAN     0xF800 //Red->Cyan

#define PINK   0x07E0 //Green-> Pink

#define RED    0x07FF //Cyan -> Red

#define GREEN 0xF81F //Pink -> Green 

#define BLUE  0xFFE0 //Yellow->Blue

#define BLACK   0xFFFF //White-> Black

#define MINPRESSURE 10

#define MAXPRESSURE 1000

/*_______Assigned______*/


/*____Calibrate TFT LCD_____*/

#define TS_MINX 125

#define TS_MINY 85

#define TS_MAXX 965

#define TS_MAXY 905

/*______End of Calibration______*/


TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300); //300 is the sensitivity

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET); //Start communication with LCD


String symbol[4][4] = {

  { "7", "8", "9", "/" },

  { "4", "5", "6", "x" },

  { "1", "2", "3", "-" },

  { "C", "0", "=", "+" }

};

 int X,Y;

 long Num1,Num2,Number;

 char action;

 boolean result = false;

int RECV_PIN = 22;
IRrecv irrecv(RECV_PIN);
decode_results results; // decode_results type is defined in IRremote.h

 


void setup() {   

  pinMode(49, OUTPUT); // Red LED
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver

  Serial.begin(9600); //Use serial monitor for debugging

  tft.reset(); //Always reset at start

  tft.begin(0x9341); // My LCD uses LIL9341 Interface driver IC

  tft.setRotation(2); // I just roated so that the power jack faces up - optional

  tft.fillScreen(WHITE);


  IntroScreen();

  

  draw_BoxNButtons(); 

}


void loop() {  

TSPoint p = waitTouch();

X = p.y; Y = p.x;

//  Serial.print(X); Serial.print(','); Serial.println(Y);// + " " + Y);


DetectButtons();


if (result==true)

CalculateResult();


DisplayResult();   


  delay(300);

if (irrecv.decode(&results)) {
  Serial.println(results.value, HEX); // display the result
  digitalWrite(49, HIGH);   
  delay(500);              
  digitalWrite(49, LOW);   
  delay(500);
  irrecv.resume(); // Get the next value
  }
  delay(100); // delay to prevent read errors 

}


TSPoint waitTouch() {

  TSPoint p;

  do {

    p = ts.getPoint(); 

    pinMode(XM, OUTPUT);

    pinMode(YP, OUTPUT);

  } while((p.z < MINPRESSURE )|| (p.z > MAXPRESSURE));

  p.x = map(p.x, TS_MINX, TS_MAXX, 0, 320);

  p.y = map(p.y, TS_MINY, TS_MAXY, 0, 240);;

  return p;

}


void DetectButtons()

{

  

  if (X<60 && X>0) //Detecting Buttons on Column 1

  {

    if (Y>0 && Y<50) //If cancel Button is pressed

    {Serial.println ("Button Cancel"); Number=Num1=Num2=0; result=false;}

    

     if (Y>50 && Y<110) //If Button 1 is pressed

    {Serial.println ("Button 1"); 

    if (Number==0)

    Number=1;

    else

    Number = (Number*10) + 1; //Pressed twice

    }

    

     if (Y>110 && Y<160) //If Button 4 is pressed

    {Serial.println ("Button 4"); 

    if (Number==0)

    Number=4;

    else

    Number = (Number*10) + 4; //Pressed twice

    }

    

     if (Y>160 && Y<220) //If Button 7 is pressed

    {Serial.println ("Button 7");

    if (Number==0)

    Number=7;

    else

    Number = (Number*10) + 7; //Pressed twice

    } 

  }

    if (X<120 && X>60) //Detecting Buttons on Column 2

  {

    if (Y>0 && Y<50)

    {Serial.println ("Button 0"); //Button 0 is Pressed

    if (Number==0)

    Number=0;

    else

    Number = (Number*10) + 0; //Pressed twice

    }

    

     if (Y>50 && Y<110)

    {Serial.println ("Button 2"); 

     if (Number==0)

    Number=2;

    else

    Number = (Number*10) + 2; //Pressed twice

    }

    

     if (Y>110 && Y<160)

    {Serial.println ("Button 5"); 

     if (Number==0)

    Number=5;

    else

    Number = (Number*10) + 5; //Pressed twic

    }

    

     if (Y>160 && Y<220)

    {Serial.println ("Button 8"); 

     if (Number==0)

    Number=8;

    else

    Number = (Number*10) + 8; //Pressed twic

    }   

  }


    if (X<180 && X>120) //Detecting Buttons on Column 3

  {

    if (Y>0 && Y<50)

    {Serial.println ("Button Equal"); 

    Num2=Number;

    result = true;

    }

    

     if (Y>50 && Y<110)

    {Serial.println ("Button 3"); 

     if (Number==0)

    Number=3;

    else

    Number = (Number*10) + 3; //Pressed twice

    }

    

     if (Y>110 && Y<160)

    {Serial.println ("Button 6"); 

    if (Number==0)

    Number=6;

    else

    Number = (Number*10) + 6; //Pressed twice

    }

    

     if (Y>160 && Y<220)

    {Serial.println ("Button 9");

    if (Number==0)

    Number=9;

    else

    Number = (Number*10) + 9; //Pressed twice

    }   

  }


      if (X<240 && X>180) //Detecting Buttons on Column 3

  {

    Num1 = Number;    

    Number =0;

    tft.setCursor(200, 20);

    tft.setTextColor(RED);

    if (Y>0 && Y<50)

    {Serial.println ("Addition"); action = 1; tft.println('+');}

     if (Y>50 && Y<110)

    {Serial.println ("Subtraction"); action = 2; tft.println('-');}

     if (Y>110 && Y<160)

    {Serial.println ("Multiplication"); action = 3; tft.println('x');}

     if (Y>160 && Y<220)

    {Serial.println ("Devesion"); action = 4; tft.println('/');}  


    delay(300);

  }  

}


void CalculateResult()

{

  if (action==1)

    Number = Num1+Num2;


  if (action==2)

    Number = Num1-Num2;


  if (action==3)

    Number = Num1*Num2;


  if (action==4)

    Number = Num1/Num2; 

}


void DisplayResult()

{

    tft.fillRect(0, 0, 240, 80, CYAN);  //clear result box

    tft.setCursor(10, 20);

    tft.setTextSize(4);

    tft.setTextColor(BLACK);

    tft.println(Number); //update new value

}


void IntroScreen()

{

  tft.setCursor (55, 120);

  tft.setTextSize (3);

  tft.setTextColor(RED);

  tft.println("ARDUINO");

  tft.setCursor (30, 160);

  tft.println("CALCULATOR");

  tft.setCursor (30, 220);

  tft.setTextSize (2);

  tft.setTextColor(BLUE);

  tft.println("  by rayXware");

  delay(1800);

}


void draw_BoxNButtons()

{

  //Draw the Result Box

  tft.fillRect(0, 0, 240, 80, CYAN);


 //Draw First Column

  tft.fillRect  (0,260,60,60,RED);

  tft.fillRect  (0,200,60,60,BLACK);

  tft.fillRect  (0,140,60,60,BLACK);

  tft.fillRect  (0,80,60,60,BLACK);


 //Draw Third Column  

  tft.fillRect  (120,260,60,60,GREEN);

  tft.fillRect  (120,200,60,60,BLACK);

  tft.fillRect  (120,140,60,60,BLACK);

  tft.fillRect  (120,80,60,60,BLACK);


  //Draw Secound & Fourth Column  

  for (int b=260; b>=80; b-=60)

 { tft.fillRect  (180,b,60,60,BLUE); 

   tft.fillRect  (60,b,60,60,BLACK);}


  //Draw Horizontal Lines

  for (int h=80; h<=320; h+=60)

  tft.drawFastHLine(0, h, 240, WHITE);


  //Draw Vertical Lines

  for (int v=0; v<=240; v+=60)

  tft.drawFastVLine(v, 80, 240, WHITE);


  //Display keypad lables 

  for (int j=0;j<4;j++) {

    for (int i=0;i<4;i++) {

      tft.setCursor(22 + (60*i), 100 + (60*j));

      tft.setTextSize(3);

      tft.setTextColor(WHITE);

      tft.println(symbol[j][i]);

    }

  }

}

Welcome to the forum.

The code only does what you wrote. There is nothing weird at all!

TSPoint p = waitTouch();

The name says it all… wait until she touches the screen. Nothing else happens. It waits.

You have to rewrite waitTouch(). Let it return even if there is no touch. You'll need a way to communicate that although the call to the touch screen handler has returned, it has done so empty handed, so to speak.

The do/while loop is what causes it to wait. Get a point and check the pressure, return if the pressure indicates no one is touching.

Perhaps coordinates that are impossible, that is to say off the physical screen. Out of bounds impossible touches could inform the next step in the loop() function.

Not too hard. Give it a try or wait for those who would not be able to resist writing it for you. :expressionless:

a7

Hello, alto777,

I'm still new to Arduino. I just bought the UNO R3 and Mega 2560 for learning. These codes I found from internet and still studying them.

Thanks for pointing out the problem. I will study the TSPoint p and see what I can do. Thanks.

If you are also new to programming, I would suggest working through the basic examples that are on offer in the IDE.

The code you found and are "repurposing", at a glance, is quite awful and would not be the best way to learn. Much.

If you are old to programming, so to speak, but have not done anything at this very low level, you'll have to keep in mind that things are a bit different. The processor is wimpy compared to your desktop machine, and there are strategies and well worn ideas for working with the outside world (LEDs and buttons and motors and stuff).

There is great and constant debate whether someone is better off learning regular programming and getting a grip on logic and design of algorithms before tossing themselves into the deeper end where hardware issues can sometimes make the software part look trivial.

Have fun always!

a7"

Agreed and thanks for your advice.

Fully agree with @alto777 's posts ...

If you still want to go further with your hardware it might be a way to test the single functionalities of IR reception and touchscreen usage separately.

The way you are using the IR library is deprecated. You may like to have a glance at this very simple demonstration on Wokwi which I adapted to the MEGA and Serial output. It should work (more or less immediately) with you MEGA ...

https://wokwi.com/projects/370964607956755457

/*
   This sketch is based on 
   
   https://wokwi.com/projects/298934082074575369

   * Removed LCD display
   * Changed to Serial output

   Reason:

   Very simple demonstration of the usage of IRremote.h 

   ec2021 
   2023-07-22


*/

#include <IRremote.h>
#define PIN_RECEIVER 22   // Signal Pin of IR receiver
IRrecv receiver(PIN_RECEIVER);

void setup()
{
  Serial.begin(115200);
  Serial.println("Press a button on the IR Remote!");
  receiver.enableIRIn(); // Start the receiver
}

void loop()
{
  // Checks received an IR signal
  if (receiver.decode()) {
    translateIR();
    receiver.resume();  // Receive the next value
  }
}

void printResults(char* text)
{
  Serial.print("Button pressed:");
  Serial.println(text);
  Serial.print("Code: ");
  Serial.println(receiver.decodedIRData.command);
}

void translateIR()
{
  // Takes command based on IR code received
  switch (receiver.decodedIRData.command) {
    case 162:
      printResults("POWER");
      break;
    case 226:
      printResults("MENU");
      break;
    case 34:
      printResults("TEST");
      break;
    case 2:
      printResults("PLUS");
      break;
    case 194:
      printResults("BACK");
      break;
    case 224:
      printResults("PREV.");
      break;
    case 168:
      printResults("PLAY");
      break;
    case 144:
      printResults("NEXT");
      break;
    case 104:
      printResults("num: 0");
      break;
    case 152:
      printResults("MINUS");
      break;
    case 176:
      printResults("key: C");
      break;
    case 48:
      printResults("num: 1");
      break;
    case 24:
      printResults("num: 2");
      break;
    case 122:
      printResults("num: 3");
      break;
    case 16:
      printResults("num: 4");
      break;
    case 56:
      printResults("num: 5");
      break;
    case 90:
      printResults("num: 6");
      break;
    case 66:
      printResults("num: 7");
      break;
    case 74:
      printResults("num: 8");
      break;
    case 82:
      printResults("num: 9");
      break;
    default:
      Serial.print(receiver.decodedIRData.command);
      Serial.println(" other button");
  }
}

You might change this code so that the calculator works with Serial using the remote control as input device ...

It would be possible to integrate a touch display as well in Wokwi, unfortunately with some significant differences to your hardware that would not allow to use the code 1:1 with your hardware ...

P.S.: The calculator parts in your sketch is not a good example to learn from:

a. Very poorly formated
b. Missing curly brackets in if clauses which makes it hard to keep the overview and to avoid mistakes
c. Button decoding more or less unstructured, hard to debug ...

Hello ec2021,

Thank you for providing the example codes. I will study and test it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.