Really basic questions - first time programming Arduino

Hi, I've been using Arduinos for a while now but mostly other peoples code. Well I now find myself at the start of a project that needs to be written by me. It's a project that has been done before by many others in a range of different ways but I need to start this one from scratch.

So, first off forgive the basic nature of this first inquiry. They will become more complex as the project goes on.

First question is about the anatomy of a sketch. Can someone confirm if my understanding is correct.

/* comment block at the start detailing purpose and nature of the sketch
also changelogs etc to help end users
*/

// Libraries to include and definitions of things

#include "a_library.h"               //These lines set the environment that the sketch runs in?
#include "a_n_other.h"

#define APackage_I2C_Adress 0x86

int mypin01 = 2


// void setup - this section is used to prepare
// the I/O in your environment to perform the functions you require?

void setup() {
pinmode (mypin01 = OUTPUT);  //setup defined pin as output
}

// main loop - a set of functions that repeat over and over 
// while the program is running.

void loop(){
                 // turning pins high or low
                 //checking data, waiting
                 //performing maths etc
}

// End of main loop

// Functions that operate outside of the main loop?
// If I understand this section contains functions that interrupt the main loop
// and perform some calculations or changes to the environment.

Then my next question is about workflow. Have I chosen the best workflow for my project?

Skinny on my project: LED lighting controller (PWM) wth RTC. Fades lights up and down like sunrise/sunset. Time and light cycle must be able to set via LCD. Also requires test mode that tests LED levels and another that tests the cycle quickly (24hours in a few minutes)

Hardware is Duemillanove (and Nano mini) using a Nokia 3310 screen + joystick.

My intended workflow: *Get the LCD menu and submenu items working - Home screen - Set time - Set cycle - test led - test cycle *Get RTC working and displaying on home screen *Write LED test function *Write cycle function and check it works using a default cycle in the code *Write cycle test function *Write set time function *Write set cycle function

I'd appreciate all and any advice at this stage of my learning and project.

Thank you.

//These lines set the environment that the sketch runs in?

No. They define what libraries you may be using. Helpers, in other words.

#define APackage_I2C_Adress 0x86

Are you doing I2C?

int mypin01 = 2

Missing a ; on the end...

pinmode (mypin01 = OUTPUT);  //setup defined pin as output

No. The pinMode function takes two arguments - the pin number and the mode. The = should be a comma.

pinmode (mypin01, OUTPUT);  //setup defined pin as output

Have I chosen the best workflow for my project?

What have you chosen? Can't comment on whether it is best until we know what it is, and what it is trying to accomplish, within what constraints.

Time and light cycle must be able to set via LCD.

That's going to be hard, since an LCD is an output device.

*Get the LCD menu and submenu items working - Home screen - Set time - Set cycle - test led - test cycle

Getting the LCD working first is a good idea. Getting the clock working next is essential to being able to set it, don't you think?

// void setup - this section is used to prepare // the I/O in your environment to perform the functions you require?

Well yes, but it's not limited to that - any initialization or one shot work is done here. There might be introductory serial prints to announce the sketch version, putting the LCD in the appropriate mode, reading config from EEPROM etc. etc.

// main loop - a set of functions that repeat over and over // while the program is running.

Yes, although the word functions is a bit misleading in this context, functionality might be clearer.

// Functions that operate outside of the main loop? // If I understand this section contains functions that interrupt the main loop // and perform some calculations or changes to the environment.

Yes, but not necessarily. Functions defined in the sketch can be called from any other, including loop and setup. They may be created because you need to do the same thing in several places without repeating the code, or it may be just a means of shortening another function (likely loop) so it's easier to understand. Functions may indeed also be invoked by interrupts - you would likely configure this in setup.

In your sketch you should add the "global variables section", where you put declarations like:

int mypin01 = 2

This is a global variable, which btw is also initialized with value 2.

Global variables are sometimes initialized in setup() function.

IMHO it's mandatory to understand the difference between global and local variables, or else you'll have a hard time writing functions that work correctly. Feel free to ask about it, of course.

Thank you for all your answers. The actual code in the example isn’t real code I’m using, hence the syntax errors here and there also sorry if I have used any misleading vocabulary. Generally take what it written to mean what it means in English not code.

Setting cycle via LCD will be possible as the LCD nokia board I use has a built in joystick to handle user inputs.

I’m really just trying to get my head round what needs to go where when I write the code and how the main loop gets interrupted to perform desired tasks.

This is where I’m at so far. It compiles but doesn’t do anything yet and I’m waiting for some parts to arrive to build another controller to develop on (the current one I have is usig different code and busy controlling my reef lights).

/* ------------------------------------
Noriduino Reef Lighting Controller

Acknowledgements:- Big thanks to- Dave Rosser, Hugh Dangerfield, Christian, cptbjorn@gmail.com

Purpose:      - to improve upon the work of the Krusduino project
Written for:  - Duemillanove based Krusduino (Temp probe removoed) with Chester's Garage LED Shield v2

Aims:         - to provide smoother ramp up and ramp down functions
              - to allow setting of photoperiod via LCD (removes the need to load from eeprom for cleaner codebase)
              - to allow setting of ramp times (removes the need to load from eeprom for cleaner codebase)
              - to provide 4 channels with graphical representation
              
Changelog:    beta 0.01
              * initial code
              
-------------------------------------*/

// Libraries

#include <Wire.h>         // specify use of Wire.h library.
// #include <Messenger.h>    // This example executes serial commands through a callback function
#include "nokia_3310_lcd.h"

// Definitions ------------------------------------------------------------------------------------

// *************************** User configurable settings start here ******************************

// LED Dimming channels

int WLed1Pin = 6;  //White channel (consider moving due to duty cycle bug on ch 6 & 5)
int Bled1Pin = 5;  //Blue channel
// int Chan3Pin =  //For adding 3rd PWM channel
// int Chan4Pin =  //For adding 4th PWM channel


// Photoperiod and ramp settings

int blueramptime = 60 ;    // minutes after start time to hit bluemax
int whiteramptime = 180 ;  // minutes after start time to hit whitemax
int bluemin = 0 ;          // minimmum dimming value of blue LEDs, range of 0-255
int bluemax = 255 ;        // maximum dimming value of blue LEDs, range of 0-255
int whitemin = 0 ;         // minimum dimming value of white LEDs, range of 0-255
int whitemax = 255 ;       // maximum dimming value of white LEDs, range of 0-255
int photoperiod = 240 ;    // minutes after white hits max to begin fade out
int ontime = 10 ;          // time of day (hour, 24h clock) to begin photoperiod fade in

int bluepercent[11] = { 0, 26, 52, 78, 103, 128, 154, 180, 205, 230, 255 };    // these are the values in 10% increments
int whitepercent[11] = { 0, 26, 52, 78, 103, 128, 154, 180, 205, 230, 255 };   // these are the values in 10% increments

//****** User config ends here - don't touch anything else unless you know what you are doing ********


//Real Time Clock variables

int secs, mins, hrs, day, date, month, year;

int set_secs = (0); //seconds;
int set_mins = (0) ; //minutes
int set_hrs = (10); //hours (24hr time)
int set_day = (6); // Day 01-07
int set_date = (1); // Date 0-31
int set_month = (10); // month 0-12
int set_year = (9); // Year 00-99

byte CST;


//Nokia 3310

#define NUM_MENU_ITEM	6  // Number of scrollable menu items

// Menu definitions
char menu_items[NUM_MENU_ITEM][12]={
	"NORIDU  ",
        "SET TIME",
        "CYCLE   ",
        "TEST LED",
        "TEST RUN",
        "ABOUT   "
};

void (*menu_funcs[NUM_MENU_ITEM])(void) = {
	menu_noridu,
        menu_set_time,
        menu_cycle,
        menu_test_led,
        menu_test_run,
        menu_about
};

char current_menu_item;

const char RTC_MENU_ITEMS = 9;    // Set time options - Hrs, Mins, Secs, Day, D, M, Y, Ok, Cancel
const char CYCLE_MENU_ITEMS =10;    // Set cycle options - Bmin, Bmax, Wmin, Wmax, Bfade, Wfade, Duration, On time, Ok, Cancel
const char LED_TEST_ITEMS = 7;    // Led test options - (White) ^^^, (Blue) ^^^, Back
const char CYCLE_TEST_ITEMS = 2;  // Cycle test options - Run, Back

// menu starting points
#define MENU_X	10		// 0-83
#define MENU_Y	1		// 0-5


//keypad debounce parameter
#define DEBOUNCE_MAX 15
#define DEBOUNCE_ON  10
#define DEBOUNCE_OFF 3 

#define NUM_KEYS 5

// joystick number
#define UP_KEY 3
#define LEFT_KEY 0
#define CENTER_KEY 1
#define DOWN_KEY 2
#define RIGHT_KEY 4

// adc preset value, represent top value,incl. noise & margin,that the adc reads, when a key is pressed
// set noise & margin = 30 (0.15V@5V)
int  adc_key_val[5] ={30, 150, 360, 535, 760 };

// debounce counters
byte button_count[NUM_KEYS];
// button status - pressed/released
byte button_status[NUM_KEYS];
// button on flags for user program 
byte button_flag[NUM_KEYS];

byte LCD_backlight_pinState = 1;
char tstr[5];  // temporary store for manipulating strings for LCD output

Nokia_3310_lcd lcd=Nokia_3310_lcd();



// Setup --------------------------------------------------------------------------------

void setup() {
  
//pinmode (WLed1Pin, OUTPUT);  //sets up pin as output
//pinmode (BLedPin, OUTPUT);   //sets up pin as output

//RTC setup

//Nokia 3310 setup

   // setup interrupt-driven keypad arrays  
   // reset button arrays
   for(byte i=0; i<NUM_KEYS; i++){
     button_count[i]=0;
     button_status[i]=0;
     button_flag[i]=0;
   }
  
  // Setup timer2 -- Prescaler/256
  TCCR2A &= ~((1<<WGM21) | (1<<WGM20));
  TCCR2B &= ~(1<<WGM22);
  TCCR2B = (1<<CS22)|(1<<CS21);      
  
  ASSR |=(0<<AS2);

   // Use normal mode  
   TCCR2A =0;    
     //Timer2 Overflow Interrupt Enable  
     TIMSK2 |= (0<<OCIE2A);
     TCNT2=0x6;  // counting starts from 6;  
   TIMSK2 = (1<<TOIE2);    
   
                  
 
  SREG|=1<<SREG_I;
  
  lcd.LCD_3310_init();
  lcd.LCD_3310_clear();
  
   //menu initialization
   init_MENU();
   current_menu_item = 0;	



}


// Loop

void loop()  {

// Dimming function

// Cycle start

// Cycle at maximum

// Cycle fade out

  
}

// Functions

// Nokia menu stuff

void init_MENU(void){

  byte i;
  
  lcd.LCD_3310_clear();

    lcd.LCD_3310_write_string(MENU_X, MENU_Y, menu_items[0], MENU_HIGHLIGHT );
    
}

// waiting for center key press
void waitfor_OKkey(){
  byte i;
  byte key = 0xFF;
	while (key!= CENTER_KEY){
    for(i=0; i<NUM_KEYS; i++){
       if(button_flag[i] !=0){
           button_flag[i]=0;  // reset button flag
           if(i== CENTER_KEY) key=CENTER_KEY;
        }
     }
   }
		
}

void menu_noridu(){
 
}

void menu_set_time(){
 LCDSetupRTClock();
}

void menu_cycle(){
 LCDSetupCycle();
}

void menu_test_led(){
  LCDSetupLEDTest();       
}

void menu_test_run()
{
  LCDSetupRunTest();       
}


void menu_about(){
  
  lcd.LCD_3310_write_string(0, 0, "  ", MENU_NORMAL);   // text strings for the about page
  lcd.LCD_3310_write_string(0, 1, "  ", MENU_NORMAL);
  lcd.LCD_3310_write_string(0, 2, "  ", MENU_NORMAL);
  lcd.LCD_3310_write_string(0, 3, "  ", MENU_NORMAL);
  lcd.LCD_3310_write_string(0, 4, "  ", MENU_NORMAL);
  lcd.LCD_3310_write_string(0, 5, "  ", MENU_NORMAL);
  lcd.LCD_3310_write_string(54, 5, "  ", MENU_NORMAL);
  
  lcd.LCD_3310_write_string(30, 5, "OK", MENU_HIGHLIGHT );
	waitfor_OKkey();   
}

Without wishing to sound rude, buy yourself a book. They are knowledge in distilled form and nearly always save time, when learning something new.