Sunday, June 20, 2010

Major Project

My major project is an extended version of my minor project, The Binary Coded Decimal Light Emitting Diode Chronological Gauging Mechanism (BCDLEDCGM). I have added some extra features to the BCDLEDCGM including, the ability to set the hours and minutes, the ability to set it to 12 or 24 hour time and the ability to set an alarm to go off at a specific hour and minute.

Here is an image and some video footage of the BCDLEDCGM:


































Here is the code:


////Pin connected to DS of 74HC595
const int dataPin = 11;

//Pin connected to SH_CP of 74HC595
const int clockPin = 12;

//Pin connected to ST_CP of 74HC595
const int latchPin = 13;

const int switchModeButton = 2;
const int actionButton = 6;
const int hourButton = 4;
const int minuteButton = 5;
const int buzzer = 10;

int minuteSetNum;
int hourSetNum;
int setMinutes;
int setHours;
int shiftAlarmSeconds;
int shiftAlarmMinutes;
int shiftAlarmHours;
int alarmMinutes;
int alarmHours;
int alarmSet;
int hourMode = 0;
int clockMode = 1;




#define SECS_PER_MIN (60UL)

#define numberOfSeconds(_time_) (_time_ % SECS_PER_MIN)


void setup()
{
Serial.begin(9600);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(switchModeButton, INPUT);
pinMode(actionButton,INPUT);
pinMode(hourButton, INPUT);
pinMode(minuteButton, INPUT);
}
void loop()
{
if (clockMode == 1)
{
int modeButtonState = digitalRead(switchModeButton);
if (modeButtonState == 1)
clockMode = 0;
int actionButtonState = digitalRead(actionButton);
if (actionButtonState == 1)
if (hourMode == 1)
hourMode = 0;
else
hourMode = 1;
int seconds = shiftSeconds();
int minutes = shiftMinutes(seconds);
int hours = shiftHours(seconds, minutes, hourMode);
if (alarmSet == 1)
if((minutes == shiftAlarmMinutes) && (hours == shiftAlarmHours))
tone(buzzer, 880, 500);
showTime(hours, minutes, seconds);
}
else
{
int modeButtonState = digitalRead(switchModeButton);
if (modeButtonState == 1)
clockMode = 1;
int actionButtonState = digitalRead(actionButton);
if (actionButtonState == 1)
if (alarmSet == 1)
alarmSet = 0;
else
alarmSet = 1;
int seconds = shiftSeconds();
shiftAlarmSeconds = alarmShiftSeconds();
shiftAlarmMinutes = alarmShiftMinutes(seconds);
shiftAlarmHours = alarmShiftHours(seconds, hourMode);
showTime(shiftAlarmHours, shiftAlarmMinutes, shiftAlarmSeconds);
}
delay(500);
}


int shiftSeconds()
{
unsigned long time = millis() / 1000;
int tenSeconds = ((numberOfSeconds(time) - (numberOfSeconds(time) % 10))/ 10);
int oneSeconds = numberOfSeconds(time) % 10 ;
return ((tenSeconds<<4) + oneSeconds);
}


int shiftMinutes(int seconds)
{
int buttonState = digitalRead(minuteButton);
if (buttonState == 1)
minuteSetNum = (minuteSetNum + 1) % 60;
if (seconds == 0)
{
setMinutes = (setMinutes + 1) % 60;
delay(750);
}
int minutes = (setMinutes + minuteSetNum) % 60;
int tenMinutes = ((minutes - (minutes % 10))/ 10);
int oneMinutes = minutes % 10 ;
return ((tenMinutes<<4) + oneMinutes);
}


int shiftHours(int seconds, int minutes, int mode)
{
int buttonState = digitalRead(hourButton);
if (buttonState == 1)
hourSetNum = (hourSetNum + 1) % 24;
if ((seconds == 0) && (minutes == 0))
{
setHours = (setHours + 1) % 24;
delay(750);
}
int hours = (setHours + hourSetNum) % 24;
if (mode == 0)
hours = (hours % 12) + 1;
int tenHours = ((hours - (hours % 10))/ 10);
int oneHours = hours % 10 ;
return ((tenHours<<4) + oneHours);
}


int alarmShiftSeconds()
{
if (alarmSet == 1)
return 255;
else
return 0;
}


int alarmShiftMinutes(int seconds)
{
int buttonState = digitalRead(minuteButton);
if (buttonState == 1)
alarmMinutes = (alarmMinutes + 1) % 60;
if (seconds == 0)
{
setMinutes = (setMinutes + 1) % 60;
delay(750);
}
int minutes = alarmMinutes % 60;
int tenMinutes = ((minutes - (minutes % 10))/ 10);
int oneMinutes = minutes % 10 ;
return ((tenMinutes<<4) + oneMinutes);
}


int alarmShiftHours(int seconds, int mode)
{
int buttonState = digitalRead(hourButton);
if (buttonState == 1)
alarmHours = (alarmHours + 1) % 24;
if ((seconds == 0) && ((setMinutes + minuteSetNum) == 0))
{
setHours = (setHours + 1) % 24;
delay(750);
}
int hours = alarmHours % 24;
if (mode == 0)
hours = (hours % 12) + 1;
int tenHours = ((hours - (hours % 10))/ 10);
int oneHours = hours % 10 ;
return ((tenHours<<4) + oneHours);
}


void showTime(int hours, int minutes, int seconds)
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST,hours);
shiftOut(dataPin, clockPin, LSBFIRST,minutes);
shiftOut(dataPin, clockPin, LSBFIRST,seconds);
digitalWrite(latchPin, HIGH);
}

Wednesday, June 16, 2010

Tasks 54 to 59: Data Logging

Task 54:

int photocellPin = 0;
int photocellReading;

void setup() {
Serial.begin(9600);

for (int i = 0; i <10; i++)
{
photocellReading = analogRead(photocellPin);
Serial.println(photocellReading);
delay(1000);
}
}

void loop()
{
}


Task 55:


int photocellPin = 0;
int photocellReading;
int mySamples[10];


void setup()
{
Serial.begin(9600);

for (int i = 0; i <10; i++)
{
photocellReading = analogRead(photocellPin);
mySamples[i] = photocellReading;
delay(1000);
}
}

void loop()
{
}


Task 56:


#include

int photocellPin = 0;
int photocellReading;
int EEPROMVal;

void setup() {
Serial.begin(9600);

for (int i = 1; i <=10; i++)
{
photocellReading = analogRead(photocellPin);
EEPROM.write(i, photocellReading);
delay(1000);
}
delay(5000);
for (int i = 1; i <=10; i++)
{
EEPROMVal = EEPROM.read(i);
Serial.println(EEPROMVal);
}
}

void loop()
{
}


Task 57:


#include

int photocellPin = 0;
int photocellReading;
int EEPROMVal;


void setup() {
Serial.begin(9600);
for (int i = 1; i <=10; i++)
{
photocellReading = analogRead(photocellPin);
EEPROM.write(i, photocellReading);
delay(1000);
}
delay(5000);
for (int i = 1; i <=10; i++)
{
EEPROMVal = EEPROM.read(i);
Serial.println(EEPROMVal);
}
smallest();
}


void smallest()
{
int smallestVal = 255;
for (int i = 1; i <=10; i++)
{
EEPROMVal = EEPROM.read(i);
if (EEPROMVal < smallestVal)
smallestVal = EEPROMVal;
}
Serial.println();
Serial.print(smallestVal);
Serial.println(" is the smallest value.");
}


void loop()
{
}


Task 58:


#include

int photocellPin = 0;
int photocellReading;
int EEPROMVal;


void setup() {
Serial.begin(9600);
for (int i = 1; i <=10; i++)
{
photocellReading = analogRead(photocellPin);
EEPROM.write(i, photocellReading);
delay(1000);
}
delay(5000);
for (int i = 1; i <=10; i++)
{
EEPROMVal = EEPROM.read(i);
Serial.println(EEPROMVal);
}
smallest();
biggest();
average();
}


void smallest()
{
int smallestVal = 255;
for (int i = 1; i <=10; i++)
{
EEPROMVal = EEPROM.read(i);
if (EEPROMVal < smallestVal)
smallestVal = EEPROMVal;
}
Serial.println();
Serial.print(smallestVal);
Serial.println(" is the smallest value.");
}


void biggest()
{
int biggestVal = 0;
for (int i = 1; i <=10; i++)
{
EEPROMVal = EEPROM.read(i);
if (EEPROMVal > biggestVal)
biggestVal = EEPROMVal;
}
Serial.println();
Serial.print(biggestVal);
Serial.println(" is the biggest value.");
}


void average()
{
float averageVal;
int totalVal = 0;
for (int i = 1; i <=10; i++)
{
EEPROMVal = EEPROM.read(i);
totalVal += EEPROMVal;
}
averageVal = totalVal / 10;
Serial.println();
Serial.print(averageVal);
Serial.println(" is the average.");
}


void loop()
{
}


Task 59:


Logging code:

#include

int photocellPin = 0;
int photocellReading;

void setup()
{
Serial.begin(9600);
for (int i = 1; i <=144; i++)
{
photocellReading = analogRead(photocellPin);
EEPROM.write(i, photocellReading);
Serial.println(photocellReading);
delay(600000);
}
}
void loop()
{
}


Recalling code:


#include

int EEPROMVal;

void setup()
{
Serial.begin(9600);
for (int i = 1; i <=144; i++)
{
EEPROMVal = EEPROM.read(i);
Serial.println(EEPROMVal);
}
}
void loop()
{
}


Data recorded in Bar Graph form:

Monday, June 14, 2010

Tasks 51 to 53: EEPROM

Task 51:











#include


int a = 0;
int value;

void setup()
{
Serial.begin(9600);
}

void loop()
{
value = EEPROM.read(a);

Serial.print(a);
Serial.print("\t");
Serial.print(value);
Serial.println();

a = a + 1;

if (a == 1023)
a = 0;

delay(500);
}



Task 52:











#include

int a = 0;
int value;

void setup()
{
Serial.begin(9600);
EEPROM.write(01, 35);
EEPROM.write(02, 42);
for (int i = 0; i <= 20; i++)
{
value = EEPROM.read(i);
Serial.print(i);
Serial.print("\t");
Serial.print(value);
Serial.println();
}
}

void loop()
{

}



Task 53:











#include

int a = 0;
int value;

void setup()
{
Serial.begin(9600);
for (int i = 1; i <= 1023; i++)
{
EEPROM.write(i, 171);
}
for (int i = 0; i <= 1023; i++)
{
value = EEPROM.read(i);
Serial.print(i);
Serial.print("\t");
Serial.print(value);
Serial.println();
delay(100);
}
}

void loop()
{

}

Friday, May 21, 2010

Minor Project - Complete

I have finally got the wiring sorted out so it is now working correctly.

My Minor Project is a Binary-Coded Decimal (BCD) Light Emmiting Diode (LED) Chronological Gauging Mechanisum(CGM).

The BCDLEDCGM is laid out like this:

Hours

*
8
*
4
* *
2 2
* *
1 1

Minutes

*
8
* *
4 4
* *
2 2
* *
1 1

Seconds
*
8
* *
4 4
* *
2 2
* *
1 1


Here are some pictures of it working:











This picture shows the clock at 12:07am in 24hr time.











This picture shows the clock at 2:22am in 24hr time.






This is the sketch that the Arduino is running:


//Pin connected to DS of 74HC595
const int hourDataPin = 5;
const int minDataPin = 8;
const int secDataPin = 11;

//Pin connected to SH_CP of 74HC595
const int hourClockPin = 6;
const int minClockPin = 9;
const int secClockPin = 12;

//Pin connected to ST_CP of 74HC595
const int hourLatchPin = 7;
const int minLatchPin = 10;
const int secLatchPin = 13;


// macros from DateTime.h
/* Useful Constants */
#define SECS_PER_MIN (60UL)
#define SECS_PER_HOUR (3600UL)
#define SECS_PER_DAY (SECS_PER_HOUR * 24L)

/* Useful Macros for getting elapsed time */
#define numberOfSeconds(_time_) (_time_ % SECS_PER_MIN)
#define numberOfMinutes(_time_) ((_time_ / SECS_PER_MIN) % SECS_PER_MIN)
#define numberOfHours(_time_) (( _time_% SECS_PER_DAY) / SECS_PER_HOUR)
#define elapsedDays(_time_) ( _time_ / SECS_PER_DAY)

void setup()
{
pinMode(hourLatchPin, OUTPUT);
pinMode(minLatchPin, OUTPUT);
pinMode(secLatchPin, OUTPUT);
pinMode(hourClockPin, OUTPUT);
pinMode(minClockPin, OUTPUT);
pinMode(secClockPin, OUTPUT);
pinMode(hourDataPin, OUTPUT);
pinMode(minDataPin, OUTPUT);
pinMode(secDataPin, OUTPUT);

void loop()
{
unsigned long time = millis() / 1000;

int tenHours = ((numberOfHours(time) - (numberOfHours(time) % 10))/ 10);
int oneHours = numberOfHours(time) % 10 ;
int tenMinutes = ((numberOfMinutes(time) - (numberOfMinutes(time) % 10))/ 10);
int oneMinutes = numberOfMinutes(time) % 10 ;
int tenSeconds = ((numberOfSeconds(time) - (numberOfSeconds(time) % 10))/ 10);
int oneSeconds = numberOfSeconds(time) % 10 ;

int shiftHours = ((tenHours<<4) + oneHours);
int shiftMinutes = ((tenMinutes<<4) + oneMinutes);
int shiftSeconds = ((tenSeconds<<4) + oneSeconds);

digitalWrite(hourLatchPin, LOW);
digitalWrite(minLatchPin, LOW);
digitalWrite(secLatchPin, LOW);
// shift out the bits:
shiftOut(hourDataPin, hourClockPin, LSBFIRST,shiftHours);
shiftOut(minDataPin, minClockPin, LSBFIRST,shiftMinutes);
shiftOut(secDataPin, secClockPin, LSBFIRST,shiftSeconds);
//take the latch pin high so the LEDs will light up:
digitalWrite(hourLatchPin, HIGH);
digitalWrite(minLatchPin, HIGH);
digitalWrite(secLatchPin, HIGH);

delay(1000);
}

Thursday, May 13, 2010

Task 46, 47, 48, 49 and 50

Task 46:

Here is the senders code:

const int dataSend = 13;

void setup()
{
pinMode(dataSend, OUTPUT);
}

void loop()
{
digitalWrite(dataSend, HIGH);
delay(8000);
digitalWrite(dataSend, LOW);
delay(2000);
}


Here is the receivers code:


Here is the senders code:

const int dataRec = 2;

int recData;

void setup()
{
Serial.begin(9600);
pinMode(dataRec, INPUT);
}

void loop()
{
recData = digitalRead(dataRec);
if (recData == HIGH)
{
Serial.print(recData);
}
else
{
Serial.println(recData);
}
delay(500);
}





Task 47:

The Roles were switched on each Arduino.




Task 48:

The sender's code:

const int dataSend = 13;
const int dataRec = 2;

int recData;
int dataState;


void setup()
{
Serial.begin(9600);
pinMode(dataSend, OUTPUT);
pinMode(dataRec, INPUT);
}

void loop()
{
dataState++ % 2;
digitalWrite(dataSend, dataState);
for (int count = 1; count <= 20; count++)
{
recData = digitalRead(dataRec);
if (recData == HIGH)
{
Serial.print(recData);
}
else
{
Serial.println(recData);
}
delay(50);
}
}


The receiver's code:


const int dataSend = 13;
const int dataRec = 2;

int recData;
int dataState;


void setup()
{
Serial.begin(9600);
pinMode(dataSend, OUTPUT);
pinMode(dataRec, INPUT);
}

void loop()
{
for (int count = 1; count <= 20; count++)
{
recData = digitalRead(dataRec);
if (recData == HIGH)
{
digitalWrite(dataSend, dataRec);
Serial.print(recData);
}
else
{
digitalWrite(dataSend, dataRec);
Serial.println(recData);
}
delay(50);
}
}



Task 49:

Sender's code:

const int dataSend = 13;

int state;

void setup()
{
pinMode(dataSend, OUTPUT);
for (int i = 1; i <= 200; i++)
{
state = i % 2;
digitalWrite(dataSend, state);
delay(1);
}

}

void loop()
{
}



Recievers code:


const int dataRec = 2;

int recData;
int pulseCount = 0;

void setup()
{
Serial.begin(9600);
pinMode(dataRec, INPUT);
}

void loop()
{
recData = digitalRead(dataRec);
if (recData == HIGH)
{
pulseCount++;
Serial.print(pulseCount);
delayMicroseconds(1500);
}
}


Task 50:

Did not have two Arduinos to test speeds.

Task 45

Task 45:


Here is the code that i am using for the IR sender and receiver on the same Arduino.

const int infraSender = 13;
const int infraReceiver = 2;

int infra;

void setup(){
  pinMode(infraReceiver,INPUT);
  pinMode(infraSender,OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  for (int i = 1; i < 21; i ++)
  {
  digitalWrite(infraSender,HIGH);
  infra = digitalRead(infraReceiver);
  delay(1000);
 
    if (infra == HIGH)
    {
      Serial.println(i);
  }
  else{
    Serial.print(i);
   
    Serial.println(" Blocked");
  }
Serial.println(infra);
}
Serial.println("Finished");
delay(10000);
}

Task 44

Task 44:

I was in the group that investigated the Passive Infrared (PIR) Sensor.