Pages: [1]   Go Down
Author Topic: Example code not working (wire.write and wire.read)  (Read 1413 times)
0 Members and 2 Guests are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

im using example code from the beginning Arduino programming by Brian Evans. Chapter 10 - RTC (DS1307)

when i try to complie the code it doesnt like Wire.write or Wire.read.

Code:
#include <Wire.h>

const int DS1307 = 0x68;

const char* days[] =
  {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
const char* months[] =
  {"January", "February", "March", "April", "May", "June", "July", "August",
   "September", "October", "November", "December"};

byte second = 0;
byte minute = 0;
byte hour = 0;
byte weekday = 0;
byte monthday = 0;
byte month = 0;
byte year = 0;

void setup() {
  Wire.begin();
  Serial.begin(9600);
 
  delay(2000);
  Serial.print("The current date and time is: ");
  printTime();
  Serial.println("To set the date and time please select Newline ending to continue.");
  Serial.println("Would you like to set the date and time now? Y/N");
  while (!Serial.available()) delay(10);
  if (Serial.read() == 'y' || Serial.read() == 'Y') {
    Serial.read();
    setTime();
    Serial.print("The current date and time is now: ");
    printTime();
  }
  Serial.println("Goodbye.");
}

void loop() {}

byte decToBcd(byte val) {
  return ((val/10*16) + (val%10));
}

byte bcdToDec(byte val) {
  return ((val/16*10) + (val%16));
}

void setTime() {
  Serial.print("Please enter the current year, 00-99.  -  ");
  year = readByte();
  Serial.println(year);
  Serial.print("Please enter the current month, 1-12.  -  ");
  month = readByte();
  Serial.println(months[month-1]);
  Serial.print("Please enter the current day of the month, 1-31.  -  ");
  monthday = readByte();
  Serial.println(monthday);
  Serial.println("Please enter the current day of the week, 1-7.");
  Serial.print("1 Sun | 2 Mon | 3 Tues | 4 Weds | 5 Thu | 6 Fri | 7 Sat  -  ");
  weekday = readByte();
  Serial.println(days[weekday-1]);
  Serial.print("Please enter the current hour in 24hr format, 0-23.  -  ");
  hour = readByte();
  Serial.println(hour);
  Serial.print("Please enter the current minute, 0-59.  -  ");
  minute = readByte();
  Serial.println(minute);
  second = 0;
  Serial.println("Thank you.");

  Wire.beginTransmission(DS1307);
  Wire.write(byte(0));

  Wire.write(decToBcd(second));
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));
  Wire.write(decToBcd(weekday));
  Wire.write(decToBcd(monthday));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));

  Wire.write(byte(0));
  Wire.endTransmission();
}

byte readByte() {
  while (!Serial.available()) delay(10);
  byte reading = 0;
  byte incomingByte = Serial.read();
  while (incomingByte != '\n') {
    if (incomingByte >= '0' && incomingByte <= '9') reading = reading * 10 + (incomingByte - '0');
    else;
    incomingByte = Serial.read();
  }
  Serial.flush();
  return reading;
}

void printTime() {
  char buffer[3];
  const char* AMPM = 0;
 
  readTime();
  Serial.print(days[weekday-1]);
  Serial.print(" ");
  Serial.print(months[month-1]);
  Serial.print(" ");
  Serial.print(monthday);
  Serial.print(", 20");
  Serial.print(year);
  Serial.print(" ");
 
  if (hour > 12) {
    hour -= 12;
    AMPM = " PM";
  } else AMPM = " AM";
 
  Serial.print(hour);
  Serial.print(":");
 
  sprintf(buffer, "%02d", minute);
  Serial.print(buffer);
  Serial.println(AMPM);
}

void readTime() {
  Wire.beginTransmission(DS1307);
  Wire.write(byte(0));
  Wire.endTransmission();

  Wire.requestFrom(DS1307, 7);

  second = bcdToDec(Wire.read());
  minute = bcdToDec(Wire.read());
  hour = bcdToDec(Wire.read());
  weekday = bcdToDec(Wire.read());
  monthday = bcdToDec(Wire.read());
  month = bcdToDec(Wire.read());
  year = bcdToDec(Wire.read());
}
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Could you be a bit more specific about "doesn't like"?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

it wont allow me to download the program to the arduino uno
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8687
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Perhaps that code was written for an earlier version of Arduino.  There were a number of changes in 1.0, probably including changes in the Wire library.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Does it tell you why?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

this is what it says at the bottom in arduino 1 (lastest version)

L10_4.cpp: In function 'void setTime()':
L10_4:71: error: 'class TwoWire' has no member named 'write'
L10_4:73: error: 'class TwoWire' has no member named 'write'
L10_4:74: error: 'class TwoWire' has no member named 'write'
L10_4:75: error: 'class TwoWire' has no member named 'write'
L10_4:76: error: 'class TwoWire' has no member named 'write'
L10_4:77: error: 'class TwoWire' has no member named 'write'
L10_4:78: error: 'class TwoWire' has no member named 'write'
L10_4:79: error: 'class TwoWire' has no member named 'write'
L10_4:81: error: 'class TwoWire' has no member named 'write'
L10_4.cpp: In function 'void readTime()':
L10_4:127: error: 'class TwoWire' has no member named 'write'
L10_4:132: error: 'class TwoWire' has no member named 'read'
L10_4:133: error: 'class TwoWire' has no member named 'read'
L10_4:134: error: 'class TwoWire' has no member named 'read'
L10_4:135: error: 'class TwoWire' has no member named 'read'
L10_4:136: error: 'class TwoWire' has no member named 'read'
L10_4:137: error: 'class TwoWire' has no member named 'read'
L10_4:138: error: 'class TwoWire' has no member named 'read'
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As Johnwasser said. Which version of the IDE are you using?

Edit: ah, missed that you had 1.0. Have you managed to get an old version of the library from somewhere?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the latest, Arduino 1.0
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8687
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Perhaps you have an old copy of the Wire library somewhere.  "As of Arduino 1.0, the library inherits from the Stream functions, making it consistent with other read/write libraries. Because of this, send() and receive() have been replaced with read() and write()."

Check the "libraries" folder in your sketch folder to make sure you don't have an old Wire library.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i will try downloading a new wire library
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You don't need to do that. The wire library is included with the IDE.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

if you change the write to send and the read to recieve it will work (complie) thank guys
Logged

Pages: [1]   Go Up
Jump to: