Sunday, November 6, 2011

Project 4 -- Major Project: initial survey..

Since this project involves THERMISTORS, i've gone to my other tech blog to see what i've done there.

And this entry has a handy data-table ^..^ .

The idea is to make a strip of about 4 or 5 thermistors to hang inside the door of my fridge; i want to see how much colder it is at the bottom compared to the top.

I know it is colder at the bottom, because meat keeps longer there, & sometimes starts to freeze, whereas it never does  that nearer the top.

I need some of that ribbon cable to 'insert' the thermistors into, & also to transmit data to the analog pins on my Arduino.  i also have a USB power supply, so i could actually do some data-logging over an extended period & save the data to EEPROM for later retrieval & display (& just by chance i've found a LED-strip with about 10 LEDs on it that i'd forgotten i had!).

I'd like to add TWI to a 24LCxx if i can, as that would allow more data to be stored.

A photo-cell (these entries) could tell me when the fridge door was opened.

Another little wrinkle would be to average all the thermistor readings at any one time, &  switch on a motor, say, if the average temperature rises above a preset value.

First things first, however: i need to set up the thermistor strip, write the basic software, & get that part working;  i can add bells & whistles later.

Wednesday, November 2, 2011

PROGMEM - some interesting stuff from Peter B

PROGMEM example

/*
 * Project:    PROGMEM_example
 * Author:     Peter Brook, tidied by Jane-Maree Howard
 * Date:       Tuesday 01/11/2011
 * Platform:   Arduino 22
 * Purpose:    To demonstrate storing data in Arduino's flash memory
 * Operation:  Description: Stores 10x10 matrix (int) in Flash memory
 *                          using the PROGMREM keywords & library
 *             Declare:     formatting buffer (char); word counter (int);
 *                          10X10 data matrix - myData[10][10] (int)
               Setup():     Serial@38400baud:TAKE CARE TO SET SM CORRECTLY         
               Loop():      Print to SM by ROWS, then by COLUMNS; 
 *                          setup() again & loop()
 */
#include {avr/pgmspace.h}      // PROGMEM library
 char chBuffer [5];            // Serial output formatting buffer
 word serialCount;             // word count (2 Bytes)
void setup(void)
{
    Serial.begin(38400);       // Serial connection @ 38400 baud!!
    Serial.println("\nPress space bar .");
//    while (Serial.read() != 32)   delay(1); // Wait for space 
}//setup()

// this is the data to be stored
const int myData[10][10] PROGMEM = {
    '0', '1', '2', '3', '4', '5', '6', '7', '8', 'A',
    '1', '1', '2', '3', '4', '5', '6', '7', '8', 'B',
    '2', '1', '2', '3', '4', '5', '6', '7', '8', 'C',
    '3', '1', '2', '3', '4', '5', '6', '7', '8', 'D',
    '4', '1', '2', '3', '4', '5', '6', '7', '8', 'E',
    '5', '1', '2', '3', '4', '5', '6', '7', '8', 'F',
    '6', '1', '2', '3', '4', '5', '6', '7', '8', 'G',
    '7', '1', '2', '3', '4', '5', '6', '7', '8', 'H',
    '8', '1', '2', '3', '4', '5', '6', '7', '8', 'I',
    '9', '1', '2', '3', '4', '5', '6', '7', '8', 'Z' };

void loop(void) 
{
    int i,j;      // loop counters
    byte n;       // Byte output vble
    
    // print heading for ROWS
    Serial.println("\nData by Rows.");
    for (i=0; i < 100;i++) 
    {
       // some formatting conditions
       if ((i %8)==0) 
          Serial.print("|");
       if ((i %16)==0) 
          Serial.println("");
       // now read a Byte from Flash memory
       n = pgm_read_byte_near(i);
       Serial.print(" ");
       // format output to Serial
       sprintf(chBuffer, "%02x", n);
       // print output 
       Serial.print(chBuffer);
       // next word (= 2 Bytes)
       serialCount++; 
     }//for (i)
    
    // print heading for COLUMNS
    Serial.println(" ");
    Serial.println("\nData by Columns.");
    for (i=0; i < 10; i++) 
    {
      Serial.println(" ");
      for (j=0; j < 10; j++) 
      {
        // print each column entry in BYTE format
        n = pgm_read_word_near(&myData[j][i]);
        Serial.print(n, BYTE);   
      }//for(j)
    }//for (i)
    
    // now set up to go round again
    Serial.println(" ");
    delay(3000);   
    setup();    
    Serial.println(" ");  //start over
}//loop()
/* END */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
See the following links to find out how some of this stuff works.. 
PROGMEM      printf
 

Wednesday, October 26, 2011

Task 40 - Timer0 Polling routine - toggle LED on/off

/*
 * Project:      Timer_0_Polling_Experiment
 * Author:      Peter Brook tidied by Jane-Maree Howard on..
 * Date:          ..Wednesday 26/10/2011
 * Platform:    Arduino 22
 * Purpose:     To visually demonstrate Timer0 polling
 * Operation:  Polls Timer0 prescaled by 1024; toggles LED on/off every second
 *      Declare: toggle variable; counter vble; ToggleLED() routine 
 *      Setup():  Serial port @9600baud; Prints opening statement; delays 5 seconds
 *                      set TImerMaSK0 to zero - turn of Timer0 interrupt;
 *                      set Timer/CounterControlRegister0B to DEC5; prescaler /s by 1024;
 *      Loop():    idle until TimerInterruptFlagRegister0 == 1;
 *                      reset TIFR0; count 1 'tick'(from 0 to 60, 61 'ticks');
 *                      @ 60'ticks', reset 'ticks' & toggle LED on/off
 */
int     state = 1;        // LED toggle vble - init. OFF!      
byte  ticks = 0;        // counts up towards 60, the LED toggle value

void setup()
{
  Serial.begin(9600);  // Serial port @ 9600 baud
  Serial.println("\n\tInitial state is OFF\n\t");
  delay(5000);            // wait 5 seconds 
  // initialize digital pin 13 as an output.
  // Pin 13 has an LED connected on most Arduino boards:

  pinMode(13, OUTPUT);
  //after this, can't use 'delay() any more
  TIMSK0 = 0;          // turn off Interrupt for Timer0
  TCCR0B = 5;         // Prescaler divides by 1024;  BIN101 = DEC5
}//setup()

void loop()
{
   while(byte(TIFR0 &0x01) ==0) // loop till flag goes up
   {/* do nothing */}                       
   TIFR0 = 1;                // clear flag
   ticks++;                     // count a tick
 
   if (ticks == 60)          // preset value
   {
      ticks=0;                  // reset 'ticks'
      Serial.print("\tTT ");
      ToggleLED();        // toggle LED
   }//if()
}// loop()

void ToggleLED()
{
  if (state==1)
  {
    digitalWrite(13,LOW);                    // LED turns OFF
    state = 0;                                          // toggle 'state' vble
    Serial.print("I've toggled OFF");   // Serial Monitor o/p
  }//if()
  else
  {
    digitalWrite(13,HIGH);                    // LED turns ON
    state=1;                                             // toggle 'state' vble
    Serial.println("I've toggled ON "); // Serial Monitor o/p
  }//else()
}//ToggleLED() 



In the code, the initial state of the on-board LED (pin 13) is OFF.


The SM output records the first toggle ("TT") & the LED
(of course ) stays OFF.

From then on, the LED's recorded toggled state matches that of the on-board LED (pin 13)

Tasks 40-41 -- Automation&Robotics: schedule 8

  • 40.. Copy the Timer0 Polling routine
             given to you in a previous post,
    and run it with some tidying up.
    Show your tidy code.
    ~
  • 41.. Repeat the program in 40,
             but this time make it work a one second LED flash
    using an Interrupt Service Routine.
    ~


Thursday, October 20, 2011

Task 38a - using the I2C bus with an EEPROM 24LC64 addressed differently

I've added a line to the earlier code coz i want to make it easier to change the device address.


 In this particular case:
  A0 is LOW;
  A1 is HIGH;
  A2 is LOW.


This device needs to be addressed as 0x52
(see code changes below)


#include {Wire.h}           // I2C library - usual brackets warning

byte  LCaddress  = 0x52;    // 24LC64 device address
:
:
// now write the char-string to the EEPROM - NOTE 0x52:address pin A1 is HIGH
i2c_eeprom_write_page(LCaddress, 0, (byte *)somedata, sizeof(somedata));
:
:
// now access the first address (0) from the memory
 byte b = i2c_eeprom_read_byte(LCaddress, 0);   //NOTE 0x52:address pin A1 is HIGH
:
:
 addr++;                //..next address &..
//..access next address from the memory.
b = i2c_eeprom_read_byte(LCaddress, addr);      //NOTE 0x52:address pin A1 is HIGH
:
:
etcetera..

& i've changed the input data just to make sure the program's doing what it should be doing..
 
char somedata[] = "Jane-Maree's EEPROM data";  // - 24 characters..



And this is what it does..










..& when i've written..

byte  LCaddress  = 0x51;    // 24LC64 device address


 ..it's because.. 

  A0 is HIGH;
  A1 is LOW;
  A2 is LOW.


..& this device needs to be addressed as 0x51..



..producing this  ==} 




i've got pikkies of the breadboard arrangement clearly (i hope) showing the connections to A0, A1, & A2.


Everything else is unchanged..







Wednesday, October 19, 2011

Task 38.1 - using the I2C bus with an EEPROM 24LC64: software edit

/*
 * Project:      eeprom_fromWeb
 * Author:      hkhijhe - tidied even more by Jane-Maree Howard
 * Date:         01/10/2010
 * Platform:   Arduino 22
 * Purpose:    To demonstrate use of the I2C bus with an EEPROM 24LC64
 -             "You were issued with an eeprom 24LC64 IC.
 -              This is a TWI memory that you connect to SCL and SDA on the Arduino.
 -              Run the program from the blog.
 -              Show your code changes and the serial monitor output in your blog".
 * Operation:  Because this chip is I2C,
 -               it only uses Arduino analog pins 4 & 5 (SDA and SCL),
 -               and of course the power (5V) and GND.
 -             Connect as follows:
 -                Arduino pin 4 to EEPROM pin 5
 -                Arduino pin 5 to EEPROM pin 6
 -                Arduino 5V to EEPROM pin 8
 -                Arduino GND to EEPROM pin 1,2,3,4
 -             Be sure to leave pin 7 of the EEPROM open or tie it to GND,
 -               otherwise the EEPROM will be write protected.
 -             Include: Wire.h - the I2C library
 -             Declare: various i2c_eeprom_read.. i2c_eeprom_write.. procedures
 -             Setup(): Opens the Wire connection, the Serial connection,
 -                      writes a char-string to the EEPROM, & prints heading
 -                      NOTE: the char-string has length = 28
 -             Loop():  Reads from the EEPROM, & prints, one byte at a time, & repeats                
 */

#include {Wire.h}               //I2C library-usual brackets warning

  void setup()
  {
    // a char-string of data to write to the EEPROM - 28 characters long!!
    char someData[] = "this is data from the eeprom";
   /*
    char somedata[] = "this is Jane-Maree's eeprom data"; - 32 characters..
     .. & it bombs.  Gibberish - I've tried it!  See WARNING below..
   */
    Wire.begin();                 // Wire connection initialised
    Serial.begin(9600);       // Serial connection @ 9600 baud
    // now write the char-string to the EEPROM
    i2c_eeprom_write_page(0x50, 0, (byte *)someData, sizeof(someData));
    delay(10);                     //add a small delay..
    // ..& print a heading
    Serial.println("\nMemory written");
    Serial.println("\nNow read memory & print to Serial port");
  } //setup()

  void loop()
  {
    int addr=0;               // address parameter, starts @ 0
    // now access the first address (0) from the memory
    byte b = i2c_eeprom_read_byte(0x50, 0);
    // while there are non-zero bytes..
    while (b!=0)
    {
      Serial.print((char)b); //..print content as char to serial port..
      addr++;                      //..next address &..
      //..access next address from the memory.
      b = i2c_eeprom_read_byte(0x50, addr);
    }//while ()
    Serial.println(" ");     // new line..
    delay(2000);               // ..2 second delay, & start again!
  } //loop()


   /*
    WARNING: address is a page address, 6-bit end will wrap around
    also, data can be maximum of about 30 bytes,
    because the Wire library has a buffer of 32 bytes
  */
  void i2c_eeprom_write_page( int deviceAddress, unsigned int eeAddressPage, byte* data, byte length )
  {
    Wire.beginTransmission(deviceAddress);
    Wire.send((int)(eeAddressPage >> 8));       // MSB
    Wire.send((int)(eeAddressPage & 0xFF));  // LSB
    byte c;
    for ( c = 0; c < length; c++)
      Wire.send(data[c]);
    Wire.endTransmission();
  } //i2c_eeprom_write_page()


  byte i2c_eeprom_read_byte( int deviceAddress, unsigned int eeAddress )
  {
    byte rdata = 0xFF;
  
    //ask device by address for data
    Wire.beginTransmission(deviceAddress);
    Wire.send((int)(eeAddress >> 8));             // MSB
    Wire.send((int)(eeAddress & 0xFF));           // LSB
    Wire.endTransmission();
  
    Wire.requestFrom(deviceAddress,1);
    if (Wire.available())
      rdata = Wire.receive();
    return rdata;
  } //i2c_eeprom_read_byte()

// END