ARDUINO PROGRAMMING, AND INTERFACING HELP

Post all your model railway electronic problems, solutions and discoverys here.
User avatar
TimberSurf
Posts: 2107
Joined: Wed Jan 08, 2014 5:47 pm
Location: N.Wales
Contact:

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby TimberSurf » Sun Jul 08, 2018 1:04 pm

OK, playing with binary intrigued me, so I had a play. In this case it does knock about 40 lines off the code (so not just 30!,more like 80 total so we saved a third) but it now makes it much harder to understand. If we were trying to drive hundreds of outputs, then it would save a massive amount of code, but the arduino won't support more than 20 odd so not really worth the effort. Enjoy the comparison...

Code: Select all

//Two direction Traffic Lights
// using direct binary read bits to outputs
// only the first (rightmost) bits of the Byte are read

unsigned long Tcurrent = 0;    // stores the value of millis() in each iteration of loop() this is equivalent to noting the time from a clock
unsigned long Tlapseold = 0; // the time when the last state was set
unsigned long Tlapse = 1000; // the time between each state change

//Declaring the LED and button pins and initial state
bool EWgreen = LOW;
bool EWyellow = LOW;
bool EWred = LOW;
bool NSgreen = LOW;
bool NSyellow = LOW;
bool NSred = LOW;
int Arraysix = 0;
int state = 0;

void setup()
{
   //Declaring the functioning of pins to outputs
   pinMode(2, OUTPUT); 
   pinMode(3, OUTPUT);
   pinMode(4, OUTPUT);
   pinMode(5, OUTPUT); 
   pinMode(6, OUTPUT);
   pinMode(7, OUTPUT);
}

void loop(){
Tcurrent = millis();   // capture the latest value of millis()
  if (Tcurrent - Tlapseold >= Tlapse) {  // check if the lapse time has elasped
    Tlapseold = Tcurrent;  // reset the last time the state as changed
    state = state + 1;  //increment the state by 1
  }
// store the bit status of the lights as and integer "Arraysix" for each state
   if (state == 1)    //EW Green,  NS Red     
   {
    Arraysix = 33;  //bin = [00]100001
   }
   else if (state == 11)    //EW Yellow,  NS Red   
   {
    Arraysix = 34;  //bin = [00]100010
   }
   else if (state == 14)    //EW Red,  NS Red   
   {
    Arraysix = 36;  //bin = [00]100100
   }
   else if (state == 16)    //EW Red,  NS Red/Amber   
   {
    Arraysix = 52;  //bin = [00]110100
   }
   else if (state == 18)    //EW Red,  NS Green   
   {
    Arraysix = 12;  //bin = [00]001100
   }
   else if (state == 22)    //EW Red,  NS Yellow   
   {
    Arraysix = 20;  //bin = [00]010100
   }
   else if (state == 25)    //EW Red,  NS Red   
   {
    Arraysix = 36;  //bin = [00]100100
   }
   else if (state == 27)    //EW Red/Amber,  NS Red   
   {
    Arraysix = 38;  //bin = [00]100110
   }
   
   if (state == 28)  {  //return to begining
      state = 0;
   }

// read each bit and store in the bool variables

EWgreen = bitRead(Arraysix, 1);
EWyellow = bitRead(Arraysix, 2);
EWred = bitRead(Arraysix, 3);
NSgreen = bitRead(Arraysix, 4);
NSyellow = bitRead(Arraysix, 5);
NSred = bitRead(Arraysix, 6);

// write the bool variables to the pins

    digitalWrite(2,EWgreen);
    digitalWrite(3,EWyellow);
    digitalWrite(4,EWred);
    digitalWrite(5,NSgreen);
    digitalWrite(6,NSyellow);
    digitalWrite(7,NSred);
}
Image
Lumsdonia <--- Hit link to go to my website for full story and wiring advice!

User avatar
TimberSurf
Posts: 2107
Joined: Wed Jan 08, 2014 5:47 pm
Location: N.Wales
Contact:

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby TimberSurf » Sun Jul 08, 2018 1:20 pm

MikeTrymTrains wrote:Hi TimberSurf I tried the code from this forum and it work on my mega so i don't know why it won't compile.

I know now!
It does compile in the ide :D , but I always try stuff in my simulator first (you can see the variables live) and it would not compile in that. There is a bug in the simulator that won't compile certain functions, obviously your code has it! (I don't think it likes your static command!)
Image
Lumsdonia <--- Hit link to go to my website for full story and wiring advice!

MikeTrymTrains
Posts: 14
Joined: Mon Jul 02, 2018 9:58 pm

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby MikeTrymTrains » Sun Jul 08, 2018 2:44 pm

Hi TimberSurf the last code is not running right on the Arduino UNO also the code before it run but the green leds stay on for longer than a second ten seconds and four seconds when I use my zebra crossing code I count the blinks for the seconds. In my traffic lights code I start with both lights on red and then go throw the sequence. I also have a longer wait on green to let the traffic go thru. I don't how to change your code I don't understand it to get it working how I want it to. I also need to add a button and tone for people to cross. I have all this working in my master code on my Arduino MEGA. I don't use simulation software I use the UNO to test code when the code is running right I add it to my master code. When I have finish all the code the MEGA is the only board I will use. It's taking me four years to get to this stage and I thought my code will help others. I have done a lot of testing code breaking code and loosing code in my mind and on computers losing data with no backups trying to remember all the different ways to code and all the website links. When I started this project I was going to add links to the websites where I found the different code but it got out of hand with so many sites and some links not working. Many thanks Mike.

User avatar
TimberSurf
Posts: 2107
Joined: Wed Jan 08, 2014 5:47 pm
Location: N.Wales
Contact:

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby TimberSurf » Sun Jul 08, 2018 4:31 pm

Mike, don't worry about the binary version, it was more about testing the theory of shortening the code, and seems not worth it.
In the normal code, the state changes every second, from 0 to 28.
The timing was set by UK nominal timings I got from the DoT.
The timing is defined as the difference between the last step and the next. Unfortunately, that means to adjust it, if you adjust one step by 1, then you need to adjust every step after it by adjusting by 1. (not the easiest of methods for tweeking, but very efficient in terms of code, because state and time are one function)
I would not advocate changing your master code, it is perfectly OK and works! (Unless you want to add more code and are running out of space)
Just watching the video (I don't know the age of it compared to the Master version), you seemed to have a problem. (hand of god). Is this resolved now?
It is hard to completely understand your master code, as we don't know what hardware it is attached too, but would be happy to go through it with you on here or PM if you want some help. (redundant code, duplicate code, better descriptors,etc)

Your code IS useful for others (saves me looking up how to program welding :D ) and is nicely broken up into chunks people can grab
Image
Lumsdonia <--- Hit link to go to my website for full story and wiring advice!

MikeTrymTrains
Posts: 14
Joined: Mon Jul 02, 2018 9:58 pm

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby MikeTrymTrains » Sun Jul 08, 2018 5:30 pm

Hi TimberSurf all the code I was posting work. I put them here to help others to grab bits of code and get things working straight away. I have no problems with my master code I put it there as an example of what can be done. It's runs servos without PWM for the railway crossing barriers, relays to power three solenoid to stop the trucks and bus and move the train forward and backward with 12 volts, piezo speaker for the traffic lights crossing and a toy two tone speaker thing I found when out walking that I use with a transistor to give more volts to use it and I will be adding more code for other things. The only problem I have is how to explain it to new people. I have a rats nest of wires on my table and not able to explain it to others but it works. :D

User avatar
TimberSurf
Posts: 2107
Joined: Wed Jan 08, 2014 5:47 pm
Location: N.Wales
Contact:

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby TimberSurf » Sun Jul 08, 2018 7:48 pm

Hi Mike
Perhaps you can use this fritzing to demo your hardware
One of my next investigations, is to use a MP3 shield to play sounds, I could do it by I/O to sound pcb's I already have here, but I am sure it would be better integrated via a TF Card U Disk MP3 Player Audio Voice Module.
Image
Lumsdonia <--- Hit link to go to my website for full story and wiring advice!

User avatar
roganty
Posts: 130
Joined: Wed Feb 17, 2016 11:53 am
Location: Bristol, UK
Contact:

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby roganty » Sun Jul 08, 2018 11:03 pm

TimberSurf wrote:OK, playing with binary intrigued me, so I had a play. In this case it does knock about 40 lines off the code (so not just 30!,more like 80 total so we saved a third) but it now makes it much harder to understand. If we were trying to drive hundreds of outputs, then it would save a massive amount of code, but the arduino won't support more than 20 odd so not really worth the effort. Enjoy the comparison...


Timbersurf,

I have only been lightly following this thread, but this snippet of code has caught my attention.

This bit of code seems inefficient.

Is there a reason each bit is read, stored and then written to the pin on every iteration of the loop?

Edit
I was looking at placing the long list of if...else statements within a while() loop, but I couldn't work it all out.

(Edited) What I would do is replace all the if...else statements with a switch...case statement.

Code: Select all

//Two direction Traffic Lights
// using direct binary read bits to outputs
// only the first (rightmost) bits of the Byte are read

unsigned long Tcurrent = 0;    // stores the value of millis() in each iteration of loop() this is equivalent to noting the time from a clock
unsigned long Tlapseold = 0; // the time when the last state was set
unsigned long Tlapse = 1000; // the time between each state change

//Declaring the LED and button pins and initial state
bool EWgreen = LOW;
bool EWyellow = LOW;
bool EWred = LOW;
bool NSgreen = LOW;
bool NSyellow = LOW;
bool NSred = LOW;
int Arraysix = 0;
int state = 0;

void setup()
{
   //Declaring the functioning of pins to outputs
   pinMode(2, OUTPUT); 
   pinMode(3, OUTPUT);
   pinMode(4, OUTPUT);
   pinMode(5, OUTPUT); 
   pinMode(6, OUTPUT);
   pinMode(7, OUTPUT);
}

void loop(){
Tcurrent = millis();   // capture the latest value of millis()
  if (Tcurrent - Tlapseold >= Tlapse) {  // check if the lapse time has elasped
    Tlapseold = Tcurrent;  // reset the last time the state as changed
    state = state + 1;  //increment the state by 1
  }
// store the bit status of the lights as and integer "Arraysix" for each state
   switch (state)
   {
   case 1:    //EW Green,  NS Red
      Arraysix = 33;  //bin = [00]100001
          break;
   case 11:    //EW Yellow,  NS Red   
      Arraysix = 34;  //bin = [00]100010
      break;
   case 14:    //EW Red,  NS Red   
      Arraysix = 36;  //bin = [00]100100
      break;
   case 16:    //EW Red,  NS Red/Amber   
      Arraysix = 52;  //bin = [00]110100
      break;
   case 18:    //EW Red,  NS Green   
      Arraysix = 12;  //bin = [00]001100
      break;
   case 22:    //EW Red,  NS Yellow   
      Arraysix = 20;  //bin = [00]010100
      break;
   case 25:    //EW Red,  NS Red   
      Arraysix = 36;  //bin = [00]100100
      break;
   case 27:   //EW Red/Amber,  NS Red   
      Arraysix = 38;  //bin = [00]100110
      break;
   case 28:  //return to begining
      state = 0;
      break;
   }
   

// read each bit and store in the bool variables

EWgreen = bitRead(Arraysix, 1);
EWyellow = bitRead(Arraysix, 2);
EWred = bitRead(Arraysix, 3);
NSgreen = bitRead(Arraysix, 4);
NSyellow = bitRead(Arraysix, 5);
NSred = bitRead(Arraysix, 6);

// write the bool variables to the pins

    digitalWrite(2,EWgreen);
    digitalWrite(3,EWyellow);
    digitalWrite(4,EWred);
    digitalWrite(5,NSgreen);
    digitalWrite(6,NSyellow);
    digitalWrite(7,NSred);
}
Main Layout: Planning | The Build

Erikslund - a small shunting layout

User avatar
roganty
Posts: 130
Joined: Wed Feb 17, 2016 11:53 am
Location: Bristol, UK
Contact:

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby roganty » Sun Jul 08, 2018 11:09 pm

roganty wrote:Edit
I was looking at placing the long list of if...else statements within a while() loop, but I couldn't work it all out.


Got it!

Replace this:

Code: Select all

Tcurrent = millis();   // capture the latest value of millis()
  if (Tcurrent - Tlapseold >= Tlapse) {  // check if the lapse time has elasped
    Tlapseold = Tcurrent;  // reset the last time the state as changed
    state = state + 1;  //increment the state by 1
  }


With this:

Code: Select all

while( Tcurrent = millis() ){   // capture the latest value of millis()
   if (Tcurrent - Tlapseold >= Tlapse) {  // check if the lapse time has elasped
      Tlapseold = Tcurrent;  // reset the last time the state as changed
      state = state + 1;  //increment the state by 1
      break; //Get out of the loop
   }
}
Main Layout: Planning | The Build

Erikslund - a small shunting layout

User avatar
TimberSurf
Posts: 2107
Joined: Wed Jan 08, 2014 5:47 pm
Location: N.Wales
Contact:

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby TimberSurf » Sun Jul 08, 2018 11:32 pm

Roganty, ok two things here
One - what is in inefficient? I left the six variables in so its still understandable!
To shorten the code, I would just write to pins direct from Arraysix, and maybe add a 'if Arraysix changes', 'write to pins', but I am not sure whats wrong with writing all six every loop iteration?

Two - should you use switch...case, yes, it's usually a shorter code than if...else, but is constrained to using 1 variable, in most cases that works, but on more complex jump calls, it has limited capabilities. (three way splits, loops in loops, etc)

And third - your last post, yep that principle would do it, but can you use break in a while?
Image
Lumsdonia <--- Hit link to go to my website for full story and wiring advice!

User avatar
roganty
Posts: 130
Joined: Wed Feb 17, 2016 11:53 am
Location: Bristol, UK
Contact:

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby roganty » Sun Jul 08, 2018 11:47 pm

TimberSurf wrote:Roganty, ok two things here
One - what is in inefficient? I left the six variables in so its still understandable!
To shorten the code, I would just write to pins direct from Arraysix, and maybe add a 'if Arraysix changes', 'write to pins', but I am not sure whats wrong with writing all six every loop iteration?


I don't "do" Arduino, so things may be different, but I was looking at the loop, and on every loop there is a 'write to pins' - That's what seemed inefficient

TimberSurf wrote:Two - should you use switch...case, yes, it's usually a shorter code than if...else, but is constrained to using 1 variable, in most cases that works, but on more complex jump calls, it has limited capabilities. (three way splits, loops in loops, etc)

True, and for those use if...else.

I think you can use AND by omitting the break

Code: Select all

switch( test ){
   case 1:
   case 2:
   case 3:
      //do something
      break;
   case 4:
      //do something
      break;
   default:
      //do something
}


TimberSurf wrote:And third - your last post, yep that principle would do it, but can you use break in a while?

Yep - break

Just thought - how about using a continue?
This will only work if break/continue works with the loop() function

Code: Select all

void loop(){
   Tcurrent = millis();   // capture the latest value of millis()
   if (Tcurrent - Tlapseold >= Tlapse) {  // check if the lapse time has elasped
      Tlapseold = Tcurrent;  // reset the last time the state as changed
      state = state + 1;  //increment the state by 1
   }else{
      continue; //skip back to the beginning
   }
     /*
        Do stuff
     */
} //void loop()
Main Layout: Planning | The Build

Erikslund - a small shunting layout

MikeTrymTrains
Posts: 14
Joined: Mon Jul 02, 2018 9:58 pm

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby MikeTrymTrains » Mon Jul 09, 2018 9:13 pm

TimberSurf wrote:Hi Mike
Perhaps you can use this fritzing to demo your hardware
One of my next investigations, is to use a MP3 shield to play sounds, I could do it by I/O to sound pcb's I already have here, but I am sure it would be better integrated via a TF Card U Disk MP3 Player Audio Voice Module.


Hi TimberSurf when I first looked at adding sound I thought about using a MP3 shield they look good but I thought a bit expensive but i'm a cheapskate that's why I using the £5 Arduino so I don't have to pay £10 to buy a fire effect and arc welder effect. But I have already spent £20 :D . Two UNO, one MEGA ,lots of leds and three relay's boards. I'm saving £150 on the faller protected level crossing and £70 on the faller traffic lights control system the Arduino can do it all. I like the four point car lift you made on your youtube channel very good keep up the good work. I've added the fritzing to all my posts that had the code I made I hope it explain it more better.

User avatar
TimberSurf
Posts: 2107
Joined: Wed Jan 08, 2014 5:47 pm
Location: N.Wales
Contact:

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby TimberSurf » Mon Jul 09, 2018 9:49 pm

Your not as bigger cheapskate than I am! I have never paid more than £2 for either an Arduino or a shield! :lol:
I have yet to play with the MP3 shield (and Canbus shields). I will of course post here and on my website once I do. Too many other projects on the go. :)
Image
Lumsdonia <--- Hit link to go to my website for full story and wiring advice!

timbologist
Posts: 362
Joined: Wed Sep 18, 2013 6:39 am
Location: Hazeldene Victoria Australia ( in the bush )

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby timbologist » Tue Jul 10, 2018 6:21 am

Hi all, it's good to see you all bouncing ideas of each other.
I was away with my son at the Round 3 of the Asian Onroad Championships ( Electric Remote Control Cars ) held in Melbourne over the Weekend so I missed this discussion. It was amazing to watch especially Former World Champions, including Andy Moore from the UK.

As can be seen from the discussion there are many different ways to achieve the same result, everybody writes differently and are different levels of knowledge-ability.

So I thought I would throw my two bobs worth anyway, and write the code as I do for my personal projects and don't worry about others following.
As can be seen it is very short, fast and efficient, And could be classed as over kill for what it is required to do.

So just remember to write your programs in a manner that you are comfortable with and can easily understand and follow.

Below is a comparison of the different programs lines of code and compiled size, one thing of interest is TimberSurf's programs the first one had 96 lines of code and compiled to 1218 bytes plus 11 bytes yet the second program with 73 lines compiled to 1254 bytes and 27 bytes.

So even when reducing the source code by changing the method of programming to give shorter source code the actual compiled code is actually larger.
Why is it so? Without going to deep into it, the compiler generates more code when you use one method even though the source code may be shorter. This can be due to the manner in which the compiler has to produce certain code, plus the amount of code required for house keeping.
If you want total control of your programme size and how fast it runs you have to write in assembler. Compiled programs it can be very difficult to control this.

MikeTrymTrains
106 lines of executable code only setup, loop
Sketch uses 1750 bytes (5%) of program storage space. Maximum is 32256 bytes.
Global variables use 15 bytes (0%) of dynamic memory, leaving 2033 bytes for local variables. Maximum is 2048 bytes.

TimberSurf 1st program

96 lines of executable code only setup, loop
Sketch uses 1218 bytes (3%) of program storage space. Maximum is 32256 bytes.
Global variables use 11 bytes (0%) of dynamic memory, leaving 2037 bytes for local variables. Maximum is 2048 bytes.

TimberSurf 2nd program
73 lines of executable code only setup, loop
Sketch uses 1254 bytes (3%) of program storage space. Maximum is 32256 bytes.
Global variables use 27 bytes (1%) of dynamic memory, leaving 2021 bytes for local variables. Maximum is 2048 bytes.

My program
26 lines of executable code only setup, loop, changeTheLights
Sketch uses 1138 bytes (3%) of program storage space. Maximum is 32256 bytes.
Global variables use 56 bytes (2%) of dynamic memory, leaving 1992 bytes for local variables. Maximum is 2048 bytes.


Code: Select all

// PROGRAM SET TRAFFIC-LIGHT-SKETCH
// FILE NAME TRAFFIC-LIGHT-SKETCH.ino
// NEW RAILWAY MODELLERS FORUM
// A.R.TURNER 2018-07-10

// PROGRAM TO SIMULATE UK TRAFFIC SIGNALS

#include <TimerOne.h>  // INCLUDE INTERRUPT TIMER LIBRARY

int nextStateNumber = 0;  // SET UP A VARIABLE FOR THE NEXT STATE NUMBER INITIALISED FOR FIRST STATE

// DEFINE AN ARRAY CONTAINING THE TRAFFIC LIGHT STATES

byte  lightStates[8] =
{            //    | nSRred | nSYellow | nSGreen | eWRed | eWYellow | eWGreen
  B00100001, //  1 |   ON   |   OFF    |   OFF   |  OFF  |    OFF   |  ON
  B00100010, // 11 |   ON   |   OFF    |   OFF   |  OFF  |    ON    |  OFF
  B00100100, // 14 |   ON   |   OFF    |   OFF   |  ON   |    OFF   |  OFF
  B00110100, // 16 |   ON   |   ON     |   OFF   |  ON   |    OFF   |  OFF
  B00001100, // 18 |   OFF  |   OFF    |   ON    |  ON   |    OFF   |  OFF
  B00010100, // 22 |   OFF  |   ON     |   OFF   |  ON   |    OFF   |  OFF
  B00100100, // 25 |   ON   |   OFF    |   OFF   |  ON   |    OFF   |  OFF
  B00100110, // 27 |   ON   |   ON     |   OFF   |  ON   |    ON    |  OFF
};

// DEFINE AN ARRAY THAT CONTAINS THE TIMES BETWEEN EACH LIGHT STATE

long mSDelaySteps[8] =
{
  10000000UL, // 10 SECOND DELAY BETWEEN FIRST AND SECOND STATE
   4000000UL, //  4 SECOND DELAY BETWEEN SECOND AND THIRD STATE
   2000000UL, //  2 SECOND DELAY BETWEEN THIRD AND FORTH STATE
   2000000UL, //  2 SECOND DELAY BETWEEN FORTH AND FIFTH STATE
   4000000UL, //  4 SECOND DELAY BETWEEN FIFTH AND SIXTH STATE
   3000000UL, //  3 SECOND DELAY BETWEEN SIXTH AND SEVENTH STATE
   2000000UL, //  2 SECOND DELAY BETWEEN SEVENTH AND EIGHTH STATE
   1000000UL, //  1 SECOND DELAY BETWEEN EIGHTH AND FIRST STATE
};


void setup()
{
Timer1.initialize(mSDelaySteps[nextStateNumber]);  // INITIALISE TIMER1 INITIALISED FOR FIRST STATE
Timer1.attachInterrupt(changeTheLights);           // ATTACH THE TIMER1 INTERRUPT TO CALL changeTheLights INTERRUPT ROUTINE EVERY INTERRUPT
DDRB = B00111111;                                  // DOING A WRITE TO THE DATA DIRECTION REGISTER PORTB SETTING  ALL PINS AS OUTPUTS 
PORTB  = lightStates[0];                           // SET THE TRAFFIC LIGHTS TO THE FIRST STATE
}

void loop()
{
// CONTAINS NOTHING
}

// INTERRUPT ROUTINE TO CHANGE LIGHT STATE WHEN TIMER I9NTERUPT OCCURS

void changeTheLights(void)
{
  PORTB  = lightStates[nextStateNumber];            // SET THE TRAFFIC LIGHTS TO THE NEXT STATE ,DIRECT PORT WRITE
  Timer1.initialize(mSDelaySteps[nextStateNumber]); // SET THE TIMER INTERRUPT FOR THE DELAY FOR THE NEXT STATE CHANGE
  nextStateNumber = nextStateNumber + 1;            // INCREMENT THE THE NEXT STATE COUNTER

  if (nextStateNumber > 7 )                         // CHECK THE NEXT STATE COUNTER FOR BEING AT LAST STATE
    {                                               // IF LAST STATE HAS OCCURRED THEN RESET TO 0 TO START OVER AGAIN
      nextStateNumber = 0;                         
    }
}


User avatar
TimberSurf
Posts: 2107
Joined: Wed Jan 08, 2014 5:47 pm
Location: N.Wales
Contact:

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby TimberSurf » Tue Jul 10, 2018 8:12 am

Thanks for that Timbologist, I knew there was a way of crushing it down further! But it is very interesting how different code can do the same thing and be very different. It is also extremely ironic that the crushed code takes up more space!
It points me to two thoughts.
Timbersurfs Law is highly applicable
Traffic lights is always a great learners tool to show that there are a dozen ways of programming it (it's always the first exercise you get, if you go on a course to learn PC, embedded or PLC code)
I can draw a few conlusions from this:-
Code the way you understand
Put in lots of notes (they get removed when you compile so take up no space) {it helps you galvanise your thoughts, understand the code when you come back months later and when others looking at it}
Use indirect addressing (make everything a variable, so it's easy to tweak after and understandable) {something we use regularly in PLC land}
Don't worry about the length of code, until you get to a very advance stage, the Arduino's limits of I/O means its memory has huge headroom.
Image
Lumsdonia <--- Hit link to go to my website for full story and wiring advice!

timbologist
Posts: 362
Joined: Wed Sep 18, 2013 6:39 am
Location: Hazeldene Victoria Australia ( in the bush )

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Postby timbologist » Tue Jul 10, 2018 9:55 am

This is probably one of the most important things is to comment your code well there is no such thing as over commenting.
TimberSurf wrote:Put in lots of notes


TimberSurf wrote:Use indirect addressing (make everything a variable, so it's easy to tweak after and understandable) {something we use regularly in PLC land}

Now this can cause a problem if you use over 75% of the ram for variables you end up with instability problems, programs will compile and load and you get a warning about it. depending on what your program is doing it may not run as it should.

My code for my hand controller had this problem, I built the mock-up with a Mega 256k flash and 8k ram, the finished item I planned to use a Mini Pro 32k flash and 2k ram. The problem arises when you have large amounts of text you write to the LCD, even though this text is constant and does not change, the compiler by default treats it as a variable because it is data and places it into ram by default. But luckily there is a compiler " = PROGMEM " directive that allows data that is constant and will only be read only to be placed into the flash memory with the actual program. this resolves the problem.
The actual program size is smaller by nearly 1.5K also from doing this, which is because of less house keeping required because of the change in location of the data.
And one thing that happens when the Arduino is first turned on is it does some other initialisation that is built into the start-up process. That is all the variables and there initial data are stored in an area of the flash with the program and must be transferred to ram each time the unit is powered up or reset. So this data is read/writeable ( become a variable ) and not a constant as would be if stored in the flash at runtime. So there is a lot that happens under the hood that you normally don't know about.

Compiled for Mega
Sketch uses 13872 bytes (5%) of program storage space. Maximum is 253952 bytes.
Global variables use 2120 bytes (25%) of dynamic memory, leaving 6072 bytes for local variables. Maximum is 8192 bytes.

compiled for UNO using program memory for screen text constants
Sketch uses 12480 bytes (38%) of program storage space. Maximum is 32256 bytes.
Global variables use 1532 bytes (74%) of dynamic memory, leaving 516 bytes for local variables. Maximum is 2048 bytes.


Return to “Electronics”

Who is online

Users browsing this forum: No registered users and 2 guests