Netduino 2 – Introduction to Netduino – Traffic lights

Class: Program.cs

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using System.Collections;

namespace NetduinoApplication1
{
    public class Program
    {
        
        //the output ports that control the LEDs on the breadboard
        static OutputPort greenLED = new OutputPort(Pins.GPIO_PIN_D0, false);
        static OutputPort amberLED = new OutputPort(Pins.GPIO_PIN_D1, false);
        static OutputPort redLED = new OutputPort(Pins.GPIO_PIN_D2, false);
        
        //array containing the traffic light states
        static ArrayList arrTrafficLightState;

        //the index of the selected state
        static int trafficLightStateIndex = 0;

        //the button on the netduino that triggers the traffic light to change state
        static InterruptPort buttonOnBoard;

        

        public static void Main()
        {
            //create traffic light states
            createStates();
            
            //turn default green led
            ((TrafficLightState)arrTrafficLightState[trafficLightStateIndex]).TurnOnLEDs();
            
            // create trigger for button
            buttonOnBoard = new InterruptPort(Pins.ONBOARD_BTN, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeHigh); //InterruptEdgeHigh will remove the switch bounce problem. If this is not done, the method button_OnInterrupt can be triggered multiple times
            buttonOnBoard.OnInterrupt += new NativeEventHandler(button_OnInterrupt);

            Thread.Sleep(Timeout.Infinite);
        }


        //called when the button on the netduino is pressed
        static void button_OnInterrupt(uint data1, uint data2, DateTime time)
        {
                for (int i = trafficLightStateIndex + 1; i <= arrTrafficLightState.Count; i++)
                {
                    ((TrafficLightState)arrTrafficLightState[i - 1]).TurnOffLEDs();

                    if (i != arrTrafficLightState.Count)
                    {
                        ((TrafficLightState)arrTrafficLightState[i]).TurnOnLEDs();
                        Thread.Sleep(Convert.ToInt32(((TrafficLightState)arrTrafficLightState[i]).TrafficLightStateDuration.ToString()));
                    }
                    else
                    {
                        //reset traffic light states
                        trafficLightStateIndex = 0;
                        ((TrafficLightState)arrTrafficLightState[trafficLightStateIndex]).TurnOnLEDs();
                    }
                  
                }
        }
        
        static void createStates()
        {
            arrTrafficLightState = new ArrayList();

            createFirstState();
            createSecondState();
            createThirdState();
            createForthState();
        }


        //only green
        static void createFirstState()
        {
            TrafficLightState objTrafficLightState = new TrafficLightState { TrafficLightStateDuration = TrafficLightState.Duration.None };
            objTrafficLightState.AddTrafficLightLED(greenLED);

            arrTrafficLightState.Add(objTrafficLightState);
        }


        //only amber
        static void createSecondState()
        {
            TrafficLightState objTrafficLightState = new TrafficLightState { TrafficLightStateDuration = TrafficLightState.Duration.Medium };
            objTrafficLightState.AddTrafficLightLED(amberLED);

            arrTrafficLightState.Add(objTrafficLightState);
        }

        //only red
        static void createThirdState()
        {
            TrafficLightState objTrafficLightState = new TrafficLightState { TrafficLightStateDuration = TrafficLightState.Duration.Long };
            objTrafficLightState.AddTrafficLightLED(redLED);

            arrTrafficLightState.Add(objTrafficLightState);
        }


        //red+amber
        static void createForthState()
        {
            TrafficLightState objTrafficLightState = new TrafficLightState { TrafficLightStateDuration = TrafficLightState.Duration.Medium };
            objTrafficLightState.AddTrafficLightLED(redLED);
            objTrafficLightState.AddTrafficLightLED(amberLED);

            arrTrafficLightState.Add(objTrafficLightState);
        }

    }
}


Class: TrafficLightState.cs

using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using System.Collections;

namespace NetduinoApplication1
{
   public class TrafficLightState
    {
      public enum Duration
       {
           None,
           Short =  1000,
           Medium = 5000,
           Long =   10000
       };
       
       //public variables
       public Duration TrafficLightStateDuration;
       public ArrayList TrafficLightStateLEDs; //List is not supported in the Microframework 


       //constructor
       public TrafficLightState() 
       {
           TrafficLightStateLEDs = new ArrayList();
       }


       //add output port to arraylist
       public void AddTrafficLightLED(OutputPort objLed)
       {
           TrafficLightStateLEDs.Add(objLed);
       }

       //turn on all output ports
       public void TurnOnLEDs()
       {
           foreach (OutputPort item in TrafficLightStateLEDs)
           {
               item.Write(true);
           }
       }


       //turn off all output ports
       public void TurnOffLEDs()
       {
           foreach (OutputPort item in TrafficLightStateLEDs)
           {
               item.Write(false);
           }
       }


    }
}

Advertisements

One thought on “Netduino 2 – Introduction to Netduino – Traffic lights

  1. Pingback: LED Controller – Android & Netduino | Roderick Vella Cesare

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s