Easy-Connect-Software-Outline

From ArduinoInfo
Jump to navigation Jump to search

V2.29 01/23/2020 Questions: terry@yourduino.com

NOTE: UNDER CONSTRUCTION - This is not Student-useable at this time

Introduction

  • WHAT IT DOES: Provides several preprogrammed Examples for users
    • There are a series of "Examples" that cover different concepts in Physical Computing, like Sensors, Actuators, Decision Making, Communications etc..
  • Uses the KEYES Easy-Plug board V2.0
  • SEE the comments after "//" on each line below
  • CONNECTIONS: KEYES Easy-plug Control board V2.0
  • NOTE: See updated connections in the Easy-plug Education software. Many pins are re-used in different projects.

INITIAL SETUP:

  • User connects LCD display to I2C connector
  • User connects IR Receiver module to connector D5
  • User connects USB cable or battery pack to Easy-Plug control board

CONNECTIONS: KEYES Easy-Plug Control Board V2.0

 D2A6A7  ks0245 keyestudio EASY plug Joystick Module
 D3D4    ks0242 keyestudio EASY plug SR01 Ultrasonic Module
 5       ks0125 keyestudio EASY plug IR Receiver Module
 6       ks0101 keyestudio EASY plug Digital White LED Module
 7       ks0102 keyestudio EASY plug Active Buzzer Module
 8       ks0129 keyestudio EASY plug DHT11 Temperature and Humidity Sensor
 9       ks0122 keyestudio EASY plug PIR Motion Sensor
 10 *    ks0115 keyestudio EASY plug Digital Tilt Sensor Module  [ON SPI CONNECTOR ]
 10 *    ks0112 keyestudio EASY plug Digital Push Button
 10 *    ks0113 keyestudio EASY plug Capacitive Touch Sensor
 11      Unavailable: SPI use only
 12      Unavailable: SPI use only
 7 *     ks0228 keyestudio EASY plug Digital Red LED Module    [OPTIONAL: SHARED]
 8 *     s0229 keyestudio EASY plug Digital Green LED Module
 9 *     ks0230 keyestudio EASY plug Digital Yellow LED Module
 A0      ks0109 keyestudio EASY plug Analog Rotation Sensor
 A1      ks0106 keyestudio EASY plug Photocell Sensor
 A2      SERVO http://www.yourduino.com/sunshop/index.php?l=product_detail&p=238
 A3      Available for General Use
 A4 A5   ks0137 keyestudio EASY plug 1602 LCD I2C Module



USER SELECTION OF EXAMPLES:

  • User uses the number buttons on the IR remote to select different examples
  • User must press RESET button to select a new example

/*-------------( Example 0: DIGITAL OUTPUT )--[Power-On DEFAULT]-------

This is the default example running at power-on or reset

  • Blinks Morse Code "HI" on pins 6, 7 and 13.
  • User sees blink on onboard LED pin 13.
  • User connects buzzer brick to STROBE_LED_PIN (7) so Morse is audible
  • Morse is discussed. Digital is discussed. Look at cables and colors.
  • User uses a cable to connect bright LED brick to pin 7

/*-------------( Example 1: DIGITAL INPUTS )---------------

  • Bright LED connected to LED_MODULE_PIN: D6 lights when switch is ON.
  • Buzzer connected to D7
  • User connects various different switches to A3
  • User pushes button brick, ON-OFF, TRUE-FALSE, 1-0, HIGH-LOW discussed.
  • LCD displays “IT’S OFF! 0 LOW” or “IT’S ON! 1 HIGH
  • Other bricks are connected to pin A3: Tilt, maybe others
  • Bare wires connected to pin A3; conductors/insulators discussed.
  • Motion Detect (Active Low) is connected to A3. D8 is shown to “Fix it in Software”

/*-----------------( Example 2: Analog to LCD )-------------

  • Potentiometer connected to A0
  • Photoresistor connected to A1
  • Compare light level, turn on LED light if dark
  • Voltage dividers discussed?
  • Different resistive type sensors discussed
  • LCD Displays the values of knob and Photoresistor (0..1024) ?Discuss binary?

/*------------------( Example 3: Analog IN / OUT )----------------

  • Analog variable from user's Potentiometer on A0
  • Dimming of Bright LED on LED_MODULE Pin 6
  • Strobe blink speed on STROBE LED on Pin 9
  • + BUZZER if "TooFast"
  • Servo Position on pin A2 (Speedometer)
  • Possible "Dashboard" construction: "Make A New Device"

/*------------------( Example 4: Temperature and Humidity with DHT11 Sensor )----------------

  • User connect DHT11 sensor to D8
  • LCD Displays Temperature / Humidity
  • ? What If - the user want to be able to see temperature in both C and F ?
  • User plugs in a Switch which changes display from Centigrade to Fahrenheit

/*------------------( Example 5: Test handheld IR Remote: display codes )----------------

  • User selects "5" with IR remote
  • User presses all the different buttons on the remote, sees results
  • User aligns the IR receiver with the remote and backs off to determine range.
  • ? What is Infrared light? Can we see it on a digital camera / Phone?? (Iphone user-facing camera)
  • ? How does the handheld communicate? (Need graphic )

/*--------------------( Example 6: Test Ultrasonic Sensor module )---------------------------

  • User connects the Ultrasonic Sensor Module to connector D3D4
  • User aims the sensor to an open area
  • User Places different objects in front of the sensor, sees distance displayed on LCD
  • User connects Buzzer to D7, hears beep speed change with distance.
  • ? How could this be used by a blind person ?

/*--------------------( Example 7: Test Joystick )---------------------------

  • User connects the Joystick D2A6A7
  • User Moves the Joystick over X-Y area
  • User sees the X and Y values displayed on the LCD display
  • User pushes DOWN on joystick, sees "Z" value (Zero or One) displayed, LED13 goes ON and OFF

/*--------------------( Example 8: System for Traffic System )---------------------------

CONNECTIONS:

  • Ultrasonic Sensor Module to D3D4
  • Touch switch to A0
  • RED LED Module to D6
  • YELLOW LED Module to D7
  • GREEN LED Module to D8
  • WHITE LED Module to D9
  • User arranges LEDs as a traffic signal RED-YELLOW-GREEN
  • User arranges WHITE LED as WALK signal
  • User makes a model intersection with the traffic signal, with the Ultrasonic sensor detecting approaching vehicle on side street, and with a WALK button at the main street.
  • The system runs the traffic signal sequence and timing. It responds to a vehicle on the cross street, and to the Walk button being pressed.
  • The development of a system like this is discussed. The State Machine that controls the system is discussed and diagrammed.

CODE for the Easy-Plug Pre-Programmed Examples

/* YourSmartDuino-EM
  - WHAT IT DOES: Provides several preprogrammed lessons options for users
  - Uses the KEYES Easy-Plug Modules

  - V2.29 01/23/2020   TRAFFIC LIGHT SYSTEM WORKING
    Questions: terry@yourduino.com
  - SEE the comments after "//" on each line below
  - CONNECTIONS: KEYES Easy-Plug Control Board V2.0

  D2A6A7  ks0245 keyestudio EASY plug Joystick Module
  D3D4    ks0242 keyestudio EASY plug SR01 Ultrasonic Module
  5       ks0125 keyestudio EASY plug IR Receiver Module
  6       ks0101 keyestudio EASY plug Digital White LED Module
  7       ks0102 keyestudio EASY plug Active Buzzer Module
  8       ks0129 keyestudio EASY plug DHT11 Temperature and Humidity Sensor
  9       ks0122 keyestudio EASY plug PIR Motion Sensor
  10 *    ks0115 keyestudio EASY plug Digital Tilt Sensor Module  [ON SPI CONNECTOR ]
  10 *    ks0112 keyestudio EASY plug Digital Push Button
  10 *    ks0113 keyestudio EASY plug Capacitive Touch Sensor
  11      Unavailable: SPI use only
  12      Unavailable: SPI use only
  7 *     ks0228 keyestudio EASY plug Digital Red LED Module    [OPTIONAL: SHARED]
  8 *     s0229 keyestudio EASY plug Digital Green LED Module
  9 *     ks0230 keyestudio EASY plug Digital Yellow LED Module
  A0      ks0109 keyestudio EASY plug Analog Rotation Sensor
  A1      ks0106 keyestudio EASY plug Photocell Sensor
  A2      SERVO http://www.yourduino.com/sunshop/index.php?l=product_detail&p=238
  A3      Available for General Use
  A4 A5   ks0137 keyestudio EASY plug 1602 LCD I2C Module


  /*-----( Import needed libraries )-----*/
#include "IRremote.h"          // For handheld remote (Get in Library <amager)
#include <LedFlasher.h>        // From Nick Gammon  (Get from online ZIP)

#include <Wire.h>  // Comes with Arduino IDE for I2C support
// Get the LCD I2C Library here:
// https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads
// Move any other LCD libraries to another folder or delete them
// See Library "Docs" folder for possible commands etc.
#include <LiquidCrystal_I2C.h>

#include <dhtnew.h>     // DHT11 Temperature-Humidity Sensor  (Get from online ZIP)
// [Rob Tillaart's new version: It finds out what module is being used..]

/*----------------( Declare Constants and Pin Numbers )-------------------*/
// NOTE: PWM Pins = 3,5,6,9,10,11

//----- ( 3 pins in socket labelled D2A6A7 can be used for JoyStick )-----
#define X_AXIS_PIN        A7  /*----(Joystick Pin connections)----*/
#define Y_AXIS_PIN        A6
#define Z_SWITCH_PIN      2

//----- ( 2 pins in socket labelled D3 D4 can be used for Ultrasonic )-----
#define ULTRA_TRIG_PIN    3   // Connect to socket labelled "D3D4 
#define ULTRA_ECHO_PIN    4

#define IR_RECEIVER_PIN   5  // Connect always, at beginning to select lessons
#define LED_MODULE_PIN    6

#define RED_LIGHT         6  // Traffic Lights
#define YELLOW_LIGHT      7
#define GREEN_LIGHT       8
#define WALK_LIGHT        9

//-------( Multiple uses of these pins )--------------
#define BUZZER_PIN        7

#define DHT11_PIN         8  // Temperature/Humidity Sensor
#define LED_MODULE_INVERT 8  // To show Fix It In Software

#define PIR_MOTION_PIN    9
#define STROBE_LED_PIN    9
#define WALK_LED_PIN      9  // ??

//----- ( 3 pins in socket labelled SPI  )-----
#define SWITCH1_PIN       10   // in SPI connector
#define SPI_Unavailable11 11
#define SPI_Unavailable12 12

#define ONBOARD_LED_PIN   13     // also in SPI connector

#define ROTATION_PIN       A0    // Analog inputs from Potentiometer etc.
#define REQUEST_WALK_PIN   A0    // Digital request traffic walk
#define LIGHT_SENSOR_PIN   A1    // Photoresistor
#define VEHICLE_SENSE_PIN  A1
#define SERVO_PIN          A2
#define SWITCH2_PIN        A3   // Used for a variety of On-OFF Devices

// NOTE: Pins A4 and A5 in "I2C socket and are used for I2C communication to LCD Display
#define I2C_SDA_PIN        A4   
#define I2C_SCL_PIN        A5

#define ForEver  true       // For use in looping
#define ON       1
#define OFF      0
#define DarkValue 500       // Onboard Light Sensor

#define ServoMIN  20  // Don't go to very end of servo travel
#define ServoMAX  160 // which may not be all the way from 0 to 180.

#define MAX_DISTANCE 300     // Maximum distance we want to ping for (in centimeters).
#define US_ROUNDTRIP_CM 58.2 // Ultrasonic Sensor constant

//-----( Traffic Control System States )-------
#define GREEN_STATE      0
#define CAUTION_STATE    1
#define RED_STATE        2
#define WALKING_STATE    3
#define WALK_FAST_STATE  4

#define MaxGreenSeconds  20  // Traffic constants
#define MinGreenSeconds  10
#define YellowSeconds     5
#define MaxRedSeconds    10

/*--------------------( Declare objects )-------------------------*/
IRrecv irrecv(IR_RECEIVER_PIN);       // create instance of 'irrecv'
decode_results results;               // create instance of 'decode_results'

int Led13OnTime  = 100;
int Led13OffTime = 900;
LedFlasher LED13 (ONBOARD_LED_PIN, Led13OffTime, Led13OnTime);   // OffTime, OnTime

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);   // Define the LCD Display

DHTNEW Temp_Humid_Sensor(DHT11_PIN);   // Define the DHT11 Temperature / humidity sensor

/*------------------------( Declare Variables )-----------------------------------*/

int   OptionNumber = 0;        // Option as picked by IR Remote
int   KnobValue;        // Analog values from 0..1023
int   PhotosensorValue; // Analog values from 0..1023

int  Xvalue; /*----(Values we will read from the Joystick)----*/
int  Yvalue;
int  Zvalue;

unsigned long     StrobeLastMs ;
unsigned long     StrobeLedOnTime  = 10L ;  // Variables for Strobe LED
unsigned long     StrobeLedOffTime = 1000L ;
boolean InStrobeTime = false;
boolean StrobeIsOn   = false;

int     ServoPosition ;

long duration_uS; // Duration used to calculate distance by Ultrasonic Sensor
int DistCM;       // Measured distance in CM

boolean  ShowedON  = false;  // Control writing to LCD
boolean  ShowedOFF = false;

int     TrafficControlState = 0; ///---( For Traffic Light Control example )----
int     StateDelaySeconds;    // Variable set for Traffic Light sequence states
boolean TrafficSenseCall    = false;
boolean WalkerSenseCall     = false;



void setup()   /******************* SETUP: RUNS ONCE *************************/
{
  Serial.begin(115200);        // Start up the Serial Monitor

  lcd.begin(16, 2);  // initialize the lcd for 16 chars 2 lines, turn on backlight
  lcd.noBacklight();
  delay(500);
  lcd.backlight(); // finish with backlight on

  lcd.setCursor(0, 0); //Start at character 0 on line 0
  lcd.print(F("EasyConnect V28"));  
  lcd.setCursor(0, 1); //Start at character 0 on line 0
  lcd.print(F("YourDuino.com"));

  irrecv.enableIRIn(); // Start the IR Remote receiver

  pinMode(LED_MODULE_PIN, OUTPUT);
  pinMode(LED_MODULE_INVERT, OUTPUT);
  pinMode(ONBOARD_LED_PIN, OUTPUT);
  pinMode(SERVO_PIN, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);

  pinMode(SWITCH2_PIN, INPUT_PULLUP);
  pinMode(SWITCH1_PIN, INPUT_PULLUP);

  pinMode(ULTRA_TRIG_PIN, OUTPUT);
  pinMode(ULTRA_ECHO_PIN, INPUT);

  pinMode(Z_SWITCH_PIN, INPUT_PULLUP); // Use built-in pullup resistor


  LED13.begin ();
  Serial.println("YourDuino.com EasyConnect system preload V2.28 3/19/2019");

  delay(200);
}//--(end setup )---


void loop()   /********************** LOOP: RUNS CONSTANTLY ************************/
{

  switch (OptionNumber)
  {
    case 0:
      /*-------------( Option 0: DIGITAL OUTPUT )---------------
         Blinks Morse Code "HI" on pins 7 and 13.
         User sees blink on onboard LED pin 13.
         User uses a cable to connect bright LED brick to pin 7
         User connects buzzer brick to LED_MODULE_PIN (7) so Morse is audible.
         Morse is discussed. Digital is discussed. Look at cables and colors.

      */
      {
        Serial.println("Option 0");
        do
        {
          BlinkHiSimple();    // Send "HI" in Morse Code on several pins

          //--------( Check if user has sent a IR Remote Command )------------------------------
          if (irrecv.decode(&results)) // have we received an IR signal?
          {
            translateIR();   // Translate the IR data, and change the Option Number
            irrecv.resume(); // receive the next value
          }

        } while (OptionNumber == 0);  // Loop here "ForEver" or until a different option requested
        break;
      } // END case 0


    case 1:   /*-------------( Option 1: DIGITAL INPUTS )---------------

        User connects different switches to A3
         User pushes button brick, ON-OFF, TRUE-FALSE, 1-0, HIGH-LOW discussed.
         Other bricks are connected to pin A3: Tilt, Vibration, Motion Detect
         Bare wires connected to pin A3; conductors/insulators discussed.
         Bright LED reconnected to pin 6: lights when switch is ON.
*/
      {
        Serial.println("Option 1");
        lcd.setCursor(0, 0); //Start at character 0 on line 0
        lcd.print(F("1: SWITCH ON/OFF"));
        pinMode(A3, INPUT_PULLUP);


        do
        {
          if    ((digitalRead(SWITCH1_PIN)   == LOW)
                 or     (digitalRead(SWITCH2_PIN)   == LOW))
          {
            digitalWrite(LED_MODULE_PIN, HIGH);
            digitalWrite(BUZZER_PIN,     HIGH);
            digitalWrite(LED_MODULE_INVERT, LOW); // Fix It In Software
            if (!ShowedON)
            {
              lcd.setCursor(0, 1); //Start at character 0 on line 0
              lcd.print(F("IT'S ON! 1 HIGH "));
              LED13.on ();
            }
            ShowedON = true;
            ShowedOFF = false;
          }
          else
          {
            digitalWrite(LED_MODULE_PIN, LOW);
            digitalWrite(BUZZER_PIN,     LOW);
            digitalWrite(LED_MODULE_INVERT, HIGH);
            if (! ShowedOFF)
            {
              lcd.setCursor(0, 1); //Start at character 0 on line 0
              lcd.print(F("IT'S OFF! 0 LOW  "));
              LED13.off ();
            }
            ShowedOFF = true;
            ShowedON = false;
          }
          LED13.update ();
        } while (ForEver);  // Loop here "ForEver" or RESET

      }// END case 1


    //-----------------------------------------------------------------------------------------
    case 2: //------------( ANALOG MEASUREMENTS - LCD )-----------------------
      {
        Serial.println("Option 2");
        lcd.setCursor(0, 0); //Start at character 0 on line 0
        lcd.print(F("2:Analog to LCD"));
        delay(4000);
        do
        {

          KnobValue = analogRead(ROTATION_PIN);   // Read the position of the Potentiometer
          lcd.setCursor(0, 0); //Start at character 0 on line 0
          lcd.print(F("ANALOG VALUES: "));
          lcd.setCursor(0, 1); //Start at character 0 on line 1
          lcd.print(F("          "));
          lcd.setCursor(0, 1); //Start at character 0 on line 1
          lcd.print(F("KNOB"));
          lcd.print(KnobValue);

          PhotosensorValue = analogRead(LIGHT_SENSOR_PIN);   // Read the light sensor
          lcd.setCursor(9, 1); //Start at character 8 on line 1
          lcd.print(F("          "));
          lcd.setCursor(9, 1); //Start at character 0 on line 0
          lcd.print(F("LITE"));
          lcd.print(PhotosensorValue);

          LED13.update ();
          delay(1000);
        } while (ForEver);  // Loop here "ForEver" or RESET
      } // END case 2
    //------------------------------------------------------------------------------------------


    case 3: // ANALOG IN OUT STROBE
      /*-------------------( Option 3: ANALOG IN / OUT / STROBE )-------------------------------
         Analog variable from user's Potentiometer on A3
         Dimming of Bright LED on LED_MODULE Pin 6
         Strobe blink speed on STROBE LED on Pin 9
         Servo Position on pin A2
      */
      {
        Serial.println("Option  3");
        lcd.setCursor(0, 0); //Start at character 0 on line 0
        //-------------------1234567890123456----------
        lcd.print(F("3:KNOB sets LEDS"));
        do
        {
          KnobValue = analogRead(ROTATION_PIN);   // Read the position of the Potentiometer
          if (KnobValue < 15) {
            KnobValue = 0;
          }

          analogWrite(LED_MODULE_PIN, ((KnobValue / 4) ) );

          /*-----------( Set up and control strobe frequency and duration )-----------------------------*/
          StrobeLedOffTime = (1023 - KnobValue) / 2L;  // Set Strobe parameters from Analog Input
          StrobeLedOnTime  = StrobeLedOffTime / 10L;

          if (InStrobeTime)
          {
            if
            (
              ((StrobeLastMs + StrobeLedOnTime) < millis())
              &&
              StrobeIsOn
            )
            {
              StrobeIsOn = false;
              digitalWrite(STROBE_LED_PIN, LOW);
            }

            if
            (
              ((StrobeLastMs + StrobeLedOnTime + StrobeLedOffTime) < millis())
              &&
              ! StrobeIsOn
            )
            {
              InStrobeTime = false;
            }


          } // END InStrobeTime

          else // Begin another Strobe cycle
          {
            StrobeLastMs = millis();
            InStrobeTime = true;
            digitalWrite(STROBE_LED_PIN, HIGH);
            StrobeIsOn   = true;

          }

          /*----Set Servo position from Potentiometer value and Send Servo Position to Servo )-----*/

          ServoPosition  = map((1023 - KnobValue), 0, 1023, 900 , 2000);
          PointServo(ServoPosition);

          LED13.update ();
        } while (ForEver);  // Loop here "ForEver" or RESET
      } // END case 3

    //-------------------------------------------------------------------


    case 4: // TEMPERATURE / HUMIDITY
      {
        Serial.println("Option 4");

        lcd.setCursor(0, 0); //Start at character 0 on line 0
        //-------------------1234567890123456----------
        lcd.print(F("4: Temp-Humidity"));
        lcd.setCursor(0, 1); //Start at character 0 on line 0
        lcd.print(F("DHT11 Sensor "));
        delay(4000);
        do
        {
          Temp_Humid_Sensor.read();
          delay(1000);
          lcd.setCursor(0, 0); //Start at character 0 on line 0
          lcd.print("Humidity   = ");
          lcd.print((float)Temp_Humid_Sensor.humidity, 0);
          lcd.print("%");

          lcd.setCursor(0, 1); //Start at character 0 on line 1
          lcd.print("Temperature= ");
          lcd.print((float)Temp_Humid_Sensor.temperature, 0);
          lcd.print("C");
        } while (ForEver);  // Loop here "ForEver" or RESET

      } // END case 4

    case 5:  // IR REMOTE
      {
        Serial.println("Option 5");
        lcd.setCursor(0, 0); //Start at character 0 on line 0
        //-------------------1234567890123456----------
        lcd.print(F("5: SHOW REMOTE  "));
        lcd.setCursor(0, 1); //Start at character 0 on line 1
        lcd.print(F("Press a button"));
        do
        {
          delay(100);

          //--------( Check if user has sent a IR Remote Command )------------------------------
          if (irrecv.decode(&results)) // have we received an IR signal?
          {
            lcd.setCursor(0, 1); //Start at character 0 on line 0
            lcd.print(F("                 "));   // Blank out previous line
            lcd.setCursor(0, 1); //Start at character 0 on line 0 for following print

            switch (results.value)
            {
              case 0xFF629D: lcd.print(F(" FORWARD")); break;
              case 0xFF22DD: lcd.print(F(" LEFT"));    break;
              case 0xFF02FD: lcd.print(F(" -OK-"));    break;
              case 0xFFC23D: lcd.print(F(" RIGHT"));   break;
              case 0xFFA857: lcd.print(F(" REVERSE")); break;
              case 0xFF6897: lcd.print(F(" 1")); break;
              case 0xFF9867: lcd.print(F(" 2")); break;
              case 0xFFB04F: lcd.print(F(" 3")); break;
              case 0xFF30CF: lcd.print(F(" 4")); break;
              case 0xFF18E7: lcd.print(F(" 5")); break;
              case 0xFF7A85: lcd.print(F(" 6")); break;
              case 0xFF10EF: lcd.print(F(" 7")); break;
              case 0xFF38C7: lcd.print(F(" 8")); break;
              case 0xFF5AA5: lcd.print(F(" 9")); break;
              case 0xFF42BD: lcd.print(F(" *")); break;
              case 0xFF4AB5: lcd.print(F(" 0")); break;
              case 0xFF52AD: lcd.print(F(" #")); break;
              case 0xFFFFFFFF: lcd.print(F(" REPEAT")); break;
              default:
                lcd.print(F(" Bad Signal"));

            }// End Case

            irrecv.resume(); // receive the next value

          }//END If IR Received

        } while (ForEver);  // Loop here "ForEver" or RESET

      } // END case 5

    case 6: // ULTRASONIC
      {
        Serial.println("Option 6");
        lcd.setCursor(0, 0); //Start at character 0 on line 0
        lcd.print(F("6: UltraSonic  "));
        lcd.setCursor(0, 1); //Start at character 0 on line 1
        lcd.print(F("DISTANCE= "));
        do
        {
          /* The following trigPin/echoPin cycle is used to determine the
            distance of the nearest object by bouncing soundwaves off of it. */
          digitalWrite(ULTRA_TRIG_PIN, LOW);  // Pulse the Trigger  pin High and then Low
          delayMicroseconds(2);
          digitalWrite(ULTRA_TRIG_PIN, HIGH);
          delayMicroseconds(10);
          digitalWrite(ULTRA_TRIG_PIN, LOW);

          duration_uS = pulseIn(ULTRA_ECHO_PIN, HIGH);

          //Calculate the distance (in cm) based on the speed of sound.
          DistCM = duration_uS / US_ROUNDTRIP_CM; // Calculate distance in CM
          Serial.print("DistCM = ");
          Serial.println(DistCM);


          lcd.setCursor(10, 1); //Start at character 0 on line 1
          lcd.print(F("      "));
          lcd.setCursor(10, 1); //Start at character 0 on line 1
          if (DistCM > 200)
          {
            lcd.print("XXX");
          }
          else
          {
            lcd.print(DistCM);
          }
          lcd.print(" CM");


          if (DistCM <= 200)
          {
            //-----( Calculate buzzer action depending on distance )--------
            digitalWrite(BUZZER_PIN, HIGH);
            delay(DistCM * 2);
            digitalWrite(BUZZER_PIN, LOW);
            delay(DistCM * 12);
          }
          delay(10);
        } while (ForEver);  // Loop here "ForEver" or RESET

      } // END case

    case 7:  // JOYSTICK
      {
        Serial.println("Option 7");
        lcd.setCursor(0, 0); //Start at character 0 on line 0
        //-----------1234567890123456----------
        lcd.print(F("7: JoyStick     "));
        delay(500);
        do
        {
          lcd.print(F("      "));
          lcd.setCursor(0, 1); //Start at character 0 on line 1
          Xvalue = analogRead (X_AXIS_PIN); /*----(Read the X value and display it)----*/
          Xvalue = map(Xvalue, 1023, 0, 0, 1023); // Correct the right-left on Joystick
          Serial.print ( "X " );
          Serial.print (Xvalue, DEC );
          lcd.print(F("X="));
          lcd.print(Xvalue, DEC);

          Yvalue = analogRead (Y_AXIS_PIN);/*----(Read the Y value and display it)----*/
          Yvalue = 1023 - Yvalue;   // Reverse the numbers so it "looks right"
          Serial.print ( " | Y " );
          Serial.print (Yvalue, DEC );
          lcd.print(F(" Y="));
          lcd.print(Yvalue, DEC);

          Zvalue = digitalRead (Z_SWITCH_PIN);/*----(Read the Z value and display it)----*/
          Serial.print ( " | Z " );
          Serial.print (Zvalue, DEC );
          lcd.print(F(" Z="));
          lcd.print(Zvalue, DEC);

          Serial.println ();  /*----(End of Line)----*/

          if (Zvalue == 1) //The knob is pushed down
          {
            digitalWrite(ONBOARD_LED_PIN, HIGH);
          }
          else
          {
            digitalWrite(ONBOARD_LED_PIN, LOW);
          }

          delay(200);  /*----(Wait to read the print)----*/
        } while (ForEver);  // Loop here "ForEver" or RESET


      } // END case

    case 8:  // -----( TRAFFIC LIGHT CONTROL SYSTEM )------
      /* States:
          GREEN_STATE  - Green One, testing Sensor, Red (Don't Walk)  [MAX 30][Extend Green (red) 10]
          CAUTION_STATE  - Flashing Yellow  White (Don't Walk)[5 sec]
          RED_STATE  - Red ,  White (Don't Walk)[10]
          WALKING_STATE  - Red plus flashing white, beeping
          WALK_FAST_STATE   - Red plus White, fast beeping

      */
      {
        Serial.println("Option 8");
        pinMode(GREEN_LIGHT, OUTPUT);
        pinMode(YELLOW_LIGHT, OUTPUT);
        pinMode(RED_LIGHT, OUTPUT);
        pinMode(WALK_LIGHT, OUTPUT);

        lcd.setCursor(0, 0); //Start at character 0 on line 0
        //-----------1234567890123456----------
        lcd.print(F("8:TRAFFIC LIGHT "));
        StateDelaySeconds = MaxGreenSeconds;
        TrafficSenseCall  = false;
        do
        {
          lcd.setCursor(0, 1); //Start at character 0 on line
          //-------------------1234567890123456----------
          lcd.print(F("                "));
          lcd.setCursor(0, 1); //Start at character 0 on line
          lcd.print(F("STATE= "));
          lcd.print(TrafficControlState);
          delay(100);


          if (digitalRead(VEHICLE_SENSE_PIN) == 0)
          {
            TrafficSenseCall = true;
          }

          if (digitalRead(REQUEST_WALK_PIN) == 0)
          {
            WalkerSenseCall = true;
          }


          switch (TrafficControlState)

          {
            case GREEN_STATE:
              {
                Serial.println("GREEN");
                lcd.setCursor(9, 1); //Start at character 9 on line
                lcd.print(F("GREEN"));
                digitalWrite(GREEN_LIGHT, ON);
                digitalWrite(RED_LIGHT, OFF);
                if (StateDelaySeconds >= 0)
                {
                  delay(1000);
                  Serial.println(StateDelaySeconds);
                  StateDelaySeconds -= 1;
                }
                else
                {
                  StateDelaySeconds = YellowSeconds;
                  TrafficControlState = CAUTION_STATE;
                }

                if (  (TrafficSenseCall | WalkerSenseCall)
                      & (StateDelaySeconds < MinGreenSeconds) )
                {
                  Serial.println("Vehicle/Walker Sensed");
                  TrafficSenseCall = false;
                  StateDelaySeconds = YellowSeconds;
                  TrafficControlState = CAUTION_STATE;
                }

              }//End GREEN_STATE

              break;

            case CAUTION_STATE:
              {
                Serial.println("YELLOW");
                lcd.setCursor(9, 1); //Start at character 9 on line
                lcd.print(F("YELLOW"));
                digitalWrite(GREEN_LIGHT, OFF);
                digitalWrite(YELLOW_LIGHT, ON);
                if (StateDelaySeconds >= 0)
                {
                  delay(1000);
                  Serial.println(StateDelaySeconds);
                  StateDelaySeconds -= 1;
                }
                else
                {
                  StateDelaySeconds = MaxRedSeconds;
                  TrafficControlState = RED_STATE;
                }

              }//End CAUTION_STATE
              break;

            case RED_STATE:
              {
                Serial.println("RED");
                lcd.setCursor(9, 1); //Start at character 9 on line
                lcd.print(F("RED"));
                digitalWrite(RED_LIGHT, ON);
                digitalWrite(YELLOW_LIGHT, OFF);
                if (StateDelaySeconds >= 0)
                {
                  delay(1000);
                  Serial.println(StateDelaySeconds);
                  StateDelaySeconds -= 1;
                }
                else
                {
                  StateDelaySeconds = MaxGreenSeconds;
                  TrafficControlState = GREEN_STATE;
                }

                if (WalkerSenseCall)
                {
                  Serial.println("Walker Sensed");
                  WalkerSenseCall = false;
                  TrafficControlState = WALKING_STATE;
                }

              }
              break;


            case WALKING_STATE:

              {
                Serial.println("WALKING");
                lcd.setCursor(9, 1); //Start at character 9 on line
                lcd.print(F("WALKING"));
                delay(1000);
                digitalWrite(WALK_LED_PIN, ON);
                delay(5000);
                digitalWrite(WALK_LED_PIN, OFF);
                StateDelaySeconds = 5;
                TrafficControlState = WALK_FAST_STATE;

              }
              break;

            case WALK_FAST_STATE:
              {
                Serial.println("WALK FAST");
                lcd.setCursor(9, 1); //Start at character 9 on line
                lcd.print(F("RUN ! !"));

                if (StateDelaySeconds >= 0)
                {
                  delay(500);
                  digitalWrite(WALK_LED_PIN, ON);
                  delay(500);
                  digitalWrite(WALK_LED_PIN, OFF);
                  Serial.println(StateDelaySeconds);
                  StateDelaySeconds -= 1;
                }
                else
                {
                  delay(1000);
                  StateDelaySeconds = MaxGreenSeconds;
                  TrafficControlState = GREEN_STATE;
                }

              }
              break;


              delay(500);
          } //End Switch on TrafficControlState
        } while (ForEver);  // Loop here "ForEver" or RESET


      } // END case

    case 9:
      {
        Serial.println("Option 9");
        lcd.setCursor(0, 0); //Start at character 0 on line 0
        //-------------------1234567890123456----------
        lcd.print(F("9:  NOPE        "));
        do
        {
          BlinkHi();
          delay(2000);
        } while (ForEver);  // Loop here "ForEver" or RESET


      } // END case

  } // End switch on OptionNumber

}//--(end main loop )---




/*--------------------( Declare User-written Functions )------------------------*/

void BeepNum(int numBeeps)
{
  int i;
  delay(1000);
  for (i = 0 ; i < numBeeps; i++)
  {
    digitalWrite(BUZZER_PIN, HIGH);
    delay(100);
    digitalWrite(BUZZER_PIN, LOW);
    delay(100);
  }

}

//-----------------( BlinkHi with switches )----------------
void BlinkHi()
{
  int i;
  for (i = 0; i < 4; i++)
  {
    digitalWrite(ONBOARD_LED_PIN, HIGH);   // turn the ONBOARD_LED_PIN on (HIGH is the voltage level)

    // Check if the user has pressed buttons
    if (digitalRead(SWITCH2_PIN) == LOW) {
      digitalWrite(LED_MODULE_PIN, HIGH);  // turn the LED_MODULE_PIN on (HIGH is the voltage level)
    }
    if (digitalRead(SWITCH1_PIN) == LOW) {
      digitalWrite(BUZZER_PIN, HIGH);
    }

    delay(100);               // wait for 1/10 second
    digitalWrite(ONBOARD_LED_PIN, LOW);    // turn the LED off by making the voltage LOW
    digitalWrite(LED_MODULE_PIN, LOW);     // turn the LED off by making the voltage LOW
    digitalWrite(BUZZER_PIN, LOW);
    delay(200);               // wait for a second, watch the Dark
  }
  delay(200);
  for (i = 0; i < 2; i++)
  {
    digitalWrite(ONBOARD_LED_PIN, HIGH);   // turn the LED on (HIGH is the voltage level)

    // Check if the user has pressed buttons
    if (digitalRead(SWITCH2_PIN) == LOW) {
      digitalWrite(LED_MODULE_PIN, HIGH);  // turn the LED_MODULE_PIN on (HIGH is the voltage level)
    }
    if (digitalRead(SWITCH1_PIN) == LOW) {
      digitalWrite(BUZZER_PIN, HIGH);
    }

    delay(100);               // wait for 1/10 second
    digitalWrite(ONBOARD_LED_PIN, LOW);    // turn the LED off by making the voltage LOW
    digitalWrite(LED_MODULE_PIN, LOW);     // turn the LED off by making the voltage LOW
    digitalWrite(BUZZER_PIN, LOW);
    delay(200);               // wait for a second, watch the Dark
  }
  delay(1000);
}//END BlinkHI

//------------( BlinkHiSimple - NO switches )-----------------
void BlinkHiSimple()
{
  int i;
  for (i = 0; i < 4; i++)
  {
    digitalWrite(ONBOARD_LED_PIN, HIGH);   // turn the ONBOARD_LED_PIN on (HIGH is the voltage level)
    digitalWrite(BUZZER_PIN, HIGH);
    digitalWrite(LED_MODULE_PIN, HIGH);
    digitalWrite(LED_MODULE_INVERT, LOW);



    delay(100);               // wait for 1/10 second
    digitalWrite(ONBOARD_LED_PIN, LOW);    // turn the LED off by making the voltage LOW
    digitalWrite(BUZZER_PIN, LOW);
    digitalWrite(LED_MODULE_PIN, LOW);     // turn the LED off by making the voltage LOW
    digitalWrite(LED_MODULE_INVERT, HIGH);

    delay(200);               // wait for a second, watch the Dark
  }



  delay(200);
  for (i = 0; i < 2; i++)
  {
    digitalWrite(ONBOARD_LED_PIN, HIGH);   // turn the ONBOARD_LED_PIN on (HIGH is the voltage level)
    digitalWrite(BUZZER_PIN, HIGH);
    digitalWrite(LED_MODULE_PIN, HIGH);
    digitalWrite(LED_MODULE_INVERT, LOW);


    delay(100);               // wait for 1/10 second
    digitalWrite(ONBOARD_LED_PIN, LOW);    // turn the LED off by making the voltage LOW
    digitalWrite(BUZZER_PIN, LOW);
    digitalWrite(LED_MODULE_PIN, LOW);     // turn the LED off by making the voltage LOW
    digitalWrite(LED_MODULE_INVERT, HIGH);

    delay(200);               // wait for a second, watch the Dark

  }

  delay(1000);
}//END BlinkHISimple



//-------------------( IsTime )----------------------------
boolean IsTime(unsigned long * timeMark, unsigned long timeInterval)
{
  if (millis() - *timeMark >= timeInterval)
  {
    *timeMark = millis();
    return true;
  }
  return false;
}

//-------------( PointServo )--------------
void PointServo(int ServoAngle)
{

  for (int i = 0; i < 3; i++) // Send the pulse 10 times
  {
    digitalWrite(SERVO_PIN, HIGH);
    delayMicroseconds(ServoAngle);
    digitalWrite(SERVO_PIN, LOW);
    delay(1);
  }
}//END  PointServo
/*---------------------------*/

//------------------( Translate IR - Read the IR Remote; change optionNumber )--------
void translateIR() // takes action based on IR code received

// describing KEYES Remote IR codes

{

  switch (results.value)

  {
    case 0xFF629D: Serial.println(" FORWARD"); break;
    case 0xFF22DD: Serial.println(" LEFT");    break;
    case 0xFF02FD: Serial.println(" -OK-");    break;
    case 0xFFC23D: Serial.println(" RIGHT");   break;
    case 0xFFA857: Serial.println(" REVERSE"); break;
    case 0xFF6897: {
        Serial.println(" 1");
        OptionNumber = 1;
      }    break;
    case 0xFF9867: {
        Serial.println(" 2");
        OptionNumber = 2;
      }    break;
    case 0xFFB04F: {
        Serial.println(" 3");
        OptionNumber = 3;
      }    break;
    case 0xFF30CF: {
        Serial.println(" 4");
        OptionNumber = 4;
      }    break;
    case 0xFF18E7: {
        Serial.println(" 5");
        OptionNumber = 5;
      }    break;
    case 0xFF7A85: {
        Serial.println(" 6");
        OptionNumber = 6;
      }    break;
    case 0xFF10EF: {
        Serial.println(" 7");
        OptionNumber = 7;
      }    break;
    case 0xFF38C7: {
        Serial.println(" 8");
        OptionNumber = 8;
      }    break;
    case 0xFF5AA5: {
        Serial.println(" 9");
        OptionNumber = 9;
      }    break;
    case 0xFF42BD: {
        Serial.println(" *");
        OptionNumber = 10;
      }    break;
    case 0xFF4AB5: {
        Serial.println(" 0");
        OptionNumber = 0;
      }    break;
    case 0xFF52AD: {
        Serial.println(" #");
        OptionNumber = 11;
      }    break;
    //   case 0xFFFFFFFF: Serial.println(" REPEAT"); break;

    default:
      Serial.println(" Bad Signal   ");

  }// End Case

  BeepNum(OptionNumber);

} //END translateIR

//*********( THE END )***********



</nowiki>