Stumped by error messages - pulling from separate library

I am building my own variation of a project found here:

It includes this library for tones:
http://arduino-tone.googlecode.com/files/Tone-V0004.zip

I got rid of the switch for selecting time and alarm setting and replaced them with separate buttons.
I also removed the capacitive switch for the snooze button and replaced it with a physical button.

Due to the original author writing it in tabs, the code will be in several posts

Here is my modified code:

 /*
 ==========Arduino Binary Alarm Clock==========
 | Author: Martin Forsgren                    |
 | Date:   15-11-2009                         |
 ==============================================
 */
 
#include <CapSense.h> // library for touchsensors
#include <Tone.h>    // library for soundgeneration
#define DEBUG 0     // set to 1 to get debugging output via serial.

// Display pins:
int hour_pins[]   = {18, 17, 16, 15, 14};    // hour pins are pins 14 to 18
int minute_pins[] = {13, 12, 11, 10, 9, 8}; // minute pins are 8 to 13
// the values are in falling order because I connected the pins that way by accident

// Button pins:
int hour_button_pin = 6;
int minute_button_pin = 5;
int setTime_button_pin = 7;
int setAlarm_button_pin = 4;
int snooze_button = 3;



// Sound:
int speaker_pin = 19;
Tone tone_maker;

// Button states:
int hour_button_state;
int minute_button_state;
int setTime_button_state;
int setAlarm_button_state;
int snooze_button_state;

// Clock variables, that stores the current time:
int seconds = 0;
int minutes = 10;
int hours = 10;

// hours_p and minutes_p are pointers that are used to decide: 
// - if the ordinary time or the alarm time shoud be displayed
// - if the time or the alarm time should be changed when the buttons are pressed
int * hours_p = &hours;     // points to hours or alarm_hours depending 
                            // on the setting_switch_state
int * minutes_p = &minutes; // points to minutes or alarm_minutes depending 
                            // on the setting_switch_state

// ALARM VARIABLES:
boolean  alarm_on = false;
boolean signal_on = false;
boolean snooze_on = false;
// alarm time:
int alarm_hours = 20;
int alarm_minutes = 20;
// time when snooze period should end:
int snooze_off_hours;
int snooze_off_minutes;

int debug_print_counter = 0;

void setup()
{
  
  // set all six minute pins to output: 
  for(int i = 0; i < 7; i++)
  {
    pinMode(minute_pins[i], OUTPUT);
  } 
  
  // set all five hour pins to output:
  for(int i = 0; i < 6; i++) 
  {
    pinMode(hour_pins[i], OUTPUT);
  }

  // set the button pins to input:
  pinMode(hour_button_pin, INPUT);
  pinMode(minute_button_pin, INPUT);
  pinMode(setTime_button_pin, INPUT);
  pinMode(setAlarm_button_pin, INPUT);
  // activate the internal pullup resistors:
  digitalWrite(hour_button_pin, HIGH);
  digitalWrite(minute_button_pin, HIGH);
  digitalWrite(setTime_button_pin, HIGH);
  digitalWrite(setAlarm_button_pin, HIGH);
  digitalWrite(snooze_button, HIGH);
  
  //output sound to the speaker_pin:
  tone_maker.begin(speaker_pin);

}

void loop() 
{
  clock();   // keep track of time, i.e. update hours,
             // minutes and seconds variables as needed.
  display(); // display the time, or the alarm time, depending on the state of the settings switch.
  alarm();   // checks if it's time for the alarm  to start.
  update_buttons_state(); // checks if the buttons and touch sensor states has changed
  //update_buttons_state_with_debounce();
  buttons(); // does what the buttons should do
  
  
}

And here are my error codes:

C:\Users\tech\Documents\Arduino\libraries\Tone\Tone.cpp: In member function 'void Tone::begin(uint8_t)':
C:\Users\tech\Documents\Arduino\libraries\Tone\Tone.cpp:125: error: 'bitWrite' was not declared in this scope
C:\Users\tech\Documents\Arduino\libraries\Tone\Tone.cpp:127: error: 'digitalPinToPort' was not declared in this scope
C:\Users\tech\Documents\Arduino\libraries\Tone\Tone.cpp:127: error: 'portOutputRegister' was not declared in this scope
C:\Users\tech\Documents\Arduino\libraries\Tone\Tone.cpp:128: error: 'digitalPinToBitMask' was not declared in this scope
C:\Users\tech\Documents\Arduino\libraries\Tone\Tone.cpp: In member function 'void Tone::play(int, long unsigned int)':
C:\Users\tech\Documents\Arduino\libraries\Tone\Tone.cpp:201: error: 'OUTPUT' was not declared in this scope
C:\Users\tech\Documents\Arduino\libraries\Tone\Tone.cpp:201: error: 'pinMode' was not declared in this scope
C:\Users\tech\Documents\Arduino\libraries\Tone\Tone.cpp:284: error: 'bitWrite' was not declared in this scope
C:\Users\tech\Documents\Arduino\libraries\Tone\Tone.cpp: In member function 'void Tone::stop()':
C:\Users\tech\Documents\Arduino\libraries\Tone\Tone.cpp:359: error: 'digitalWrite' was not declared in this scope

Other than changing the pin assignments, I didn't make any changes to the code and I can't figure out what these error codes are referring to.

Here's the clock code:

//"INTERNAL" VARIABLES FOR CLOCK FUNCTION:
#define MAX_MILLIS_VALUE 34359738
unsigned long current_millis_value = 0;
unsigned long previous_millis_value = 0;
unsigned long m = 0;
/*
unsigned int seconds = 0;
unsigned int minutes = 0;
unsigned int hours = 0;
*/
void clock()
{
  current_millis_value = millis();
  if (current_millis_value < previous_millis_value) // if millis overflows
  {
    m += MAX_MILLIS_VALUE - previous_millis_value + current_millis_value;
  }
  else // if millis has not overflown
  {
    m += current_millis_value - previous_millis_value;
  }
  seconds += m / 1000;
  m = m % 1000;
  minutes += seconds / 60;
  seconds = seconds % 60;
  hours += minutes / 60;
  minutes = minutes % 60;
  hours = hours % 24;
  previous_millis_value = current_millis_value;
}

/* Clock function that uses if statements instead of modulo */
void ifClock()
{
  current_millis_value = millis();
  if (current_millis_value < previous_millis_value) // if millis overflows
  {
    m += MAX_MILLIS_VALUE - previous_millis_value + current_millis_value;
  }
  else // if millis has not overflown
  {
    m += current_millis_value - previous_millis_value;
  }
  if (m>999)
  {
    seconds++;
    m = m-1000;
  }
  if (seconds>59) // if seconds == 60
  {
    minutes++;
    seconds = 0;
  }
  if (minutes>59) // if minutes == 60
  {
    hours++;
    minutes = 0;
  }
  if (hours>23) // if hours == 24
  {
    hours = 0;
  }
  
  previous_millis_value = current_millis_value;
}

Here's the display code:

// "INTERNAL" VARIABLES FOR DISPLAY FUNCTION: 
int remainder;
int led_value;

void display()
{ 
  // display() will display the ordinary time or the alarm time,
  // depending on what hours_p points to, this is decided
  // by the settings_switch_state, in the buttons() function
  
  // Display hours:
  remainder = *hours_p;
  for(int i = 0; i < 5; i++) // repeat four all five hour-LEDs
  { 
    led_value = 16/round(pow(2,i)); // first LED = 16, second = 8, third = 4 etc.
    
    if(remainder/led_value == 1)
      digitalWrite(hour_pins[i], HIGH); 
    else
      digitalWrite(hour_pins[i], LOW);
    
    // the remainder of the hours is saved for 
    // the next LED that is displaying a lower value
    remainder=remainder%led_value;
  }
  
  // Display minutes:
  remainder = *minutes_p;
  for(int i = 0; i < 6; i++) // repeat for all six minute-LEDs
  { 
    led_value = 32/round(pow(2,i)); // first 32, then 16, then 8 etc.
    
    if(remainder/led_value == 1)
      digitalWrite(minute_pins[i], HIGH);
    else
      digitalWrite(minute_pins[i], LOW);
    
    // the remainder of the minutes is saved for 
    // the next LED that is displaying a lower value
    remainder=remainder%led_value;
  }
}

Here's the alarm code:

// "INTERNAL" VARIABLES FOR ALARM FUNCTION:
boolean first_time_signal_on = true; // used to make shure the signal is
                                    // only started once, so that you can
                                   // snooze without the alarm starting again
                                  // imidiately.

void alarm()
{
  if(alarm_on)
  {
    // Check if the time is same as alarm time:
    if(hours==alarm_hours && minutes==alarm_minutes && first_time_signal_on)
    {
      // if so, turn on the alarm signal:
      signal_on = true;
      first_time_signal_on = false;
    }
    
    if(signal_on)
    {
      play_melody();
    }
    
    // look in buttons() for snooze button and alarm off button
    
    if(snooze_on)
    {
      // Check if the time is same as snooze off time:
      if(hours==snooze_off_hours && minutes==snooze_off_minutes)
      {
        // if so, turn off snooze and restart alarm signal:
        snooze_on = false;
        signal_on = true;
      }
    }  
  }
  else
  {
    // reset so that the alarm will work next time:
    first_time_signal_on = true;
  }
}

Here's the button code:

void update_buttons_state()
{
  hour_button_state    = digitalRead(hour_button_pin);
  minute_button_state  = digitalRead(minute_button_pin); 
  setTime_button_state = digitalRead(setTime_button_pin);
  setAlarm_button_state = digitalRead(setAlarm_button_pin);
  
  
}


// "INTERNAL" VARIABLES FOR BUTTONS FUNCTION:
boolean first_time_hour = true;   // these are used to make sure that the hours
boolean first_time_minute = true; // and minutes only is increased once every keypress.
unsigned long snooze_button_timer; // used to keep track of how long the snooze button has
                                   // been held down. when the button has been held down
                                   // a certain amount of time, the alarm will be turned
                                   // of completely.
void buttons()
{
  // LOW == button pressed
  // HIGH == button released
  // (this is because pullup resistors is used)
  
  // Decide if we should set time or alarm:
  // (this also makes the display show the alarm time)
  if(setTime_button_state==LOW) // LOW = Set time
  {
    hours_p = &hours;
    minutes_p = &minutes;
  }
  if(setAlarm_button_state==LOW) // LOW = Set alarm
  {
    hours_p = &alarm_hours;
    minutes_p = &alarm_minutes;
  }

  // If hour button is pressed, increase hours:
  if(hour_button_state==LOW && first_time_hour) // only increase the hours once
  {                                             // every button press.
    if(*hours_p < 23)
      (*hours_p)++;
    else
      *hours_p = 0;
    
    first_time_hour = false;
    
   
  }
  else if(hour_button_state==HIGH)
  {
    first_time_hour = true; // reset when button is released, 
  }                         // so that the next press will be registerd.
  
  // If minute button is pressed, increase minutes:
  if(minute_button_state==LOW && first_time_minute) // only increase the minutes
  {                                                 // once every button press.
    if(*minutes_p < 59)
      (*minutes_p)++;
    else
      *minutes_p = 0;
      
    first_time_minute = false;
  }
  else if(minute_button_state==HIGH)
  {
    first_time_minute = true; // reset when button is released, 
  }                           // so that the next press will be registerd.

  
  if(snooze_button_state==LOW)
  {
    if(signal_on)
    {
      // set the time when the alarm signal will start again,
      // this will give 10 minutes snooze:
      if(minutes<50)
      {
        snooze_off_minutes = minutes+10; 
        snooze_off_hours = hours;
      }
      else
      {
        snooze_off_minutes = minutes - 50; 
        snooze_off_hours = hours + 1;
      }
      snooze_on = true;
      signal_on = false;
    }
    // if the snooze button has been held down for more than 3 seconds turn off/on the alarm
    if((millis() - snooze_button_timer) > 3000) 
    {
      if(alarm_on) // if on, turn off
      {
        signal_on = false; 
        alarm_on = false;
        snooze_on = false;
        // play tone so the user know the alarm turned off:
        tone_maker.play(NOTE_A3, 100);
      }
      else if(alarm_on==false) // if off, turn on
      {
        alarm_on = true;
        // play tone so the user know the alarm turned off:
        tone_maker.play(NOTE_A7, 100);
      }
      //reset the snooze button timer
      snooze_button_timer=millis();
    }
    
  }
  else
  {
    //reset the snooze button timer
    snooze_button_timer=millis();
  }
}

And here's the Melody code:

// "INTERNAL" VARIABLES FOR PLAY_MELODY FUNCTION:
int melody[] = { NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, 
                 NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5 };
int melody_length = sizeof(melody) / 2; // Melody length, for looping.
// sizeof() returns the size of the array in bytes, and because 
// an int is 2 bytes, sizeof will return 2*(number of array elements)

int i = 0;        //loop variable
boolean reset_loop = true; 
int jump = 1;     // how many notes to jump in the melody array
int position = 0; // position in melody array

void play_melody()
{
  if(!(tone_maker.isPlaying())) // if the last tone has stopped
  {
    if(i<melody_length)
    {
      //tone_maker.stop();
      tone_maker.play(melody[position], 300);
      // A pause between notes...
      //delay(300); // replace with nonblocking.
      
      if (DEBUG) 
      { // If debugging, report loop, tone, beat, and duration
        Serial.print(position);
        Serial.print(":");
        Serial.print(melody[position]);
        Serial.print("\n");
      }
      
      position += jump;
      //if position is bigger than the array, start from beginning of array:
      position = position%melody_length; 
      i++;
     }
     else
     {
       jump++;
       i = 0;
     }
  }
}

C:\Users\tech\Documents\Arduino\libraries\Tone\Tone.cpp: In member function 'void Tone::begin(uint8_t)':
C:\Users\tech\Documents\Arduino\libraries\Tone\Tone.cpp:125: error: 'bitWrite' was not declared in this scope

This looks as if the tone library you're using hasn't included Arduino.h. That might be because it predates the Arduino 1.0 changes and hasn't been updated. I haven't looked into what you're doing with that library, but the file location above suggests that it's something you have installed. Is it doing something you can't already do using the standard Arduino tone() function?

PeterH:

C:\Users\tech\Documents\Arduino\libraries\Tone\Tone.cpp: In member function 'void Tone::begin(uint8_t)':
C:\Users\tech\Documents\Arduino\libraries\Tone\Tone.cpp:125: error: 'bitWrite' was not declared in this scope

This looks as if the tone library you're using hasn't included Arduino.h. That might be because it predates the Arduino 1.0 changes and hasn't been updated. I haven't looked into what you're doing with that library, but the file location above suggests that it's something you have installed. Is it doing something you can't already do using the standard Arduino tone() function?

I've never done tones with the arduino, i've always done lights. I chose this project because it was all included and required minimal reprogramming for my needs. Here is the documentation page for the tone library. Google Code Archive - Long-term storage for Google Code Project Hosting. This was supposed to be a birthday present for my step-son yesterday. I have all the hardware ready to finalize, but I can't even test it because the code won't load.

Alright, so I understand that it probably has to do with the fact that the code was written in 2009 and there is probably a compatibility issue, but I need to know how to fix it. HELP PLEASE!

Look at the tone() and notone() API provided by the Arduino runtime and check whether you actually need a separate Tone library. Perhaps you can achieve your goal without it.

If you do need to use your Tone library then look at http://arduino.cc/en/Main/ReleaseNotes and read the release notes for version 1.0, especially the change from WProgram.h to Arduino.h. Look at the implementation of your Tone library to see whether it was coded prior to V1.0. If so, the release notes describe the changes that would need to be made to be compatible with the current IDE version.

I looked at the release notes as you suggested and added :

 #if defined(TONE) && TONE >= 100
  #include "Tone.h"
  #else
  #include "WTone.h"
  #endif

But to no avail, I still get this error:

Arduino: 1.5.6-r2 (Windows 7), Board: "Arduino Pro or Pro Mini, ATmega328 (5V, 16 MHz)"

Build options changed, rebuilding all

Using library Tone in folder: C:\Users\Chris\Documents\Arduino\libraries\Tone (legacy)



C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\avr-g++ -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -MMD -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=156 -DARDUINO_AVR_PRO -DARDUINO_ARCH_AVR -IC:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino -IC:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\standard -IC:\Users\Chris\Documents\Arduino\libraries\Tone C:\Users\Chris\AppData\Local\Temp\build2205479925934688467.tmp\Binary_clock_v1_3.cpp -o C:\Users\Chris\AppData\Local\Temp\build2205479925934688467.tmp\Binary_clock_v1_3.cpp.o 

Binary_clock_v1_3.ino:14:21: error: WTone.h: No such file or directory
Binary_clock_v1_3.ino:34: error: 'Tone' does not name a type
Binary_clock_v1_3.ino: In function 'void setup()':
Binary_clock_v1_3.ino:97: error: 'tone_maker' was not declared in this scope
d_button_code.ino: In function 'void buttons()':
d_button_code.ino:99: error: 'tone_maker' was not declared in this scope
d_button_code.ino:99: error: 'NOTE_A3' was not declared in this scope
d_button_code.ino:105: error: 'tone_maker' was not declared in this scope
d_button_code.ino:105: error: 'NOTE_A7' was not declared in this scope
e_melody_code.ino: At global scope:
e_melody_code.ino:2: error: 'NOTE_C4' was not declared in this scope
e_melody_code.ino:2: error: 'NOTE_D4' was not declared in this scope
e_melody_code.ino:2: error: 'NOTE_E4' was not declared in this scope
e_melody_code.ino:2: error: 'NOTE_F4' was not declared in this scope
e_melody_code.ino:3: error: 'NOTE_G4' was not declared in this scope
e_melody_code.ino:3: error: 'NOTE_A4' was not declared in this scope
e_melody_code.ino:3: error: 'NOTE_B4' was not declared in this scope
e_melody_code.ino:3: error: 'NOTE_C5' was not declared in this scope
e_melody_code.ino: In function 'void play_melody()':
e_melody_code.ino:15: error: 'tone_maker' was not declared in this scope

The 1.5+ versions are for Due and other ARM-based Arduino. You do not have an ARM_based Arduino, so why are you using 1.5.6?

Because it was the newest version and until this project it worked just fine.

HolidayV:
I looked at the release notes as you suggested and added :

#if defined(TONE) && TONE >= 100

#include "Tone.h"
  #else
  #include "WTone.h"
  #endif

That is nothing like what is recommended in the release notes - you're over-thinking this. Just make the change as it is described in the release notes.

PeterH:

HolidayV:
I looked at the release notes as you suggested and added :

#if defined(TONE) && TONE >= 100

#include "Tone.h"
  #else
  #include "WTone.h"
  #endif

That is nothing like what is recommended in the release notes - you're over-thinking this. Just make the change as it is described in the release notes.

[internals]

  • The WProgram.h file, which provides declarations for the Arduino API,
    has been renamed to Arduino.h. To create a library that will work in
    both Arduino 0022 and Arduino 1.0, you can use an #ifdef that checks
    for the ARDUINO constant, which was 22 and is now 100. For example:

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

That is EXACTLY as it is in the release notes with exception of me changing the word "arduino" to "tone" becuase the name of the library is Tone.h

I changed versions to 1.0.5 and reloaded without the previous changes and now I get the following error codes:

C:\Users\Chris\Documents\Arduino\libraries\Tone\Tone.cpp:29:20: warning: wiring.h: No such file or directory
C:\Users\Chris\Documents\Arduino\libraries\Tone\Tone.cpp:94: warning: only initialized variables can be placed into program memory area
C:\Users\Chris\Documents\Arduino\libraries\Tone\Tone.cpp: In member function 'void Tone::begin(uint8_t)':
C:\Users\Chris\Documents\Arduino\libraries\Tone\Tone.cpp:125: error: 'bitWrite' was not declared in this scope
C:\Users\Chris\Documents\Arduino\libraries\Tone\Tone.cpp:127: error: 'digitalPinToPort' was not declared in this scope
C:\Users\Chris\Documents\Arduino\libraries\Tone\Tone.cpp:127: error: 'portOutputRegister' was not declared in this scope
C:\Users\Chris\Documents\Arduino\libraries\Tone\Tone.cpp:128: error: 'digitalPinToBitMask' was not declared in this scope
C:\Users\Chris\Documents\Arduino\libraries\Tone\Tone.cpp: In member function 'void Tone::play(int, long unsigned int)':
C:\Users\Chris\Documents\Arduino\libraries\Tone\Tone.cpp:201: error: 'OUTPUT' was not declared in this scope
C:\Users\Chris\Documents\Arduino\libraries\Tone\Tone.cpp:201: error: 'pinMode' was not declared in this scope
C:\Users\Chris\Documents\Arduino\libraries\Tone\Tone.cpp:284: error: 'bitWrite' was not declared in this scope
C:\Users\Chris\Documents\Arduino\libraries\Tone\Tone.cpp: In member function 'void Tone::stop()':
C:\Users\Chris\Documents\Arduino\libraries\Tone\Tone.cpp:359: error: 'digitalWrite' was not declared in this scope

HolidayV:
That is EXACTLY as it is in the release notes with exception of me changing the word "arduino" to "tone" becuase the name of the library is Tone.h

Hi Chris,

PeterH is right, you are trying to be too clever. Make those changes to the tone library exactly as described, don't change anything. Those lines of code are not referring to the tone library, they are referring to another library which used to be called "WProgram" but these days is called "Arduino". Its the library that contains all the basic standard Arduino commands like digitalWrite. You won't have been aware that library existed until now because you never need to include that library in your own sketches, it is automatically included for you.

Paul

HolidayV:
That is EXACTLY as it is in the release notes with exception of me changing ...

That is not a template for you to put your own identifiers in, it's a code snippet for you to use exactly as it's given.

OK, I'm starting to understand, but I need to know where I'm supposed to put that code snippet. Obviously it doesn't go into my sketch, and from what I understand, I'm supposed to make a library containing this snippet of code? If so, I have no idea how to do that.

You need to look in the tone.h and tone.c that you have.

At the top of one or both of these files, you might see "#include WProgram.h"

Edit that line to read #include Arduino.h

Problem solved.