# MEGA 2560 Code problem

**URL:** <https://forum.arduino.cc/t/mega-2560-code-problem/1333529>\
**Category:** General Guidance\
**Created:** [December 18, 2024, 4:18pm UTC](https://forum.arduino.cc/t/mega-2560-code-problem/1333529 "2024-12-18T16:18:27Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ayhancan65](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ayhancan65/32/976017_2.png) [@ayhancan65](https://forum.arduino.cc/u/ayhancan65)\
**Post date:** [December 18, 2024, 4:18pm UTC](https://forum.arduino.cc/t/mega-2560-code-problem/1333529/1 "2024-12-18T16:18:27Z")

</div>

Hello everyone My problem is I switched from Arduino Uno to MEGA2560 There is a screenshot on Arduino Uno I cannot take a screenshot on Mega2560 I wonder what kind of changes I need to make in my codes I need your help Thank you

```arduino
#include <EEPROM.h>
#include <Adafruit_GFX.h> // include Adafruit graphics library
#include <Adafruit_ILI9341.h> // include Adafruit ILI9341 TFT library
#define TFT_CS 8 // TFT CS pin is connected to arduino pin 8
#define TFT_RST 9 // TFT RST pin is connected to arduino pin 9
#define TFT_DC 10 // TFT DC pin is connected to arduino pin 10
// initialize ILI9341 TFT library
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
 
int tempPin = A1; // make variables// thermistor is at A0
int led =5; // led is at pin
float temp; // make a variable called temp
int settemp; // make a variable called temp
int swtu = 7; // switch up is at pin 7
int swtd = 6; // switch down is at pin 6
byte jos = 10;
 
void setup(void){

 
  
   pinMode (led,5); // make led or pin13 an output
  tft.begin();
  Serial.begin(9600);
  tft.setRotation(3);
  tft.fillScreen(ILI9341_WHITE);
  tft.setTextColor(ILI9341_BLACK, ILI9341_WHITE); 
  
  
  

}
void loop(void) {
  tft.setTextSize(3);
tft.setCursor (21,82); 
   double temp = ((5.0/1024.0) * analogRead(A1)) * 10; //10mV per degree 0.01V/C. Scalling

   
  tft.print (temp); // Print the current temp in f
  
  Serial.println (temp); // print the temp it the serial monitor
  settemp = EEPROM.read(1); // read the settemp on the eeprom

  delay (250); // wait for the lcd to refresh every 250 milliseconds

  if // if we se the switch up pin reading on 1 or 5 volts
    (digitalRead(swtu)== 1 )
  {
    settemp ++ // add one to the settemp, the settemp is the ideal temperature for you

      ;
  }

else{// other wise do nothing

}

if
(digitalRead (swtd) == 1) 
{
  (settemp --); 
}
else {
 
}

if (temp > settemp)  
{
  digitalWrite (led, 1);  
}
else  
{
  digitalWrite (led,0);
}

 

tft.setCursor (50,200);  
tft.print ("Set To ");  
tft.print (settemp);
tft.print(".C");
tft.print(" ");
  
Serial.println(settemp); 

EEPROM.write (1,settemp);  

delay (250);  

}

```

---

<div class="post-metadata">

**Author:** ![rlsieben](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/rlsieben/32/1079125_2.png) [@rlsieben](https://forum.arduino.cc/u/rlsieben)\
**Post date:** [December 18, 2024, 4:21pm UTC](https://forum.arduino.cc/t/mega-2560-code-problem/1333529/2 "2024-12-18T16:21:30Z")

</div>

Please use the code formatting tool when you post your code, so it is easier to see.

![Screenshot 2024-12-18 101957](https://europe1.discourse-cdn.com/arduino/original/4X/d/a/1/da11de7af54e6e022c4e314fb8daf906145995b6.png)

---

<div class="post-metadata">

**Author:** ![Dave\_Lowther](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dave_lowther/32/1092885_2.png) [@Dave\_Lowther](https://forum.arduino.cc/u/Dave_Lowther)\
**Post date:** [December 18, 2024, 4:58pm UTC](https://forum.arduino.cc/t/mega-2560-code-problem/1333529/3 "2024-12-18T16:58:31Z")

</div>

> [@ayhancan65](#):
>
> There is a screenshot on Arduino Uno I cannot take a screenshot on Mega2560

Do you mean that the code you pasted is your Uno code and you can't paste your Mega2560 code? If so, why can't you paste your Mega2560 code? Is it because you haven't written it yet?

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [December 18, 2024, 5:06pm UTC](https://forum.arduino.cc/t/mega-2560-code-problem/1333529/4 "2024-12-18T17:06:45Z")

</div>

> [@ayhancan65](#):
>
> `Adafruit_ILI9341`

As far as I understand it, your display uses SPI. If so, which pins do you use for the SPI interface? 11/12/13 will be the wrong answer 😉

---

<div class="post-metadata">

**Author:** ![ayhancan65](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ayhancan65/32/976017_2.png) [@ayhancan65](https://forum.arduino.cc/u/ayhancan65)\
**Post date:** [December 18, 2024, 5:12pm UTC](https://forum.arduino.cc/t/mega-2560-code-problem/1333529/6 "2024-12-18T17:12:27Z")

</div>

mega 2560 also 8,9,10,11,13 Uno also Same

---

<div class="post-metadata">

**Author:** ![ayhancan65](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ayhancan65/32/976017_2.png) [@ayhancan65](https://forum.arduino.cc/u/ayhancan65)\
**Post date:** [December 18, 2024, 5:13pm UTC](https://forum.arduino.cc/t/mega-2560-code-problem/1333529/7 "2024-12-18T17:13:04Z")

</div>

I didn't understand what you meant.

---

<div class="post-metadata">

**Author:** ![ayhancan65](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ayhancan65/32/976017_2.png) [@ayhancan65](https://forum.arduino.cc/u/ayhancan65)\
**Post date:** [December 18, 2024, 5:58pm UTC](https://forum.arduino.cc/t/mega-2560-code-problem/1333529/9 "2024-12-18T17:58:28Z")

</div>

I want to use the attached code on Mega 2560. So what kind of changes can I make to the Uno code in my attached code so that I can use it on Mega2560 in Papıp?

---

<div class="post-metadata">

**Author:** ![van\_der\_decken](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/van_der_decken/32/1006976_2.png) [@van\_der\_decken](https://forum.arduino.cc/u/van_der_decken)\
**Post date:** [December 18, 2024, 6:07pm UTC](https://forum.arduino.cc/t/mega-2560-code-problem/1333529/10 "2024-12-18T18:07:06Z")

</div>

Perhaps looking at where the SPI pins are on a Mega might be enlightening.

Hmm. No mention of SPI functionality where they were on an Uno...

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/4/f/4/4f465dabadffa35dd876aff4df5ef1699529693e.png)

Where are they? Right here.

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/c/f/d/cfdfa426913b1c1a4f6b97c126b039207a670b67.png)

When moving to a new board, don't assume that everything is where it was and works the same as it did on the old one. Always check.

---

<div class="post-metadata">

**Author:** ![ayhancan65](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ayhancan65/32/976017_2.png) [@ayhancan65](https://forum.arduino.cc/u/ayhancan65)\
**Post date:** [December 18, 2024, 6:47pm UTC](https://forum.arduino.cc/t/mega-2560-code-problem/1333529/11 "2024-12-18T18:47:30Z")

</div>

Yes, I checked, 50,51,52,53 correspond to the mega but it still did not work. I need a code and a connection diagram that will run my MEGA2560 tft screen ili9341 2.8 MEGA2560. I will rewrite the other codes, no problem.

---

<div class="post-metadata">

**Author:** ![hallowed31](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/hallowed31/32/1093680_2.png) [@hallowed31](https://forum.arduino.cc/u/hallowed31)\
**Post date:** [December 18, 2024, 7:21pm UTC](https://forum.arduino.cc/t/mega-2560-code-problem/1333529/12 "2024-12-18T19:21:47Z")

</div>

> [@ayhancan65](#):
>
> `EEPROM.write (1,settemp); `

It's ill advised to write to the EEPROM too often. You're doing it every time through the loop.

> EEPROM.update()  
> method that writes data only if it is different from the previous content of the locations to be written. This solution may save execution time because every write operation takes 3.3 ms; the EEPROM has also a limit of 100,000 write cycles per single location, therefore avoiding rewriting the same value in any location will increase the EEPROM overall life.

source:  
[https://docs.arduino.cc/learn/programming/eeprom-guide/](https://docs.arduino.cc/learn/programming/eeprom-guide/)

---

<div class="post-metadata">

**Author:** ![ayhancan65](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ayhancan65/32/976017_2.png) [@ayhancan65](https://forum.arduino.cc/u/ayhancan65)\
**Post date:** [December 18, 2024, 7:28pm UTC](https://forum.arduino.cc/t/mega-2560-code-problem/1333529/13 "2024-12-18T19:28:52Z")

</div>

My problem is not EEPROM

---

<div class="post-metadata">

**Author:** ![hallowed31](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/hallowed31/32/1093680_2.png) [@hallowed31](https://forum.arduino.cc/u/hallowed31)\
**Post date:** [December 18, 2024, 7:36pm UTC](https://forum.arduino.cc/t/mega-2560-code-problem/1333529/14 "2024-12-18T19:36:42Z")

</div>

Well, not _yet_...

---

<div class="post-metadata">

**Author:** ![hallowed31](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/hallowed31/32/1093680_2.png) [@hallowed31](https://forum.arduino.cc/u/hallowed31)\
**Post date:** [December 18, 2024, 7:41pm UTC](https://forum.arduino.cc/t/mega-2560-code-problem/1333529/15 "2024-12-18T19:41:52Z")

</div>

> pinMode(led, 5); // make led or pin13 an output

should be

```arduino
 pinMode(led, OUTPUT); // make led or pin13 an output

```

> int tempPin = A1; // make variables// thermistor is at A0

Which is it? A0 or A1?

---

<div class="post-metadata">

**Author:** ![van\_der\_decken](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/van_der_decken/32/1006976_2.png) [@van\_der\_decken](https://forum.arduino.cc/u/van_der_decken)\
**Post date:** [December 18, 2024, 9:10pm UTC](https://forum.arduino.cc/t/mega-2560-code-problem/1333529/16 "2024-12-18T21:10:31Z")

</div>

> [@ayhancan65](#):
>
> I will rewrite the other codes, no problem.

Yes, problem. But it's not your code.

Well, it's certainly not your _code_ that's preventing things from working. To say your code is correct would be a stretch - but it does display things on the screen.

Here is your sketch, working, with a Mega. I made one change that does not affect display operation - I multiplied by 100 rather than 10 to get the correct temperature.

 ![Not the code](https://europe1.discourse-cdn.com/arduino/original/4X/7/1/c/71c490a875a1c4a78c822e733fcb98762f36bb0b.jpeg)

I have not changed any pin definitions. I simply hooked up SCK and MOSI to the correct SPI pins on the Mega. DC, CS and RESET haven't changed.

TL;DR: there are many problems with your sketch (as others have already pointed out) but nothing that will prevent the display from working with a Mega.

---

<div class="post-metadata">

**Author:** ![van\_der\_decken](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/van_der_decken/32/1006976_2.png) [@van\_der\_decken](https://forum.arduino.cc/u/van_der_decken)\
**Post date:** [December 18, 2024, 9:13pm UTC](https://forum.arduino.cc/t/mega-2560-code-problem/1333529/17 "2024-12-18T21:13:37Z")

</div>

> [@hallowed31](#):
>
> > int tempPin = A1; // make variables// thermistor is at A0
> 
> Which is it? A0 or A1?

And oddly enough, tempPin is never even used in the code...

---

<div class="post-metadata">

**Author:** ![ayhancan65](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ayhancan65/32/976017_2.png) [@ayhancan65](https://forum.arduino.cc/u/ayhancan65)\
**Post date:** [December 18, 2024, 10:06pm UTC](https://forum.arduino.cc/t/mega-2560-code-problem/1333529/18 "2024-12-18T22:06:03Z")

</div>

My problem is not the temperature, but to be able to take a screenshot from the mega2560. My only concern is to be able to take an image (for example, just let it say Hello on the screen).

---

<div class="post-metadata">

**Author:** ![van\_der\_decken](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/van_der_decken/32/1006976_2.png) [@van\_der\_decken](https://forum.arduino.cc/u/van_der_decken)\
**Post date:** [December 18, 2024, 10:08pm UTC](https://forum.arduino.cc/t/mega-2560-code-problem/1333529/19 "2024-12-18T22:08:23Z")

</div>

It's just not possible to help someone who refuses to read. I'm done here.

---

<div class="post-metadata">

**Author:** ![ayhancan65](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ayhancan65/32/976017_2.png) [@ayhancan65](https://forum.arduino.cc/u/ayhancan65)\
**Post date:** [December 20, 2024, 2:28pm UTC](https://forum.arduino.cc/t/mega-2560-code-problem/1333529/20 "2024-12-20T14:28:06Z")

</div>

Hello, my code in the attachment works flawlessly on Arduino Uno. Now I want to try this code and the same circuit on MEGA2560. What kind of changes should I make in the codes? If you can help me, I am grateful.

```arduino
#include <ILI9486_SPI.h> //Hardware-specific library
#if (defined(TEENSYDUINO) && (TEENSYDUINO == 147))
ILI9486_SPI tft(/*CS=*/ 10, /*DC=*/ 15, /*RST=*/ 14);
// for my wirings used for e-paper displays:
#elif defined (ESP8266)
ILI9486_SPI tft(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2); // my D1 mini connection shield for e-paper displays
//ILI9486_SPI tft(/*CS=D8*/ SS, /*DC=D4*/ 2, /*RST=D3*/ 0); // my proto board for RPi display
#elif defined(ESP32)
ILI9486_SPI tft(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16);
#elif defined(_BOARD_GENERIC_STM32F103C_H_)
ILI9486_SPI tft(/*CS=4*/ SS, /*DC=*/ 3, /*RST=*/ 2);
#elif defined(__AVR)
ILI9486_SPI tft(/*CS=10*/ 8, /*DC=*/ 10, /*RST=*/ 9);
#elif defined(ARDUINO_ARCH_SAM)
ILI9486_SPI tft(/*CS=10*/ SS, /*DC=*/ 6, /*RST=*/ 5); // my proto board
#elif defined(ARDUINO_ARCH_SAMD)
// mapping suggestion for Arduino MKR1000 or MKRZERO
// note: can't use SS on MKR1000: is defined as 24, should be 4
// BUSY -> 5, RST -> 6, DC -> 7, CS-> 4, CLK -> 9, DIN -> 8 // my e-paper connector
ILI9486_SPI tft(/*CS=*/ 4, /*DC=*/ 7, /*RST=*/ 6); // to my proto board
#else
// catch all other default
ILI9486_SPI tft(/*CS=10*/ SS, /*DC=*/ 8, /*RST=*/ 9);
#endif

#if !defined(ESP8266)
#define yield()
#endif

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

void setup() {
  
 Serial.begin(9600); 
tft.setSpiKludge(false);
tft.init();
tft.fillScreen(RED); tft.setRotation(1); tft.setTextColor(WHITE); tft.setTextSize(4);
tft.setCursor(30, 30); tft.setRotation(1);
  tft.setTextColor(WHITE); tft.setTextSize(2);
tft.drawLine(10, 10, 60, 60, WHITE);
tft.drawFastHLine(30, 20, 40, WHITE);
tft.drawRect(50,50, 80, 100, WHITE);
tft.fillRect(200, 150, 100, 60, BLACK);
tft.fillCircle(200, 100, 30, GREEN);
tft.drawTriangle(40, 5, 40, 60, 70, 60, BLACK);
tft.fillTriangle(70, 70, 20, 200, 90, 50, YELLOW);
tft.drawRoundRect(400, 150, 60, 10, 20, BLUE);
tft.fillRoundRect(100, 150, 60, 50, 20, BLUE);  

pinMode(A0,INPUT);            
}

void loop() {

float X=analogRead(A0);
tft.setCursor(300, 130); 
tft.setTextColor(WHITE);
tft.setTextSize(2); 
tft.setRotation(1);
tft.println(X,1);

* Liste ögesi

delay(100);
//tft.init(X);
}

```

 ![12c158cb3273f9dbb42b9b0c4c2378b44c6cc46a_2_512x500](https://europe1.discourse-cdn.com/arduino/original/4X/a/a/b/aab41e512d4eeee738706bb3695582bf3e856b52.png)

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [December 20, 2024, 2:44pm UTC](https://forum.arduino.cc/t/mega-2560-code-problem/1333529/21 "2024-12-20T14:44:29Z")

</div>

You are aware that on a Mega the SPI pins are not on pins 11/12/13? Consult a Mega pinout diagram if in doubt.

---

<div class="post-metadata">

**Author:** ![ayhancan65](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ayhancan65/32/976017_2.png) [@ayhancan65](https://forum.arduino.cc/u/ayhancan65)\
**Post date:** [December 20, 2024, 2:47pm UTC](https://forum.arduino.cc/t/mega-2560-code-problem/1333529/22 "2024-12-20T14:47:08Z")

</div>

sterretje I am a novice in this subject. Could you explain it more clearly? Thank you.

[Next page](https://forum.arduino.cc/t/mega-2560-code-problem/1333529.md?page=2)
