Help with My First Big Code

I'm new to coding and Arduino and i got this code from a friend and when i went to verify it i got errors. What is wrong with this code?

#include <Tone.h>

Tone freq1;
Tone freq2;

const int ledPin = 6;
const int transistorGnd = 7;
const int repulsorSpeaker = 11;
const int speakerGnd = 10;
boolean readyToFire = true;
boolean beginCount = true;
boolean notDeactivated = true;
int repulsorStable = 10;
const int tilt_s1 = A2;
const int tilt_s2 = A1;

long previousMillis = 0;        // will store last time LED was updated
long interval = 12000; 

int thisPitch1;
int thisPitch2;

void setup() {
  // put your setup code here, to run once:
  
  Serial.begin(9600);
  
  freq1.begin(repulsorSpeaker);
  freq2.begin(repulsorSpeaker);
  pinMode(ledPin, OUTPUT);
  pinMode(speakerGnd, OUTPUT);
  digitalWrite(speakerGnd, LOW);
  pinMode(transistorGnd, OUTPUT);
  digitalWrite(transistorGnd, LOW);
  pinMode(A3, OUTPUT);
  digitalWrite(A3, LOW);
  pinMode(tilt_s1, INPUT);
  pinMode(tilt_s2, INPUT);
  
  analogWrite(ledPin, repulsorStable);
  
}

void loop() {
  // put your main code here, to run repeatedly: 
  
  int tilt = getTiltPosition();  // read the position of the tilt sensor
  Serial.print("Ready: ");
  Serial.print(readyToFire);  // status of whether or not the hand has been reset to prone position
  Serial.print(", Pos: ");
  Serial.println(tilt);  // position of tilt sensor
  delay(10);  // slight delay to get better readings of sensor
 
  
  if (tilt == 1) {  // if hand is in prone position
    
    if (readyToFire == false) {  // if blast has been fired 
      beginCount = true;  // begin counting time since hand has been in prone position
    }
    
    readyToFire = true;  // ability fire has been reset
    
    if (beginCount == true) {
      unsigned long previousMillis = millis();  // reset counter
      beginCount = false;  // counter will not be reset until it has reached
    }
    
    unsigned long currentMillis = millis();
    
    if(currentMillis - previousMillis > interval) {
      // save the last time you blinked the LED 
      previousMillis = currentMillis;
      
      if (notDeactivated == true) {
        deactivate();
        notDeactivated = false;
      }
      
      do { 
      
        analogWrite(ledPin, 0);
        tilt = getTiltPosition();
        Serial.print("Pos: ");
        Serial.println(tilt);
        delay(10);
      
      } while (tilt == 1);
      
    }
    
  } else if (tilt == 2 && readyToFire == true) {
    
    readyToFire = false;
    repulsorFire();
    notDeactivated = true;
    
  }
  

}

int getTiltPosition(){
  int s1 = digitalRead(tilt_s1);
  int s2 = digitalRead(tilt_s2);
  return (s1 << 1) | s2; //bitwise math to combine the values
}

void repulsorFire() {
  
  for (int i = 2; i < 252; i+=2) {  // from 0 to 254
                
    analogWrite(ledPin, i);
    
    if(i > 125) { 
      delay(5);
    }

    delay(5);  // cause slight linear delay between fade steps
    thisPitch1 = 0;
    thisPitch2 = map(i, 2, 252, 1200, 1700);
    playDTMF(thisPitch1, thisPitch2, 30);

  }  // end for loop  
  
  for (int j = 50; j > 0; j--) {
      
    for (int k = 0; k < 20; k+=2) {
      
      analogWrite(ledPin, 12*k);  
      thisPitch1 = map(k, 20, 0, (k*14), (j*10));
      thisPitch2 = map(k, 20, 0, (k*36), (j*30));
      playDTMF(thisPitch1, thisPitch2, 30);
      delay(1);
    
    } // end for loop
  analogWrite(ledPin, 12*j);
  delay(1);
    
  } // end for loop
  
  analogWrite(ledPin, repulsorStable);

}  // end repulsorFire function

void deactivate () {
  
  for (int i = 200; i > 0; i-=2) {  // from 200 to 0
                
    analogWrite(ledPin, i/20);
    
    if(i < 100) { 
      delay(10);
    }

    delay(5);  // cause slight linear delay between fade steps
    thisPitch1 = 0;
    thisPitch2 = map(i, 200, 0, 1500, 1200);
    playDTMF(thisPitch1, thisPitch2, 30);

  }  // end for loop
  
}

void playDTMF(int number1, int number2, long duration)
{
  freq1.play(number1, duration);
  freq2.play(number2, duration);
}

Show the error.

I didn't get an error, so you need to supply more details. IDE version. Which Arduino board? Which operating system are you using? What the error is.

How to use this forum

Im using an Arduino UNO on windows..is there a way to copy the errors off the bottom of the sketch window or do i have to type them all out here because i have a lot of errors?

Hello,

Use CTRL+C, CTRL+V :slight_smile:

These are the errors below.

C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp: In member
function 'void Tone::begin(unit8_t)':
C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp:121: error:
'bitWrite' was not declared in this scope
C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp:123: error:
'digitalPinToPort' was not declared in this scope
C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp:123: error:
'portOutputRegister' was not declared in this scope
C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp:124: error:
'digitalPinToBitMask' was not declared in this scope
C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp: In member
function 'void Tone::play(unit16_t, unit32_t)':
C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp:198: error:
'OUTPUT' was not declared in this scope
C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp:198: error:
'pinMode' was not declared in this scope
C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp:294: error:
'bitWrite' was not declared in this scope
C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp: In member
function 'void Tone::stop()':
C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp:361: error:
'digitalWrite' was not declared in this scope

Does your copy of Tone.h or Tone.cpp include WProgram.h? If so, change that to Arduino.h.

WProgram.h was not in there.

Am i missing other libraries that are needed?

What do need to do to fix this.

#include <Tone.h>

Tone freq1;
Tone freq2;

const int ledPin = 6;
const int transistorGnd = 7;
const int repulsorSpeaker = 11;
const int speakerGnd = 10;
boolean readyToFire = true;
boolean beginCount = true;
boolean notDeactivated = true;
int repulsorStable = 10;
const int tilt_s1 = A2;
const int tilt_s2 = A1;

long previousMillis = 0;        // will store last time LED was updated
long interval = 12000; 

int thisPitch1;
int thisPitch2;

void setup() {
  // put your setup code here, to run once:
  
  Serial.begin(9600);
  
  freq1.begin(repulsorSpeaker);
  freq2.begin(repulsorSpeaker);
  pinMode(ledPin, OUTPUT);
  pinMode(speakerGnd, OUTPUT);
  digitalWrite(speakerGnd, LOW);
  pinMode(transistorGnd, OUTPUT);
  digitalWrite(transistorGnd, LOW);
  pinMode(A3, OUTPUT);
  digitalWrite(A3, LOW);
  pinMode(tilt_s1, INPUT);
  pinMode(tilt_s2, INPUT);
  
  analogWrite(ledPin, repulsorStable);
  
}

void loop() {
  // put your main code here, to run repeatedly: 
  
  int tilt = getTiltPosition();  // read the position of the tilt sensor
  Serial.print("Ready: ");
  Serial.print(readyToFire);  // status of whether or not the hand has been reset to prone position
  Serial.print(", Pos: ");
  Serial.println(tilt);  // position of tilt sensor
  delay(10);  // slight delay to get better readings of sensor
 
  
  if (tilt == 1) {  // if hand is in prone position
    
    if (readyToFire == false) {  // if blast has been fired 
      beginCount = true;  // begin counting time since hand has been in prone position
    }
    
    readyToFire = true;  // ability fire has been reset
    
    if (beginCount == true) {
      unsigned long previousMillis = millis();  // reset counter
      beginCount = false;  // counter will not be reset until it has reached
    }
    
    unsigned long currentMillis = millis();
    
    if(currentMillis - previousMillis > interval) {
      // save the last time you blinked the LED 
      previousMillis = currentMillis;
      
      if (notDeactivated == true) {
        deactivate();
        notDeactivated = false;
      }
      
      do { 
      
        analogWrite(ledPin, 0);
        tilt = getTiltPosition();
        Serial.print("Pos: ");
        Serial.println(tilt);
        delay(10);
      
      } while (tilt == 1);
      
    }
    
  } else if (tilt == 2 && readyToFire == true) {
    
    readyToFire = false;
    repulsorFire();
    notDeactivated = true;
    
  }
  

}

int getTiltPosition(){
  int s1 = digitalRead(tilt_s1);
  int s2 = digitalRead(tilt_s2);
  return (s1 << 1) | s2; //bitwise math to combine the values
}

void repulsorFire() {
  
  for (int i = 2; i < 252; i+=2) {  // from 0 to 254
                
    analogWrite(ledPin, i);
    
    if(i > 125) { 
      delay(5);
    }

    delay(5);  // cause slight linear delay between fade steps
    thisPitch1 = 0;
    thisPitch2 = map(i, 2, 252, 1200, 1700);
    playDTMF(thisPitch1, thisPitch2, 30);

  }  // end for loop  
  
  for (int j = 50; j > 0; j--) {
      
    for (int k = 0; k < 20; k+=2) {
      
      analogWrite(ledPin, 12*k);  
      thisPitch1 = map(k, 20, 0, (k*14), (j*10));
      thisPitch2 = map(k, 20, 0, (k*36), (j*30));
      playDTMF(thisPitch1, thisPitch2, 30);
      delay(1);
    
    } // end for loop
  analogWrite(ledPin, 12*j);
  delay(1);
    
  } // end for loop
  
  analogWrite(ledPin, repulsorStable);

}  // end repulsorFire function

void deactivate () {
  
  for (int i = 200; i > 0; i-=2) {  // from 200 to 0
                
    analogWrite(ledPin, i/20);
    
    if(i < 100) { 
      delay(10);
    }

    delay(5);  // cause slight linear delay between fade steps
    thisPitch1 = 0;
    thisPitch2 = map(i, 200, 0, 1500, 1200);
    playDTMF(thisPitch1, thisPitch2, 30);

  }  // end for loop
  
}

void playDTMF(int number1, int number2, long duration)
{
  freq1.play(number1, duration);
  freq2.play(number2, duration);
}

Errors below.

C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp: In member
function 'void Tone::begin(unit8_t)':
C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp:121: error:
'bitWrite' was not declared in this scope
C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp:123: error:
'digitalPinToPort' was not declared in this scope
C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp:123: error:
'portOutputRegister' was not declared in this scope
C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp:124: error:
'digitalPinToBitMask' was not declared in this scope
C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp: In member
function 'void Tone::play(unit16_t, unit32_t)':
C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp:198: error:
'OUTPUT' was not declared in this scope
C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp:198: error:
'pinMode' was not declared in this scope
C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp:294: error:
'bitWrite' was not declared in this scope
C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp: In member
function 'void Tone::stop()':
C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp:361: error:
'digitalWrite' was not declared in this scope

Im using an Arduino UNO on windows.

Turn on "verbose compiling", try again, and copy and paste what you get.

Jabblaze:
What do need to do to fix this.

You need to not cross post!


Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.

Threads merged.

  • Moderator

How to use this forum

Errors below.

C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\hardware\tools\avr\bin\avr-g++ -c -g -0s
-Wall -fno-exceptions -ffunction-sections -fdata-sections
-mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null
-DUSB_PID=null -DARDUINO=103 -IC:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\hardware\arduino\cores\arduino
-IC:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\hardware\arduino\variants\standard
-IC:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone
C:\DOCUME~1\Allan\LOCALS~1\Temp\build2189210571437226469.tmp\Repuls
or_Original.cpp -o
Repulsor_Original.ino: In function 'void loop()':
Repulsor_Original.ino:64: warning: unused variable 'previousMillis'
Repulsor_Original.ino:70: warning: comparison between signed and
unsigned integer expressions
C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\hardware\tools\avr\bin\avr-g++ -c -g -0s
-Wall -fno-exceptions -ffunction-sections -fdata-sections
-mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null
-DUSB_PID=null -DARDUINO=103 -IC:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\hardware\arduino\cores\arduino
-IC:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\hardware\arduino\variants\standard
-IC:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone -IC:\Documents and
Settings\Allan\My Documents\arduino-1.0.3\libraries\Tone\utility
C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp -o
C:\DOCUME~1\Allan\LOCALS~1\Temp\build2189210571437226469.tmp\Tone\T
one.cpp.o
C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp:26:20: warning:
wiring.h: No such file or directory
C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp:91: warning: only
initialized variables can be placed into program memory area
C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp: In member
function 'void Tone::begin(unit8_t)':
C:\Documents\arduino-1.0.3\libraries\Tone\Tone.cpp:121: error:
'bitWrite' was not declared in this scope
C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp:123: error:
'digitalPinToPort' was not declared in this scope
C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp:123: error:
'portOutputRegister' was not declared in this scope
C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp:124: error:
'digitalPinToBitMask' was not declared in this scope
C:\Documents and Settings\Allan\My
Documents/arduino-1.0.3\libraries\Tone\Tone.cpp: In member
function 'void Tone::play(unit16_t, unit32_t)':
C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp:198: error:
'OUTPUT' was not declared in this scope
C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp:198: error:
'pinMode' was not declared in this scope
C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp:294: error:
'bitWrite' was not declared in this scope
C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp: In member
function 'void Tone::stop()':
C:\Documents and Settings\Allan\My
Documents\arduino-1.0.3\libraries\Tone\Tone.cpp:361: error:
'digitalWrite' was not declared in this scope

I can't reproduce your problem, either. I'd suggest that you uninstall Arduino 1.0.3, and reinstall it (or 1.0.4, instead).

Can somebody try this code in their arduino software to see if it will verify right?

Can somebody try this code in their arduino software to see if it will verify right?

You mean besides Nick and myself?