Skip to content

Instantly share code, notes, and snippets.

@nenepadi
Last active June 20, 2019 18:05
Show Gist options
  • Select an option

  • Save nenepadi/4c36717db9ae23c0e020 to your computer and use it in GitHub Desktop.

Select an option

Save nenepadi/4c36717db9ae23c0e020 to your computer and use it in GitHub Desktop.
PublicGists

My first hack with the board

I am not so intelligent when it comes to electrronics or electricals or physics. But with the arduino board am ready to hack on to do awesome stuffs in that field. I was introduced to arduino by Elite education. Thanks to Chelsey and his team.What makes all this interesting is the fact that I didnt know what to really do with it. With time though I read through some hacks and I was delighted. At that point I really wanted to do something with it. They was this assignment given us by a lecturer and I suggested we used the arduino board. With my suggestion agreed to i had to setup really quick.

Arduino boards are quick to set up. Basically I found all the information I needed here. After setting up and going through some few examples, I started to tackle the task given us by the lecturer, a traffic light system. First since the task was a group task I had to put everyone together and come out with ideas on how to do it. At first we thought of making it big, but without the resources we were forced to use the only thing I had, the arduino board, the leds, the wires and the basic stuffs that come with the arduino.

Building the hardware part of the whole system was a real tussle. I had to go back and read some more on electricals. At the end I did something really simple. This is what I used:

  • 5 leds (2 reds, 2 greens and 1 amber)
  • A breadboard
  • The arduino board
  • Wires
  • Usb cable to connect the board to my PC

The following are the steps I took to setup the hardware:

  • Join the longer part of the leds together with a wire. Basically a soldering iron will do the magic but I had none so I used my hand. I took one red, yellow and green leds each and i join the longer pins to one another and afterwards joined it to a wire. The longer part of the led is negatively charged. Joining them together made it easier for me to have one common ground or earth as my local folks call it.
  • Join a wire to each of the shorter parts. The shorter pin of the led is positively charged and as such that will do the real action. I joined a wire each to this part of the led and fixed the whole setup to the breadboard, thus I have the traffic light system without the pedestrain system setup.
  • Extend some wires from the breadboard to the arduino board. Next of, I had to extend the breadboard setup to the microcontroller(arduino board) to give it full functionality. I fixed the grounded part of the leds to pin GND. The +pin of red, amber and green leds were fixed to pin 12, 10 and 8 respectively. All on the arduino board.

Time to go. I will continue with how I fixed the pedestrain lights and then writeup the code I used for the functionality. Meanwhile you can try my setup if you have the tools. You can also find some help here.

Ardios

My code

Holla people, My code for the arduino project is below. I am in a hurry today. I will tell you how it all works in my next post.

//pin numbers...
const unsigned int RED = 6;
const unsigned int YELLOW = 4;
const unsigned int GREEN = 2;
const unsigned int PRED = 8;
const unsigned int PGREEN = 9;
const unsigned int PAUSE = 700;

//variables for the sake of monitoring
//const int sensorAlert = ;
const int sensor = A0;
const float carSpeed = 5.0;

//variables that will change
int sensorReading = 0;
int ledState = LOW;

//traffic delays
int DELAY_GREEN = 15000;
int DELAY_YELLOW = 3000;
int DELAY_RED = 10000;


void setup(){
  pinMode(RED,OUTPUT);
  pinMode(YELLOW,OUTPUT);
  pinMode(GREEN,OUTPUT);
  //pinMode(sensorAlert, OUTPUT);
  pinMode(PRED,OUTPUT);
  pinMode(PGREEN,OUTPUT);
  
  Serial.begin(9600);
}

void loop(){
  //*****CONTROL SYSTEM******//
  if (Serial.available() > 0){
    int command = Serial.read();
    if(command == '1'){
      while(command == '1'){
        //******VEHICULAR AND PEDESTRAIN SYSTEM******//
        //Greenled on
        green_light();
        /*
         * When greenlight is on determine cars speed...
         * When the car's speed is beyond maximum alert the police...
         * But because it is a test product we just print something to the screen... 
         */
        //*****MONITORING SYSTEM********//
        if(ledState == HIGH){
          sensorReading = analogRead(sensor);
          Serial.println(sensorReading);
          if(determine_car_speed(sensorReading) > carSpeed){
            //ledState = !ledState;
            //digitalWrite(sensorAlert, ledState);
            Serial.println("Call the police");
            Serial.println("There is a lunatic on our roads");
          }
        }
        delay(DELAY_GREEN);
        
        //Yellow light on
        amber_light();
        digitalWrite(PRED, HIGH);
        delay(DELAY_YELLOW);
        
        //Red light on
        red_light();
        //******STILL MONITORING******//
        //monitor humans...
        if(ledState == HIGH){
          if(sensorReading < 450){
            DELAY_RED = DELAY_RED + 2000;
          }
        }
        delay(DELAY_RED);
      }
      } else if(command == '2'){
        while(command == '2'){
        //activate only the yellow light...
        //boolean instruction = true;
        all_lights_off();
        blinking(YELLOW);
        //instruction = false;
        }
      } else{
        Serial.println("*****************************************");
        Serial.println("You cant be in charge of this system");
        Serial.println("You fed me the wrong input");
        Serial.println("*****************************************");
    }   
  }
}

/***************************************************************
***************************FUNCTIONS****************************/

void all_lights_off(){
  digitalWrite(GREEN, LOW);
  digitalWrite(YELLOW, LOW);
  digitalWrite(RED, LOW);
  digitalWrite(PRED, LOW);
  digitalWrite(PGREEN, LOW);
}

void green_light(){
  all_lights_off();
  ledState = HIGH;
  //vehicular light...
  digitalWrite(GREEN, ledState);
  //pedestrain light...
  digitalWrite(PRED, ledState);
}

void amber_light(){
  all_lights_off();
  ledState = HIGH;
  digitalWrite(YELLOW, ledState);
}

void red_light(){
  all_lights_off();
  ledState = HIGH;
  //vehicular light...
  digitalWrite(RED, ledState);
  //pedestrain light...
  digitalWrite(PGREEN, ledState);
}

//monitoring functions...
float determine_car_speed(int reading){
  float speedResult;
  speedResult = reading / 120;
  return speedResult;
}

void blinking(int PIN){
    digitalWrite(PIN, HIGH);   
    delay(1000);             
    digitalWrite(PIN, LOW);    
    delay(1000);
}
/***************************************************************/

@rpip
Copy link

rpip commented Jun 30, 2014

Great post!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment