ARDUINO PROGRAMMING, AND INTERFACING HELP

Basic electrical and electronics, such as DC/Analog control.
Paul-H
Posts: 244
Joined: Fri Jun 05, 2009 3:00 pm

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Post by Paul-H »

Video removed for violating YouTubes terms

What could this video have shown that upset YouTube so much given the bile they often let through :shock:
timbologist
Posts: 372
Joined: Wed Sep 18, 2013 6:39 am
Location: Hazeldene Victoria Australia ( in the bush )

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Post by timbologist »

I have no idea why this has been removed it must have got removed from the time i loaded it on YouTube to the Time I posted on the forum as I tried the link and got what you got. :shock: :?: :!: It must have been because it had the word flashing in it :wink:
I have already submitted an appeal against it so will have to see if they are as quick to delete it, to give me a reply.
So until then I am sorry about not having the video for you to see.
timbologist
Posts: 372
Joined: Wed Sep 18, 2013 6:39 am
Location: Hazeldene Victoria Australia ( in the bush )

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Post by timbologist »

After have problems with YouTube with my Video I have loaded it onto Vimeo so see what happens here.
https://vimeo.com/269534670
So far so good :wink:
timbologist
Posts: 372
Joined: Wed Sep 18, 2013 6:39 am
Location: Hazeldene Victoria Australia ( in the bush )

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Post by timbologist »

PROJECT 1
STAGE 2
PART A.

As with with stage 1 I have broken this up into 2 parts Software and Hardware as they seem to get vary large quickly.

As can be seen from the new Schematic and the breadboard I have added 2 Push Button Switches and 1 Toggle Switch plus 3 10k resisters that are used to pull the pins to ground when the switch is open circuit this prevents the input from floating and picking up noise that may effect the program.

Note The Arduino has built in pull-up and pull-down resisters that can be set using software, but at this stage I would prefer to show using external components

The Push Button Switch on Pin 4 will be used to turn the flasher on and the Push Button Switch on Pin 5 will be used to turn the flasher off. The toggle switch on Pin 6 will be used to override the Push Button Switches, when turned on the flashers will flash and the Push buttons will have no effect.

Now one problem that arises when using mechanical switches with Microcontrollers is that when the button is pressed the contacts will close then open again very quickly, and keep repeating this cycle until they settle down and stop. They act a bit like a spring coming to rest. Because of this the Microcontroller has a problem working out if the switch is open or closed. There are 2 methods to solve this problem one is in hardware, by adding a resister and capacitor network we can remove these spikes. Below is a couple of links about switches and denouncing. The second link has an oscilloscope capture of a switch closing.
https://www.embeddedflakes.com/2016/12/ ... ebouncing/
https://en.wikipedia.org/wiki/Debounce


But for this project I will show you the software method of dealing with switch de-bouncing.
So for now will leave you and will be back with Stage 2 Part B
PROJECT 01 STAGE 02 BREADBOARD LAYOUT
PROJECT 01 STAGE 02 BREADBOARD LAYOUT
PROJECT 01 STAGE 02 SCHEMATIC DIAGRAM
PROJECT 01 STAGE 02 SCHEMATIC DIAGRAM
timbologist
Posts: 372
Joined: Wed Sep 18, 2013 6:39 am
Location: Hazeldene Victoria Australia ( in the bush )

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Post by timbologist »

Tonight I received a reply about my appeal to YouTube about the Video of the Crossing Flasher being taken down. Below is there reply
Dear timbologist:
Thank you for submitting your video appeal to YouTube. After further review, we've determined that your video doesn't violate our Community Guidelines. Your video has been reinstated and your account is in good standing.
Sincerely,
— The YouTube Team
What can I say but I will stick with Vimeo as it will probably happen again.
timbologist
Posts: 372
Joined: Wed Sep 18, 2013 6:39 am
Location: Hazeldene Victoria Australia ( in the bush )

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Post by timbologist »

As you can see from the program listing the program has grown a lot and is a lot more complex.
Those who are experienced programmers will note that there is a lot of redundant code in here that needlessly executes each time through the loop.
I have done this to make the program less complicated for those who are learning and trying to follow along.
I will put a video up on Vimeo to show this stage in operation.
As I have not done that yet, so will post the link when I have done so.

Code: Select all

// PROGRAM SET PROJECT-01-STAGE-02
// FILE NAME PROJECT-01-STAGE-02.ino
// NEW RAILWAY MODELLERS FORUM
// ARDUINO PROGRAMMING, AND INTERFACING HELP
// PROJECT 01
// LEVEL CROSSING FLASHING LIGHTS USIND AN ARDUINO
// A.R.TURNER 2018-05-18

// 2018-05-18
// THIS IS THE SECOND STAGE OF THIS PROJECT WHERE I HAVE ADDED
// 1 PUSH BUTTON TO TURN THE LIGHTS ON
// 1 PUSH BUTTON TO TURN THE LIGHTS OFF
// 1 TOGGLE SWITCH TO OVERIDE THE PUSH BUTTONS

// SET UP OUR PINS CONECTED TO THE LEDS
// WE NEED TO SET UP OUR PIN ASSIGNMENTS OUTSIDE EITHER THE SETUP SECTION OR THE LOOP SECTION
// SO THEY BECOME GLOBAL VARIABLES MEANING THEY CAN BE ACCESSED ANYWHERE IN THE PROGRAM
// IF WE DEFINE THEM INSIDE THE SETUP SECTION OF THE LOOP SECTION THEY BECOME LOCAL VARIABLES
// WHICH MEANS THEY CAN ONLY BE ACCESSESD WITHIN THE BOUNDS OF THAT SECTION
// IN SOME PROGRAMS YOU WILL SEE THESE ASSIGNMENT IN DIFFERENT FORMS IE
// const int redLedTopLeftBottomRight  = 2;
// OR 
// int redLedTopLeftBottomRight  = 2;
// BOTH DO THE SAME THING ACCEPT THE ONE WITH THE "const" IN FRONT OF IT CREATES THE VARIABLE AS READ ONLY
// SO IT CANNOT BE CHANGED ANYWHERE IN THE PROGRAM.


const int redLedTopLeftBottomRight  = 2;  // OUTPUT PIN CONECTED TO TOP LEFT AND BOTTOM RIGHT LED
const int redLedTopRightBottomLeft  = 3;  // OUTPUT PIN CONECTED TO TOP RIGHT AND BOTTOM LEFT LED

// 2018-05-18 START OF STAGE 2 ADDITIONS

const int pushButtonLightsOn = 4;   // PIN THAT THE LIGHTS ON BUTTON IS CONNECTED T0
const int pushButtonLightsOff = 5;  // PIN THAT THE LIGHTS OFF BUTTON IS CONNECTED T0
const int toggleSwitchOveride = 6;  // PIN THAT THE OVERIDE SWITCH IS CONNECTED T0

int buttonReadVal01;          // VARIABLE TO STORE THE FIRST READ WHEN WE DEBONCE THE INPUTS
int buttonReadVal02;          // VARIABLE TO STORE THE SECOND READ WHEN WE DEBONCE THE INPUTS
int buttonReadLightsOn;       // VARIABLE TO STORE THE RESULT OF THE READ OF THE LIGHTS ON BUTTON PIN
int buttonReadLightsOff;      // VARIABLE TO STORE THE RESULT OF THE READ OF THE LIGHTS ON BUTTON PIN
int toggleSwitchReadOveride;  // VARIABLE TO STORE THE RESULT OF THE READ OF THE OVERIDE SWITCH PIN

bool lightsOn= false;  // THE FLAG THAT IS SET WHEN THE LIGHTS ON BUTTON IS PRESSED AND CLEARED CLEARED WHEN THE LIGHTS OFF BUTTON HAS BEEN PRESSED
bool overRide = false; // THE FLAG THAT IS SET WHEN THE OVERIDE SWITCH IS OF

// 2018-05-18 END OF STAGE 2 ADDITIONS

// THE SETUP PART OF THE PROGRAM IS ONLY EXECUTED ONCE WHEN THE ARDUINO IS POWERED UP OR THE
// RESET BUTTON IS PRESSED IN THIS SECTION WE SET UP OUR PINS AS OUTPUTS AND ENABLE THE SERIAL PORT
// IN THIS PROGRAM WE DO NOT USE THE SERIAL PORT AT THIS STAGE

void setup()
{
  // put your setup code here, to run once:
  
  Serial.begin(9600);   // SET UP BAUD RATE TO BE ABLE TALK TO COMPUTER VIA USB PORT COMPUTER 

  pinMode(redLedTopLeftBottomRight,OUTPUT); // SET THE PIN CONECTED TO TOP LEFT AND BOTTOM RIGHT LED AS AN OUTPUT
  pinMode(redLedTopRightBottomLeft,OUTPUT); // SET THE PIN CONECTED TO TOP RIGHT AND BOTTOM LEFT LED AS AN OUTPUT

// 2018-05-18 START OF STAGE 2 ADDITIONS

  pinMode(pushButtonLightsOn,INPUT); // SET THE PIN CONECTED TO THE TURN LIGHTS ON BUTTON AS INPUT
  pinMode(pushButtonLightsOff,INPUT); // SET THE PIN CONECTED TO THE TURN LIGHTS OFF BUTTON AS INPUT
  pinMode(toggleSwitchOveride,INPUT); // SET THE PIN CONECTED TO THE TURN LIGHTS ON BUTTON AS INPUT  

// 2018-05-18 END OF STAGE 2 ADDITIONS  
}

// THE LOOP SECTION AS IT SAYS CREATES THE MAN PROGRAM LOOP THAT WILL JUST KEEP RUNNING TILL YOU TURN THE ARDUINO OFF
// SO IN THIS LOOP THERE IS 2 "digitalWrite's". THE FISRT "digitalWrite" TURNS ONE PAIR OF LEDS ON BY SETTING THE OUTPUT HIGH THAT IS TO 5 VOLTS 
// THE SECOND "digitalWrite" TURNS THE SECOND PAIR OF LEDS OFF BY SETTING THE OUTPUT LOW O VOLTS.
// THE PROGRAM THEN WAITS FOR A "DELAY(750)" WHICH IN THIS CASE IS SET FOR 750 MILLISECONDS OF 0.75 SECONDS SO THE LEDS STAY ON OR OFF FOR THIS PERIOD OF TIME.
// AFTER THE DELAY THE NEXT 2 "digitalWrite's" CHANGE THE STATE OF THE 2 OUTPUTS THE LEDS THAT WERE ON ARE NOW TURNED OF
// THE PROGRAM THEN WAITS WITH A "DELAY(750)" KEEPING THE LEDS ON OF OFF FOR THIS PERIOD THE THE PROGRAM GOES BACK TO THE FIRST PAIR OF "digitalWrite's"
// AND STARTING ALL OVER AGAIN
//
// 2018-05-18 START OF STAGE 2 ADDITIONS
// THE WHOLE LOOP PART OF THE PROGRAM HAS BEEN CHANGED WITH THE ADDITION OF THE READING OF THE BUTTONS AND THE SWITCH
// PLUS THERE IS NOW SOME LOGIC ADDED THAT DECIDES WHICH PARTS OF THE PROGRAM ARE TO BE EXECUTED OR NOT EXECUTED
// DEPENDING WHEN CERTAIN CONDITIONS ARE MET.
// THE ADDITION OF SWITCH BEBOUNCE ROUTINES HAVE BEEN ADDED


void loop()
{
    buttonReadLightsOn =  digitalRead(pushButtonLightsOn);      // READ THE PIN CONNECTED TO THE START BUTTON
    buttonReadLightsOff = digitalRead(pushButtonLightsOff);     // READ THE PIN CONNECTED TO THE START BUTTON
    toggleSwitchReadOveride = digitalRead(toggleSwitchOveride); // READ THE PIN CONNECTED TO THE START BUTTON
    
 // put your main code here, to run repeatedly:
  if ( !overRide ) // WE CHECK TO SEE IF THE OERIDE SWITCH IS OFF IF OFF WE ARE ALLOWED TO CHECK THE ON OFF BUTTONS
  
    { 
      if (buttonReadLightsOn == 1) // IF THE ON BUTTON IS PRESSED WE GO AND DEBOUNCE THE BUTTON
        {
          
          for (int count=0;count<5;) 
            {
              buttonReadVal01 = digitalRead(pushButtonLightsOn);  // READ THE PIN AND SAVE IT
              delay(5);                                           // DELAY BETWEEN READS
              buttonReadVal02 = digitalRead(pushButtonLightsOn);  // RE-READ THE PIN AND SAVE IT
              if (buttonReadVal01 == buttonReadVal02)             // CHECK TO SEE IF BOTH READS ARE THE SAME
                {
                  count++; // INCREMENT THE COUNTER AS WE HAVE HAD A GOOD READ
                }
             }
        lightsOn = true; // SET THE FLAG THAT WE WANT THE LIGHTS TO FLASH
        }

        if (buttonReadLightsOff == 1) // IF THE OFF BUTTON IS PRESSED WE GO AND DEBOUNCE THE BUTTON
          {
            for (int count=0;count<5;) // LOOP 5 TIMES TO CHECK BUTTONS STATE
              {
                buttonReadVal01 = digitalRead(pushButtonLightsOff);  // READ THE PIN AND SAVE IT
                delay(5);                                            // DELAY BETWEEN READS
                buttonReadVal02 = digitalRead(pushButtonLightsOff);  // RE-READ THE PIN AND SAVE IT
                if (buttonReadVal01 == buttonReadVal02)              // CHECK TO SEE IF BOTH READS ARE THE SAME
                  {
                    count++; // INCREMENT THE COUNTER AS WE HAVE HAD A GOOD READ
                  }
              }
       lightsOn = false; // SET THE FLAG THAT WE WANT THE LIGHTS TO FLASH
       digitalWrite(redLedTopRightBottomLeft, LOW);  // WRITE A LOGIC LOW TO THIS PIN TURNS THE LEDS OFF
       digitalWrite(redLedTopLeftBottomRight, LOW);  // WRITE A LOGIC LOW TO THIS PIN TURNS THE LEDS OFF
       }
    }
    
   if (toggleSwitchReadOveride == 1) // IF THE TOGGLE SWITCH IS ON WE GO AND DEBOUNCE THE SWITCH
     {
       
    for (int count=0;count<5;) // LOOP 5 TIMES TO CHECK BUTTONS STATE
      {
        buttonReadVal01 = digitalRead(toggleSwitchOveride);  // READ THE PIN AND SAVE IT
        delay(5);                                            // DELAY BETWEEN READS
        buttonReadVal02 = digitalRead(toggleSwitchOveride);  // RE-READ THE PIN AND SAVE IT
        if (buttonReadVal01 == buttonReadVal02)              // CHECK TO SEE IF BOTH READS ARE THE SAME
          {
            count++; // INCREMENT THE COUNTER AS WE HAVE HAD A GOOD READ
          }
       }
  overRide =   true; // SET THE FLAG THAT WE WANT THE LIGHTS TO FLASH AND IGNORE THE PUSH BUTTONS
     }
    else // IF THE THE SWITCH IS OFF WE DO THIS INSTEAD
    {
    overRide =   false; // SET THE FLAG THAT WE WANT THE LIGHTS TO TURN OFF AND WE CAN READ THE PUSH BUTTONS
    digitalWrite(redLedTopRightBottomLeft, LOW);  // WRITE A LOGIC LOW TO THIS PIN TURNS THE LEDS OFF
    digitalWrite(redLedTopLeftBottomRight, LOW);  // WRITE A LOGIC LOW TO THIS PIN TURNS THE LEDS OFF
    }

  if ( overRide   || lightsOn )// IF THE OVERIDE FLAG OR THE LIGHTS ON FLAG IS SET WE FLASH THE LIGHTS
  {
  digitalWrite(redLedTopLeftBottomRight, HIGH); // WRITE A LOGIC HIGH TO THIS PIN TURNS THE LEDS ON
  digitalWrite(redLedTopRightBottomLeft, LOW);  // WRITE A LOGIC LOW TO THIS PIN TURNS THE LEDS OFF
  delay(750);
  digitalWrite(redLedTopLeftBottomRight, LOW);  // WRITE A LOGIC LOW TO THIS PIN TURNS THE LEDS OFF
  digitalWrite(redLedTopRightBottomLeft, HIGH); // WRITE A LOGIC HIGH TO THIS PIN TURNS THE LEDS ON
  delay(750);
  }
}
timbologist
Posts: 372
Joined: Wed Sep 18, 2013 6:39 am
Location: Hazeldene Victoria Australia ( in the bush )

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Post by timbologist »

Video is now up on Vimeo.
You will notice that there is a bit of a delay from when the button is pressed to when the lights start or stop flashing, this is because if the program is in the section of the program where the delay is, it takes awhile before it goes and reads the switches. later on I will show how to improve on the program to solve this problem by using interrupts to control the flashing so the loop is free to monitor the switches.
https://vimeo.com/269534508
User avatar
TimberSurf
Posts: 2537
Joined: Wed Jan 08, 2014 5:47 pm
Location: N.Wales
Contact:

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Post by TimberSurf »

You need a tripod! lol
Image
Lumsdonia <--- Hit link to go to my website for full story and wiring advice!
timbologist
Posts: 372
Joined: Wed Sep 18, 2013 6:39 am
Location: Hazeldene Victoria Australia ( in the bush )

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Post by timbologist »

Hello again
Welcome to
PROJECT 1
STAGE 3

The program is getting longer and more complex as it is having to deal with problems
from the outside world that happen to slowly for the Arduino to deal with.
So I have had to add a couple of while loops that do nothing except make the program
sit there waiting for the button to be released I this is not done the program gets back to reading the button again.
So treats it as though it is a new press, which gives the impression that the program is not working correctly.
Again the program should have enough comments for you to follow what is happening.
The video of this stage is on Vimeo at the below link, I did record it with voice but did not work when uploaded.
TimberSurf wrote:You need a tripod! lol
At least I now know there is one person reading this :)
This is a very very low budget production, I'd go find 3 sticks and use piece of string to make one but can't afford the string :wink:

Don't forget any problems or question please ask.

The next stage I will change to how to make the program more efficient and easier to follow.
So stage 5 will have the interfacing to actual sensors to turn the lights on and of by the train going past.

https://vimeo.com/271206254

Code: Select all

// PROGRAM SET PROJECT-01-STAGE-03
// FILE NAME PROJECT-01-STAGE-03.ino
// NEW RAILWAY MODELLERS FORUM
// ARDUINO PROGRAMMING, AND INTERFACING HELP
// PROJECT 01
// LEVEL CROSSING FLASHING LIGHTS USIND AN ARDUINO
// A.R.TURNER 2018-05-22

// 2018-05-22
// THIS IS THE THIRD STAGE OF THIS PROJECT WHERE I HAVE CHANGED 
// THE ACTION OF THE BUTTONS 
// FIRSTLY PUSH BUTTON A TURNS THE LIGHTS ON WHILE PUSH BUTTON B TURNS IT OFF
// OR PUSH BUTTON B TURNS THE LIGHTS ON WHILE PUSH BUTTON A TURNS IT OFF
// TOGGLE SWITCH TO STILL OVERIDES THE PUSH BUTTONS
// SOME VARIABLES HAVE BEEN RENAMED TO DESCRIBE THERE NEW USE.

// 2018-05-18
// THIS IS THE SECOND STAGE OF THIS PROJECT WHERE I HAVE ADDED
// 1 PUSH BUTTON TO TURN THE LIGHTS ON
// 1 PUSH BUTTON TO TURN THE LIGHTS OFF
// 1 TOGGLE SWITCH TO OVERIDE THE PUSH BUTTONS

// SET UP OUR PINS CONECTED TO THE LEDS
// WE NEED TO SET UP OUR PIN ASSIGNMENTS OUTSIDE EITHER THE SETUP SECTION OR THE LOOP SECTION
// SO THEY BECOME GLOBAL VARIABLES MEANING THEY CAN BE ACCESSED ANYWHERE IN THE PROGRAM
// IF WE DEFINE THEM INSIDE THE SETUP SECTION OF THE LOOP SECTION THEY BECOME LOCAL VARIABLES
// WHICH MEANS THEY CAN ONLY BE ACCESSESD WITHIN THE BOUNDS OF THAT SECTION
// IN SOME PROGRAMS YOU WILL SEE THESE ASSIGNMENT IN DIFFERENT FORMS IE
// const int redLedTopLeftBottomRight  = 2;
// OR 
// int redLedTopLeftBottomRight  = 2;
// BOTH DO THE SAME THING ACCEPT THE ONE WITH THE "const" IN FRONT OF IT CREATES THE VARIABLE AS READ ONLY
// SO IT CANNOT BE CHANGED ANYWHERE IN THE PROGRAM.


const int redLedTopLeftBottomRight  = 2;  // OUTPUT PIN CONECTED TO TOP LEFT AND BOTTOM RIGHT LED
const int redLedTopRightBottomLeft  = 3;  // OUTPUT PIN CONECTED TO TOP RIGHT AND BOTTOM LEFT LED

// 2018-05-21 START OF STAGE 3 CHANGES

// FIRST THING HAVE CHANGED THE NAMES OF THE BUTTONS AND THE VARIABLES
// AND ADDED A NEW FLAG THAT IS SET WHEN DIRECTION IS LEFT TO RIGHT AND IS CLEARED WHEN RIGHT TO LEFT 

const int pushButtonLeft = 5;       // PIN THAT THE LEFT BUTTON IS CONNECTED T0
const int pushButtonRight = 4;      // PIN THAT THE RIGHT BUTTON IS CONNECTED T0
const int toggleSwitchOveride = 6;  // PIN THAT THE OVERIDE SWITCH IS CONNECTED T0

int buttonReadVal01;                // VARIABLE TO STORE THE FIRST READ WHEN WE DEBONCE THE INPUTS
int buttonReadVal02;                // VARIABLE TO STORE THE SECOND READ WHEN WE DEBONCE THE INPUTS
int buttonReadButtonLeft;           // VARIABLE TO STORE THE RESULT OF THE READ OF THE LEFT BUTTON BUTTON PIN
int buttonReadButtonRight;          // VARIABLE TO STORE THE RESULT OF THE READ OF THE RIGHT BUTTON BUTTON PIN
int toggleSwitchReadOveride;        // VARIABLE TO STORE THE RESULT OF THE READ OF THE OVERIDE SWITCH PIN

bool lightsOn= false;               // THE FLAG THAT IS SET WHEN THE LIGHTS ARE FLASHING
bool overRide = false;              // THE FLAG THAT IS SET WHEN THE OVERIDE SWITCH IS ON
bool directionLeftToRight;          // THE FLAG THAT IS SET WHEN DIRECTION IS LEFT TO RIGHT AND IS CLEARED WHEN RIGHT TO LEFT 
bool directionRightToLeft;          // THE FLAG THAT IS SET WHEN DIRECTION IS RIGHT TO LEFT AND IS CLEARED WHEN LEFT TO RIGHT

// 2018-05-21 END OF STAGE 2 3 CHANGES

// THE SETUP PART OF THE PROGRAM IS ONLY EXECUTED ONCE WHEN THE ARDUINO IS POWERED UP OR THE
// RESET BUTTON IS PRESSED IN THIS SECTION WE SET UP OUR PINS AS OUTPUTS AND ENABLE THE SERIAL PORT
// IN THIS PROGRAM WE DO NOT USE THE SERIAL PORT AT THIS STAGE

void setup()
{
  // put your setup code here, to run once:
  
  Serial.begin(9600);   // SET UP BAUD RATE TO BE ABLE TALK TO COMPUTER VIA USB PORT COMPUTER 

  pinMode(redLedTopLeftBottomRight,OUTPUT); // SET THE PIN CONECTED TO TOP LEFT AND BOTTOM RIGHT LED AS AN OUTPUT
  pinMode(redLedTopRightBottomLeft,OUTPUT); // SET THE PIN CONECTED TO TOP RIGHT AND BOTTOM LEFT LED AS AN OUTPUT

// 2018-05-18 START OF STAGE 2 ADDITIONS

  pinMode(pushButtonLeft,INPUT); // SET THE PIN CONECTED TO THE TURN LIGHTS ON BUTTON AS INPUT
  pinMode(pushButtonRight,INPUT); // SET THE PIN CONECTED TO THE TURN LIGHTS OFF BUTTON AS INPUT
  pinMode(toggleSwitchOveride,INPUT); // SET THE PIN CONECTED TO THE TURN LIGHTS ON BUTTON AS INPUT  

// 2018-05-18 END OF STAGE 2 ADDITIONS  
}

// THE LOOP SECTION AS IT SAYS CREATES THE MAN PROGRAM LOOP THAT WILL JUST KEEP RUNNING TILL YOU TURN THE ARDUINO OFF
// SO IN THIS LOOP THERE IS 2 "digitalWrite's". THE FISRT "digitalWrite" TURNS ONE PAIR OF LEDS ON BY SETTING THE OUTPUT HIGH THAT IS TO 5 VOLTS 
// THE SECOND "digitalWrite" TURNS THE SECOND PAIR OF LEDS OFF BY SETTING THE OUTPUT LOW O VOLTS.
// THE PROGRAM THEN WAITS FOR A "DELAY(750)" WHICH IN THIS CASE IS SET FOR 750 MILLISECONDS OF 0.75 SECONDS SO THE LEDS STAY ON OR OFF FOR THIS PERIOD OF TIME.
// AFTER THE DELAY THE NEXT 2 "digitalWrite's" CHANGE THE STATE OF THE 2 OUTPUTS THE LEDS THAT WERE ON ARE NOW TURNED OF
// THE PROGRAM THEN WAITS WITH A "DELAY(750)" KEEPING THE LEDS ON OF OFF FOR THIS PERIOD THE THE PROGRAM GOES BACK TO THE FIRST PAIR OF "digitalWrite's"
// AND STARTING ALL OVER AGAIN
//
// 2018-05-18 START OF STAGE 2 ADDITIONS
// THE WHOLE LOOP PART OF THE PROGRAM HAS BEEN CHANGED WITH THE ADDITION OF THE READING OF THE BUTTONS AND THE SWITCH
// PLUS THERE IS NOW SOME LOGIC ADDED THAT DECIDES WHICH PARTS OF THE PROGRAM ARE TO BE EXECUTED OR NOT EXECUTED
// DEPENDING WHEN CERTAIN CONDITIONS ARE MET.
// THE ADDITION OF SWITCH BEBOUNCE ROUTINES HAVE BEEN ADDED
//
// 2018-05-21 STAGE 3 ADDITIONS
// THE ACTUAL BUTTON READING SECTIONS HAVE BEEN CHANGED
// SECTIONS HAVE BEEN ADDED TO BOTH BUTTONS TO CHECK THE ORDER OF THE BUTTON PRESSES 
// AND TO SET THE DIRECTION FOR WHICH BUTTON IS ON AND WHICH BUTTON BECOMES THE OFF BUTTON
// YOU WILL NOTICE A STRANGE LINE AT THE END OF BOTH BUTTON SECTIONS CONTAINING
// LEFT BUTTON "while(digitalRead(pushButtonLeft) == 1) // WAIT HERE TILL THE BUTTON IS RELEASED
//               {                                      // DO NOTHING WHILE IT WAITING
//               }
// THE REASON FOR THIS MAY NOT BE OBVIOUS BUT THE SPEED THAT THE ARDUINO RUNS THROUGH THE PROGRAM
// ESPECIALLY WHEN THE LIGHTS ARE OFF IS MICROSECONDS SO WITHOUT THIS WHEN THE OFF BUTTON IS PRESSED
// THE THE PROGRAM IS BACK READING THE SWITCH AGAIN BECAUSE IT IS STILL PRESSED THEN THE PROGRAM THINKS
// THIS IS A NEW BUTTON PRESS SO THE LIGHTS APPEAR TO NOT HAVE TURNED OFF HENCE WE THINK WE HAVE
// A BUG IN THE PROGRAM SO PUTTING THIS "while" LOOP THAT FCHECKS FOR THE BUTTON REALEASE BEFORE 
// THE PROGRAM IS ALLOWED TO CONTINUE TO EXECUTE
//

void loop()
{
  buttonReadButtonLeft =  digitalRead(pushButtonLeft);      // READ THE PIN CONNECTED TO THE START BUTTON
  buttonReadButtonRight = digitalRead(pushButtonRight);     // READ THE PIN CONNECTED TO THE START BUTTON
  toggleSwitchReadOveride = digitalRead(toggleSwitchOveride); // READ THE PIN CONNECTED TO THE START BUTTON
    
 // put your main code here, to run repeatedly:
  if ( !overRide ) // WE CHECK TO SEE IF THE OVERIDE SWITCH IS OFF IF OFF WE ARE ALLOWED TO CHECK THE LEFT RIGHT BUTTONS
    { 
      if ( buttonReadButtonLeft == 1 ) // IF THE LEFT BUTTON IS PRESSED WE GO AND DEBOUNCE THE BUTTON
        {
          for ( int count=0;count<5; ) 
            {
              buttonReadVal01 = digitalRead(pushButtonLeft);  // READ THE PIN AND SAVE IT
              delay(5);                                           // DELAY BETWEEN READS
              buttonReadVal02 = digitalRead(pushButtonLeft);  // RE-READ THE PIN AND SAVE IT
              if ( buttonReadVal01 == buttonReadVal02 )             // CHECK TO SEE IF BOTH READS ARE THE SAME
                {
                  count++; // INCREMENT THE COUNTER AS WE HAVE HAD A GOOD READ
                }
             }

// 2018-05-22 START OF STAGE 3 ADDITIONS 

          if ( !lightsOn )                                  // IF THE LIGHTS ARE OFF
            {                                               
              lightsOn = true;                              // SET THE FLAG TO ON
              directionLeftToRight = true;                  // SET THE DIRECTION FLAG LEFT TO RIGHT
              directionRightToLeft = false;                 // CLEAR THE DIRECTION FLAG RIGHT TO LEFT
            }
              
          if ( lightsOn && directionRightToLeft )           // IF THE LIGHTS ARE ON AND DIRECTION IS RIGHT TO LEFT
            {                                               // THEN TURN THEM OFF AND SET THE FLAG TO OFF
              lightsOn = false;                             // SET THE FLAG TO OFF
              digitalWrite(redLedTopRightBottomLeft, LOW);  // WRITE A LOGIC LOW TO THIS PIN TURNS THE LEDS OFF
              digitalWrite(redLedTopLeftBottomRight, LOW);  // WRITE A LOGIC LOW TO THIS PIN TURNS THE LEDS OFF
              directionLeftToRight = false;
              directionRightToLeft = false; 
              
             while(digitalRead(pushButtonLeft) == 1)        // WAIT HERE TILL THE BUTTON IS RELEASED
             {                                              // DO NOTHING WHILE IT WAITING
             }
             
             
            }
        }

// 2018-05-22 END OF STAGE 3 ADDITIONS LEFT BUTTON


      if ( buttonReadButtonRight == 1 ) // IF THE RIGHT BUTTON IS PRESSED WE GO AND DEBOUNCE THE BUTTON
        {
          for ( int count=0;count<5; ) // LOOP 5 TIMES TO CHECK BUTTONS STATE
            {
              buttonReadVal01 = digitalRead(pushButtonRight);  // READ THE PIN AND SAVE IT
              delay(5);                                            // DELAY BETWEEN READS
              buttonReadVal02 = digitalRead(pushButtonRight);  // RE-READ THE PIN AND SAVE IT
              if ( buttonReadVal01 == buttonReadVal02 )              // CHECK TO SEE IF BOTH READS ARE THE SAME
                {
                  count++; // INCREMENT THE COUNTER AS WE HAVE HAD A GOOD READ
                }
            }

// 2018-05-22 START OF STAGE 3 ADDITIONS RIGHT BUTTON
              
          if ( !lightsOn )                                  // IF THE LIGHTS ARE OFF
            {                                               
              lightsOn = true;                              // SET THE FLAG TO ON
              directionLeftToRight = false;                 // SET THE DIRECTION LEFT TO RIGHT
              directionRightToLeft = true;                  // CLEAR THE DIRECTION FLAG RIGHT TO LEFT
            }
              
          if ( lightsOn && directionLeftToRight )            // IF THE LIGHTS ARE ON AND DIRECTION IS LEFT TO RIGHT
            {                                               // THEN TURN THEM OFF AND SET THE FLAG TO OFF

              lightsOn = false;                             // SET THE FLAG TO OFF
              digitalWrite(redLedTopRightBottomLeft, LOW);  // WRITE A LOGIC LOW TO THIS PIN TURNS THE LEDS OFF
              digitalWrite(redLedTopLeftBottomRight, LOW);  // WRITE A LOGIC LOW TO THIS PIN TURNS THE LEDS OFF
              directionRightToLeft = false; 
              directionLeftToRight = false; 
               
             while(digitalRead(pushButtonRight) == 1)        // WAIT HERE TILL THE BUTTON IS RELEASED
             {                                               // DO NOTHING WHILE WAITING
             } 
            }
        }

// 2018-05-21 END OF STAGE 3 ADDITIONS RIGHT BUTTON
    }
           
   if (toggleSwitchReadOveride == 1) // IF THE TOGGLE SWITCH IS ON WE GO AND DEBOUNCE THE SWITCH
     {
       
    for (int count=0;count<5;) // LOOP 5 TIMES TO CHECK BUTTONS STATE
      {
        buttonReadVal01 = digitalRead(toggleSwitchOveride);  // READ THE PIN AND SAVE IT
        delay(5);                                            // DELAY BETWEEN READS
        buttonReadVal02 = digitalRead(toggleSwitchOveride);  // RE-READ THE PIN AND SAVE IT
        if (buttonReadVal01 == buttonReadVal02)              // CHECK TO SEE IF BOTH READS ARE THE SAME
          {
            count++; // INCREMENT THE COUNTER AS WE HAVE HAD A GOOD READ
          }
       }
  overRide =   true; // SET THE FLAG THAT WE WANT THE LIGHTS TO FLASH AND IGNORE THE PUSH BUTTONS
     }
    else // IF THE THE SWITCH IS OFF WE DO THIS INSTEAD
    {
    overRide =   false; // SET THE FLAG THAT WE WANT THE LIGHTS TO TURN OFF AND WE CAN READ THE PUSH BUTTONS
    digitalWrite(redLedTopRightBottomLeft, LOW);  // WRITE A LOGIC LOW TO THIS PIN TURNS THE LEDS OFF
    digitalWrite(redLedTopLeftBottomRight, LOW);  // WRITE A LOGIC LOW TO THIS PIN TURNS THE LEDS OFF
    }

  if ( overRide   || lightsOn )// IF THE OVERIDE FLAG OR THE LIGHTS ON FLAG IS SET WE FLASH THE LIGHTS
  {
  digitalWrite(redLedTopLeftBottomRight, HIGH); // WRITE A LOGIC HIGH TO THIS PIN TURNS THE LEDS ON
  digitalWrite(redLedTopRightBottomLeft, LOW);  // WRITE A LOGIC LOW TO THIS PIN TURNS THE LEDS OFF
  delay(750);
  digitalWrite(redLedTopLeftBottomRight, LOW);  // WRITE A LOGIC LOW TO THIS PIN TURNS THE LEDS OFF
  digitalWrite(redLedTopRightBottomLeft, HIGH); // WRITE A LOGIC HIGH TO THIS PIN TURNS THE LEDS ON
  delay(750);
  }
}
BuffyMcBuffer
Posts: 103
Joined: Sun Apr 01, 2018 12:59 pm

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Post by BuffyMcBuffer »

At least I now know there is one person reading this :)
I have been following this from the beginning and looking forward to you showing us more

Oh btw I use a program called UnoArdSim and tried to run your program but when it compiled it came up with and error saying it was looking for something after a bracket? I don’t have my laptop now but will let you know what line the error is on

Anyway thank you for sharing your knowledge it has helped me a lot
timbologist
Posts: 372
Joined: Wed Sep 18, 2013 6:39 am
Location: Hazeldene Victoria Australia ( in the bush )

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Post by timbologist »

BuffyMcBuffer wrote:Oh btw I use a program called UnoArdSim and tried to run your program but when it compiled it came up with and error saying it was looking for something after a bracket? I don’t have my laptop now but will let you know what line the error is on
Not sure about that as I have copied the code using select all from the header and pasted it back into the Arduino IDE and it compiles and up loads correctly. Did you use that method or just manually select then copy and paste.
As for UnoArdSim will have a look at that, butr it is for windows only so may not be able to run it.
BuffyMcBuffer
Posts: 103
Joined: Sun Apr 01, 2018 12:59 pm

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Post by BuffyMcBuffer »

Not sure about that as I have copied the code using select all from the header and pasted it back into the Arduino IDE and it compiles and up loads correctly. Did you use that method or just manually select then copy and paste.
As for UnoArdSim will have a look at that, butr it is for windows only so may not be able to run it.
Yep did it exactly as you suggested I think I could be the program

I’ll just have to get hold of a arduino uni and hardwire it all up, just trying to save some pennies running it in a virtual environment
timbologist
Posts: 372
Joined: Wed Sep 18, 2013 6:39 am
Location: Hazeldene Victoria Australia ( in the bush )

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Post by timbologist »

BuffyMcBuffer wrote:Yep did it exactly as you suggested I think I could be the program
I have downloaded that program and managed to get it to run in Linux, and get the same error as you did
does not like this statement " for ( int count=0;count<5; ) " tried separating it into 2 separate statements as thus

int count=0;
for (count < 5 )
and still did not like it, so it is a bug in the "UnoArdSim" program.
Unfortunately things like this turn people off trying to do any sort of programming, or hardware experimenting
as they think they are doing something wrong, when it is the actual resources they are using or referring to.
Anything I put up here has been proven before I upload it then when it is posted I download the code and try it.
But I am not perfect and may let something slip, so please let me know as soon as any problem does arise.
BuffyMcBuffer
Posts: 103
Joined: Sun Apr 01, 2018 12:59 pm

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Post by BuffyMcBuffer »

Thank you for taking the time to check that out,

I will grab some physical hardware to play with then

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

Re: ARDUINO PROGRAMMING, AND INTERFACING HELP

Post by timbologist »

Hello again
BuffyMcBuffer wrote:Thank you for taking the time to check that out,
I will grab some physical hardware to play with then
Next lesson :)
I am here to give help in any manor that is required so that you can have a trouble free learning experience.

Attached is a Zip file containing a much neater layout for this project, so you just need to extract it and it should put all the files in the correct directory.
From now on I will be laying out the programs in this style. The code view below is of the main program section, this attached set of files is how I lay my programming projects out. As will be seen it is much easier to follow. I have also added some extra code after the while loops, as this can still give a false reading that the button has been released. Because when the contacts break you get arcing even though extremely small in this case, but this arcing gives the same type of effect as when the button is pressed so it needs to be catered for in the program.

So the next post will be of the program with more efficient coding, which is more directed at persons with a more advanced knowledge. But there is no harm in anybody learning how to get the most out of the Arduino by using more direct methods of doing the same job as has already been done.
All gathered knowledge is useful at some time in your life :)

Code: Select all

// PROGRAM SET PROJECT-01-STAGE-03-ADDENDUM
// FILE NAME B-MAIN.ino
// NEW RAILWAY MODELLERS FORUM
// ARDUINO PROGRAMMING, AND INTERFACING HELP
// PROJECT 01
// LEVEL CROSSING FLASHING LIGHTS USING AN ARDUINO

// MAIN PROGRAM FILE

// A.R.TURNER 2018-05-22

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(9600);   // SET UP BAUD RATE TO BE ABLE TALK TO COMPUTER VIA USB PORT COMPUTER 
  pinSetup();           // GO AND INITIALISE THE INPUT OUTPUT PINS
}

void loop()
{
// put your main code here, to run repeatedly:

readTheButtons();              // GO TO ROUTINE CHECKS AND READS THE BUTTONS

readTheSwitch();               // GO TO ROUTINE CHECKS AND READS THE TOGGLE SWITCH
           
if ( overRide   || lightsOn )  // IF THE OVERIDE FLAG OR THE LIGHTS ON FLAG IS SET WE FLASH THE LIGHTS
  {
  flashTheLights();            // GO AND FLASH THE LIGHTS
  }
}
Attachments
NRM-PROJECT-01-STAGE-03-ADDENDUM.zip
(6.83 KiB) Downloaded 38 times
Post Reply