Tuesday, 18 June 2013

Major Project (Not Complete)

Major Project

For my major project I decided to expand on my original temperature reader.

Additions:
-A new automatic clock reader I found online. Requires an older version of the arduino environment I found on-line to run due to changes in keywords, but it's better than a manually in putted time.

-EEPROM write

-LEDs to alarm me when it is below 20 degrees celcius, and I should therefore light the fire.

Code:

const int temperaturePin = 0;

#include <EEPROM.h>
#include <Time.h>

#define TIME_MSG_LEN  11    // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER  'T'   // Header tag for serial time sync message
#define TIME_REQUEST  7    // ASCII bell character requests a time sync message

int addr = 0;
int led1 = 13;
int led2 = 12;

void setup()
{
  Serial.begin(9600);
  pinMode(led1, OUTPUT);  
  pinMode(led2, OUTPUT);

  Serial.begin(9600);
  setSyncProvider( requestSync);  //set function to call when sync required
  Serial.println("Waiting for sync message");
}

void loop()
{
  float voltage, degreesC, degreesF;
  int val = analogRead(0) / 4; //EEPROM so results so can be recovered if Arduino turns off

  voltage = getVoltage(temperaturePin);
  degreesC = (voltage - 0.5) * 100.0; //reads arduons voltage, and calculates that into celcius
  EEPROM.write(addr, val);
 
   if(Serial.available() )
  {
    processSyncMessage();
  }
  if(timeStatus()!= timeNotSet)
  {
    digitalClockDisplay();
  }
  delay(1000);
 
  if (degreesC > 10)
  {
    digitalWrite(led2, HIGH);
  }
  else if (degreesC < 10)
  {
    digitalWrite(led1, HIGH);
  }
 
  addr = addr + 1;
  if (addr == 512)
    addr = 0;
   
  delay(1000); //wait 1 hour
}

float getVoltage(int pin)

{
  return (analogRead(pin) * 0.004882814);
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(dayStr(weekday()));
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(monthShortStr(month()));
  Serial.print(" ");
  Serial.print(year());
  Serial.println();
}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

void processSyncMessage() {
  // if time sync available from serial port, update time and return true
  while(Serial.available() >=  TIME_MSG_LEN ){  // time message consists of a header and ten ascii digits
    char c = Serial.read() ;
    Serial.print(c);
    if( c == TIME_HEADER ) {      
      time_t pctime = 0;
      for(int i=0; i < TIME_MSG_LEN -1; i++){  
        c = Serial.read();        
        if( c >= '0' && c <= '9'){  
          pctime = (10 * pctime) + (c - '0') ; // convert digits to a number  
        }
      }  
      setTime(pctime);   // Sync Arduino clock to the time received on the serial port
    }
  }
}
time_t requestSync()

{
  Serial.print(TIME_REQUEST,BYTE);
  return 0; // the time will be sent later in response to serial mesg
}

Problems:
-Requires older version (less than 1.0) of the arduino environment to run the time reader

-EEPROM still glitchy. Not sure on problem. Worked at one point but no longer does

Photo:

Thursday, 13 June 2013

32 - String conversion

/*
String to Integer conversion
Reads a serial input string until it sees a newline, then converts
the string to a number if the characters are digits.
The circuit:
No external components needed.
created 29 Nov 2010
by Tom Igoe
This example code is in the public domain.
*/

String inString = ""; // string to hold input

void setup() {
// Initialize serial communications:
Serial.begin(9600);
}

void loop() {
// Read serial input:
while (Serial.available() > 0) {
int inChar = Serial.read();
if (isDigit(inChar)) {
// convert the incoming byte to a char
// and add it to the string:
inString += (char)inChar;
}
// if you get a newline, print the string,
// then the string's value:
if (inChar == '\n') {
Serial.print("Value:");
Serial.println(inString.toInt());
Serial.print("String: ");
Serial.println(inString);
// clear the string for new input:
inString = "";
}
}
}

31 - Button + Buzzer + LED count

int button1State = 0;
int count = 0;

int lastIncrementState = 0;
const int button1Pin = 2;
const int buzzerPin = 9;
const int led1 = 3;

void setup()
{
  // Here we'll configure the Arduino pins we're using
  // LEDs are to be outputs, and buttons are inputs:

  pinMode(button1Pin, INPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(led1, OUTPUT);
}

void displayCount()
{
    delay(10000);
    serial.println(count);
}
void loop()
{
    displayCount();
    if (button1State != lastIncrementState)
        {
              if (button1State == LOW)
             {
                  buzzerPin(tone);
                  digitalWrite(led1, HIGH);
                  count += 1;
             }

        lastIncrementState = button2State;

       }
       
}

30 - Buzzer & LED

int button1State = 0;
int lastIncrementState = 0;
const int button1Pin = 2;
const int buzzerPin = 9;
const int led1 = 3;

void setup()
{
  // Here we'll configure the Arduino pins we're using
  // LEDs are to be outputs, and buttons are inputs:

  pinMode(button1Pin, INPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(led1, OUTPUT);
}

void loop()
{
    if (button1State != lastIncrementState)
        {
              if (button1State == LOW)
             {
                  buzzerPin(tone);
                  digitalWrite(led1, HIGH);
             } 

        lastIncrementState = button2State;

       }
}

29 - Debouncer Solution

When pressing a button the button sends more than one signal.

To fix this you need to check twice in this short period of time if the button was pressed or not in the code, using variables HIGH & LOW.

When pressed, there will be a small delay. If HIGH, the button is pressed and if LOW the button is not pressed at that time.


28 -Buttons Serial Output

/*
  Button

 Turns on and off a light emitting diode(LED) connected to digital 
 pin 13, when pressing a pushbutton attached to pin 2.


 The circuit:
 * LED attached from pin 13 to ground
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground

 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.


 created 2005
 by DojoDave <http://www.0j0.org>
 modified 30 Aug 2011
 by Tom Igoe

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/Button
 */

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);     
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);   
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {   
    // turn LED on:
    Serial.print("Button is pressed... LED is on"); 
    digitalWrite(ledPin, HIGH); 
  }
  else {
    // turn LED off:
    Serial.print("Button is not pressed... LED is off");
    digitalWrite(ledPin, LOW);
  }
}

27 - Reverse Button

/*
  Button

 Turns on and off a light emitting diode(LED) connected to digital
 pin 13, when pressing a pushbutton attached to pin 2.


 The circuit:
 * LED attached from pin 13 to ground
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground

 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.


 created 2005
 by DojoDave <http://www.0j0.org>
 modified 30 Aug 2011
 by Tom Igoe

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/Button
 */


// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is LOW:
  if (buttonState == LOW) {     
    // turn LED off:    
    digitalWrite(ledPin, LOW);  
  } 
  else {
    // turn LED on:
    digitalWrite(ledPin, HIGH); 
  }
}

26 - Button

/*
  Button

 Turns on and off a light emitting diode(LED) connected to digital
 pin 13, when pressing a pushbutton attached to pin 2.


 The circuit:
 * LED attached from pin 13 to ground
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground

 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.


 created 2005
 by DojoDave <http://www.0j0.org>
 modified 30 Aug 2011
 by Tom Igoe

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/Button
 */


// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
  }
}

25 A/B - Output between 2 resistors

const int sensorPin = 0;
int lightLevel, high = 0, low = 1023;

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

void loop()
{
  lightLevel = analogRead(sensorPin);
  Serial.print(lightLevel);
  Serial.print("\n");

  delay(1000); 
}

25 - 100 sixes

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

long randNum;
int count = 1;
int sixCount = 0;

void loop()
{
  while (sixCount < 100)
  {
    randNum = random(1, 7);

    if (randNum == 6)
    {
      sixCount ++;
    }
    count++;
  }
 
  Serial.print("It took ");
  Serial.print(count); 
  Serial.print(" throws of the dice to reach 100 6's \n");
  delay(2000);
  count = 0;
  sixCount = 0;
}

24 - Random until 25 sixes

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

long randNum;
int count = 1;
int sixCount = 0;

void loop()
{
  while (sixCount < 25)
  {
    randNum = random(1, 7);
    delay(200);
    Serial.print("Roll number ");
    Serial.print(count);
    Serial.print(": ");
  
    if (randNum == 6)
    {
      sixCount ++;
    }
  
    Serial.println(randNum);
    count ++;
  }
}

23 - Number of sixes/dice throw

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

long randNum;
int count = 1;
int sixCount = 0;

void loop()
{
  while (count < 101)
  {
    randNum = random(1, 7);
    delay(1000);
    Serial.print("Roll number ");
    Serial.print(count);
    Serial.print(": ");
   
    if (randNum == 6)
    {
      sixCount ++;
    }
   
    Serial.print(randNum);
    Serial.print("\nAmount of sixes this far: ");
    Serial.print(sixCount);
    Serial.println("\n");
    count ++;
  }
}

22 - Dice

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

int count = 1;

void loop()
{
  while (count < 101)
  {
    delay(1000);
    Serial.print("Roll number ");
    Serial.print(count);
    Serial.print(": ");
    Serial.println(random(1,7));
    count ++;
  }
}

21 - Random

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

int count = 0;

void loop()
{
  while (count < 100)
  {
    Serial.println(random(1,7));
    count ++;
  }
}