Starting out with the MEGA 2560

Help...

Hi All, I realise this probably has been done to death a million or more times but I am struggling to find the information I require.

I am old school - started out many years ago 2002 with the Micro Chip PIC (16F636, 16F684, 16F877, & 16F84) NEC-78K Series, Mitsubishi M16C/62P, ATMEL (ATtiny26L, ATmega8)

But since then apart from a brief section in 2010 when I need to design and build a four channel data logger based on a 16F877 with RS232 comms and 4x16 LCD Screen.

Wife brought me the Arduino Mega 2560 a few years ago as a Christmas Present and last few days I have picked it up to have a play and get back into programming as I miss it...

But things have moved on so much that I do not recognise anything - looking at sample source code all the headers appear to be missing - do you still need to refer to the various library's if so where the hell are they located - what is this sketch pad how does this work?

Can someone please point me in the right direction as I feel like I have landed on an Alien Planet lol

So will the AT Mega8 code look at all remotely similar?

As I am a new member and unable to upload attachments I am placing some example code below from 2010 for the PIC 16F877 - this is how I used to do things - is this still current?

Delays appear to be in mills or similar wording from what I can find.

All I want is a basic setup file to get be started and back up to speed rest I can work out lol

/*==================================================================================
** COMPANY = Wolverhampton University - Third Year Project
** PROJECT = Temperature Data Recorder
** MODULE =
** VERSION = 0.0
** DATE = 28-07-2010
** LAST CHANGE =
** =================================================================================
** Description: Main Temperature Recording Programme
**
** Enviroment: Device: PIC 16F877 Version:
** Assembler: HI-TECH PICC Lite Version: Version 9.60
** C-Compiler: HI-TECH PICC Lite Version: Version 9.60
** Linker: HI-TECH PICC Lite Version: Version 9.60
** Programmer: PICkit(tm)1 Version: Flash Starter Kit, V1.60.0
** Firmware Version 2.0.2.
**
** =================================================================================
** By: James Mason
** =================================================================================
Changes:
** =================================================================================
*/

//#define RUN_TESTS
#include <htc.h>
#include "portability.h" // You MUST read portability.h for all macros X_()
#define PIC_HW
#include "hardware.h" // All hardware definitions
#include "lcd.h"
#include "delay.h"
#include "DS18B20.h"
#include <stdio.h>
#include <string.h>
#include "Serial.h"
#include "EEPROM.h"
#include "DS1307.h"
#include "Tests.h"
#include "User.h"

// Function Prototypes
//char test_buttons(void);
//void run_tests(void);
void do_readings(void);
void put_sleep(void);
//void set_time(void);
//void set_date(void);

//Global Variables
//eeprom int reading_address;
unsigned char shadowB=0;
unsigned char SerialFlag;
char uart_data = 0;
int user_num = 0;
unsigned char eeprom_data[10];
volatile long eeprom_address = 0;
long reading = 1;
volatile char int_flag;
char start_readings = 0;
char start_serial;
static bit button_pressed;
static bit sleep;
char serial_present;

/**** INTERRUPT ****/
void interrupt timer1(void)
{

if(RBIF)
{
	RBIF = 0;		
}

if(TMR1IF && TMR1IE)
{
	TMR1IF = 0;				// Clear interrupt flag
	int_flag = 1;			// Set flag to trigger routine in main
	TMR1ON = 0;
	NOP();
	TMR1H = 0xFC;
	TMR1L = 0x7C;			// Set timer for a count of 60 (FC7C = 15 mins at 1Hz) FFC4 = 1min FFEC = 20 seconds
	NOP();
	TMR1ON = 1;
}

}

/**** MAIN PROGRAM ****/

void main(void)
{
int i = 0;
int button_timer = 0x1FFF;
int_flag = 0; // initialise interrupt flag
serial_present = 0; // initialise flags to 0
start_serial = 0;
button_pressed = 0;
sleep = 0;
restore_context(); // Restore reading address if power has failed whilst readings are being taken
LCD_POWER = 0; // Turn LCD on
init(); // initialise ports etc
i2c_init(); // initialise I2C
lcd_init(); // initialise LCD
RTC_StartOutput(); // Start RTC and output 1Hz square wave

DelayMs(200);
lcd_clear();

#if defined(RUN_TESTS)
run_eeprom_test();
run_tests(); // Does not return - uncomment to run tests
#endif

lcd_puts("TEMP LOGGER");
lcd_goto(0x40);
lcd_puts("v1.0.0");
DelayMs(2000);
lcd_goto(0x40);
lcd_puts("By James Mason");
DelayMs(2000);
lcd_clear();
lcd_goto(0x00);
lcd_puts("Press button");
lcd_goto(0x40);
lcd_puts(" to start");

if(start_readings)
{
	lcd_clear();
	lcd_puts("Continuing");
	lcd_goto(0x40);
	lcd_puts("Readings");
	DelayMs(1000);	
}


/********** MAIN LOOP ***************************/
/*	Tests switches and runs according functions	*/
/*												*/
/************************************************/	
while(1)
{
	char button = test_buttons();			// Test for button press

	button_timer--;
	if(button_timer == 0) put_sleep();		// If time out without button press, then sleep

	if(sleep)						// On wake up from sleep
	{
		sleep = 0;					// Turn sleep flag off
		button_timer = 0x1FFF;		// Reset button timer
		RBIE = 0;					// Turn button interrupt off, to enable buttons to be used
		//LCD_POWER = 0;				// Turn LCD on
		//DelayMs(100);
		//lcd_init();					
		//lcd_clear();
		if(int_flag == 0 && TMR1IE == 1)		// Check if wake up is from button press
		{
			LCD_POWER = 0;				// Turn LCD on
			DelayMs(100);
			lcd_init();					
			lcd_clear();
			read_eeprom_temp_lcd(eeprom_address - 16);			// Read back last reading to check data has written correctly 
			lcd_goto(0x08);										// and disply on LCD
			read_eeprom_temp_lcd((eeprom_address - 16) + 2);
			lcd_goto(0x40);
			read_eeprom_temp_lcd((eeprom_address - 16) + 4);
			lcd_goto(0x48);
			read_eeprom_temp_lcd((eeprom_address - 16) + 6);
			lcd_goto(0x10);
			lcd_puts("Rd: ");
			lcd_putdec(reading - 1);
			lcd_goto(0x50);
			read_time_eeprom_lcd((eeprom_address - 16) + 8);
			lcd_goto(0x58);
			read_date_eeprom_lcd((eeprom_address - 16) + 11);
		}
	}

	if(button == BUTTON1)
	{
		button_timer = 0x1FFF;
		if(LCD_POWER == 1) 
		{
		LCD_POWER = 0;
		DelayMs(100);
		lcd_init();
		lcd_clear();
		read_eeprom_temp_lcd(eeprom_address - 16);			// Read back last reading to check data has written correctly 
		lcd_goto(0x08);										// and disply on LCD
		read_eeprom_temp_lcd((eeprom_address - 16) + 2);
		lcd_goto(0x40);
		read_eeprom_temp_lcd((eeprom_address - 16) + 4);
		lcd_goto(0x48);
		read_eeprom_temp_lcd((eeprom_address - 16) + 6);
		lcd_goto(0x10);
		lcd_putdec(reading - 1);
		}

		int i = 5;
		if(start_readings == 0)
		{
		start_readings = 1;				// Start temp sensor readings
		lcd_clear();
		lcd_puts("Readings started");
		}
		TMR1IE = 1;						// Enable Timer interrupt for taking readings

		while(button == BUTTON1)		// If button held down
		{
			button_pressed = 1;
			button = test_buttons();
			DelayMs(1000);
			i--;
			if(i == 0)						// If held for >5 seconds
			{ 
			eeprom_address = 0;				// reset reading address
			reading = 1;					// reset reading counter (for display on LCD)
			lcd_clear();
			lcd_puts("Readings stopped");
			lcd_goto(0x40);
			lcd_puts(" and zeroed");
			start_readings = 0;
			save_context();
			TMR1IE = 0;
			TMR1ON = 0;						// Turn timer off to change value
			NOP();
			TMR1H = 0xFF;					// Set for next reading in a few seconds
			TMR1L = 0xFD;
			TMR1ON = 1;
			}
		}	
		DelayMs(500); // for debug, remove later
		lcd_clear();
	}

	if(button == BUTTON2)
	{

			button_timer = 0x1FFF;
			i = 5;

			while(button == BUTTON2)
			{
				button_pressed = 1;
				button = test_buttons();
				DelayMs(1000);
				i--;
				if(i == 0)						// If held for 5 seconds, go to set time
				{ 
					lcd_clear();
					lcd_goto(0x00);
					lcd_puts("Set Time");
					set_time();
				}
		}	

	}

	if(button == BUTTON3)
	{

			button_timer = 0x1FFF;
			i = 5;

			while(button == BUTTON3)
			{
				button_pressed = 1;
				button = test_buttons();
				DelayMs(1000);
				i--;
				if(i == 0)							// If held for 5 seconds, go to set date
				{ 
					lcd_clear();
					lcd_goto(0x00);
					lcd_puts("Set Date");
					set_date();
				}
		}	

	}

	if(button == BUTTON4)
	{

			button_timer = 0x1FFF;
			i = 5;

			while(button == BUTTON4)
			{
				button_pressed = 1;
				button = test_buttons();
				DelayMs(1000);
				i--;
				if(i == 0)							// If held for 5 seconds, activate serial comms
				{ 
					start_serial = 1;
					lcd_clear();
					lcd_goto(0x00);
					lcd_puts("Serial Active");
				}
		}	

	}


	if(start_readings) do_readings();						// take readings if started

	serial_changed();										// Check for serial cable plugged in
	if(start_serial && serial_present)  serial_commands();	// put last as can block interrupt 
}	

} // end of main

// Takes readings from sensors and writes to eeprom with time/date stamp
void do_readings()
{
char output[10] = {0,0,0,0,0,0,0,0,0,0};
char crc = 0;
char temp;

if(int_flag == 1)
{
	int_flag = 0;
	
	LED2 = 1;
	/*
	LCD_POWER = 0;
	DelayMs(150);
	lcd_init();
	DelayMs(150);
	lcd_clear();
	lcd_goto(0x00);
	lcd_puts("Take Reading");
	lcd_goto(0x40);
	lcd_putdec(reading);

	DelayMs(1500);
	*/

	crc = write_eeprom_temp(eeprom_address, DQ1);			// Write sensor 1 data to eeprom
	if(crc)
	{ 
		write_ext_eeprom((eeprom_address + 14), 0x80);
	}
	else
	{
		write_ext_eeprom((eeprom_address + 14), 0x00);
	}
	crc = write_eeprom_temp(eeprom_address + 2, DQ2);		// sensor 2
	if(crc)
	{ 
		temp = read_ext_eeprom(eeprom_address + 14); 
		write_ext_eeprom((eeprom_address + 14), (temp | 0x08));
	}
	crc = write_eeprom_temp(eeprom_address + 4, DQ3);		// sensor 3
	if(crc)
	{ 
		write_ext_eeprom((eeprom_address + 15), 0x80);
	}
	else
	{
		write_ext_eeprom((eeprom_address + 15), 0x00);
	}
	crc = write_eeprom_temp(eeprom_address + 6, DQ4);		// sensor 4
	if(crc)
	{ 
		temp = read_ext_eeprom(eeprom_address + 15); 
		write_ext_eeprom((eeprom_address + 15), (temp | 0x08));
	}
	write_time_eeprom(eeprom_address + 8);			// Write time to eeprom
	write_date_eeprom(eeprom_address + 11);			// Write date to eeprom

	/*
	// Display data written to eeprom
	lcd_clear();
	read_eeprom_temp_lcd(eeprom_address);			// Read back to check data has written correctly
	lcd_goto(0x08);									// and display on LCD
	read_eeprom_temp_lcd(eeprom_address + 2);
	lcd_goto(0x40);
	read_eeprom_temp_lcd(eeprom_address + 4);
	lcd_goto(0x48);
	read_eeprom_temp_lcd(eeprom_address + 6);
	lcd_goto(0x14);
	RTC_ReadTimeLCD();
	lcd_goto(0x54);
	RTC_ReadDateLCD();
	*/
	eeprom_address+=16;			// increment reading address
	reading++;					// increment reading counter
	save_context();				// Save reading address + counter in case of power failure
	DelayMs(2000);
	LED2 = 0;
		
	if(reading >= 6720)			// Reading number to stop at (6720 = 4 * 24 hours * 70 days - for readings every 15 min for 10 weeks) 
	{
		lcd_clear();
		lcd_goto(0x00);
		lcd_puts("Readings");
		lcd_goto(0x40);
		lcd_puts("complete");
		TMR1IE = 0;
		while(test_buttons() == 0)
		{
		LED2 = 1;
		DelayMs(500);
		LED2 = 0;
		DelayMs(1000);
		}
	}
}

}

// Sends PIC to sleep and turns off LCD for power saving
void put_sleep()
{
LED1 = 0;
LED2 = 0;
PORTD = 0b00000000; // Make sure all sensor pins are low, and LCD pins are low
RBIE = 1; // Turn on button interrupts for wake up
LCD_POWER = 1; // Turn off LCD
sleep = 1; // Set sleep flag
SLEEP(); // Sleep
}

Welcome to the forum

To post images etc. you need trust level 1, you can get there by:

  • Entering at least 5 topics
  • Reading at least 30 posts
  • Spend a total of 10 minutes reading posts

Users at trust level 1 can...

  • Use all core Discourse functions; all new user restrictions are removed
  • Send PMs
  • Upload images and attachments

However, attaching program files is not the preferred method of uploading code to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use [color = red]code tags[/color] (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

OMG lot of rules etc - things have changed so much lol

Code posting was purely as an example to show what I was used to doing in 2002 & 2010

Not for commenting on...

So where do I locate info on the Mega 2560 as it appears to be an obsolete board

The Arduino IDE does a couple of things for you. One thing is to automatically include the file 'Arduino.h' which takes care of all standard includes that you need when you start compiling; this is done by a process known as the builder.

You can find all standard include files in the 'Arduino15' directory on your PC; on a Windows system, it is located in C:\Users\yourUsername\AppData\Local\Arduino15\, for e.g. a Mega C:\Users\yourUsername\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\cores\arduino where 1.8.6 is the version of the AVR board package. You will find the cores there, some additional libraries will be C:\Users\yourUsername\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries; just dig around :wink:

You're not advised to changes things in there; an update of a board package will overwrite your changes.

3rd party libraries go in the libraries directory located in the sketchbook directory. You can find the sketchbook directory under file → preferences in the IDE.

Brilliant thank you...

So there are files on the PC - thought there should be but was unable to locate them...

Will have a quick look what is available - presume there should be some demo code to get you started like a flashing LED etc. in there as well.

Ok I have info missing - C:\Users\yourUsername

Then I loose the trail - I have:- .arduino-create & .arduinoIDE

Rest of the path does not exist

No AppData found on this Laptop...

AppData is a hidden folder; you have to enable "show hidden files" in Windows explorer.

Further I made the incorrect assumption that you're using a locally installed IDE; that might not be the case if I see arduino-create (so much for old-school :rofl:)

Not really

The reasons for asking that code of any kind is posted inside code tags are

  • to prevent the forum software interpreting any of it as HTML commands and thus making a nonsense of it
  • it puts the code in a scrolling window
  • it allows copying of the whole code with a single mouse click

Understood - will do in the future.

Looks like that did the trick - going to take 6 minutes to assign attributes lol

That just totally crashed my pc - lost all files on desktop etc...

Just trying a system restart to recover the works pc

Sorry to hear. What crashed your PC? The change to make the hidden files and directories visible?

Yes...

Totally removed my user access everything...

Users was empty after changes...

Desktop was empty just the trash can symbol...

True that, there are some particular particulars.

The good thing is that the programming language is C++, so any C chops you have will be advantageous - I only use C++ inadvertently or when dragging in a library written in C++.

The internet is clogged with tutorials and how-tos and so forth; adding "arduino" to just about any search will demonstrate that almost anything you can think of has been done, however well or not.

arduino chicken coop door

Also, the IDE has many examples on offer, and they usually link to an article that is informative if not brilliant and perfect.

Lastly, I suggest going with the flow. To school noobs on a basic structure for this kind of programming, the "new" sketch has two functions you must write

void setup() {

}

// and... 

void loop() {

}

As you might have seen by now, setup() is used for stuff you do at startup time, like fixing I/O pins or initialising things and

loop() is called repeatedly by the main() function the designers keep us away from. You can find the main() without too much trouble, you will see literally almost as simple as

int main(void) {

    setup();

    for (; ; )
        loop();
}

But every line of code that goes into a sketch (sry, just going with the flow here and calling programs sketches) is either one you wrote, or can be found at the source code level somewhere on you machine, library or not. This can be very useful and educational, how they do that? kinda thing.

Lastly, I've never heard of the Mega 2560 going obsolete, this I found now I know but my suspicion is that except for some weedy stuff you can just plow ahead calling it a Mega:

HTH and welcome to the fora.

a7

Hidden files tab...

Lost everything...

Folders are empty

Work going to kill me - blank pc

Yeah, that. Arduino is specifically aimed at people without that sort of knowledge, so a lot of the traditional "baggage" is done for you.

All I want is a basic setup file to get be started

The basic program is what you get when you open an IDE window. It has setup() and loop() functions, and will compile just like that. All of the necessary "setup" that is traditional when working with microcontrollers is done automagically based on the "board" setting in the tools menu.

presume there should be some demo code to get you started like a flashing LED etc. in there as well.

In the "Files" menu, there is an "Examples" sub-menu with numerous examples.
If you add arduino libraries, you get additional examples specific to those libraries.


will the AT Mega8 code look at all remotely similar?

Arduino ATmega8 (or ATmega328P ("Uno")) code will be similar to Arduino Mega2560 code. It probably won't look similar to your old ATmega8 or PIC "bare metal" code because Arduino tends to rely on libraries that "abstract" hardware into C++ objects. For your temperature logging example, you'd search for a DS18B20 temperature sensor library, and a DS1307 RTC library, and use them to make a sketch that looked something like:

void loop() {
   delay(60*1000UL);  // every minute
   printTime(rtc.readTime);
   printTemp(sensor.readTemp);
}

(The Arduino library manager is pretty phenomenal, letting you search hundreds of official and user-submitted libraries from the "sketch/Libraries/Library Manager" menu. Actual quality of the libraries can vary a great deal.)

(actually, MOST Arduino users would probably Google "Arduino Serial Temperature Logger" and use one of the many results/tutorials, with or without minor modifications. For better or worse.)

You CAN write "bare metal" code, it's just that it's rarely necessary unless you have really special requirements.

The Mega2560 is not obsolete, but it is less commonly used than Unos and Nanos. For a while, many people were powering their 3D printers with Megas.

Tell me about it...

Clicked show hidden folders in Windows 10, it then started synching and organising folders took 6 minutes saw all the files i needed in the box as it modified them then poof

Everything was removed, its like a new Windows setup.

But the folders and files are there...

My work folder has 6gbyte of data, but i am unable to view the folder wish i could send a screen shot

I don't think I've ever heard of something like that happening, arduino-related or otherwise (and arduino doesn't do anything special to the filesystem, so it shouldn't have anything to do with "show hidden files" operations. The whole "appdata" thing is a windows standard "thing" for dealing with files installed and needed by an application.

Occasionally workplace o student environments will have "odd" behaviors due to "Security" requirements. It may make sense to look into a "portable install" of the Arduino environment.

Bizarre. But if you “know the files are there” does that mean File Explorer is still OK? In which case, what else is also usable? And what is not?

Can you use System Restore? I suggest you post (with a much fuller description of what you did and what you now see) in one of the Windows forums.

I’m assuming you don’t have an image restoring tool like Macrium Relect you can turn to?

All sorted - took a few hours - everything was hidden folders - but got everything fully back phew...

So setting up an old laptop for this project...