Pages: [1]   Go Down
Author Topic: Trying to understand given code errors.  (Read 158 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

     These errors keep occurring even when I try to change the code. (Actually sometimes more appear when I mess with it)
 error: too few arguments to function 'void analogWrite(uint8_t, int)'
sketch_oct28a:28: error: at this point in file
sketch_oct28a:42: error: 'frequency' was not declared in this scope
sketch_oct28a:83: error: 'note' was not declared in this scope
sketch_oct28a:85: error: return-statement with a value, in function returning 'void'
         
     My code is supposed to make a RGB LED, piezo, and motor interact with a potentiometer and button. Everything is supposed to be off until the button is pressed and held. Then, when button pressed, the RGB flashes colors (the delay is the potentiometer),  the piezo does a scale, and the motor spins (how fast is based on the potentiometer too). Right now it won't even finish uploading to the arduino board. I took out all the RGB colors to make the code shorter for this.

Code:
const int RED_PIN = 11;
const int GREEN_PIN = 12;
const int BLUE_PIN =13;
int buzzerPin = 9;
int sensorPin = 0;
int motorPin = 10;
int buttonPin = 2;
const int songLength = 13;
char notes[] = "cdefgabagfedc";
int beats[] = {2,2,2,2,2,2,2,2,2,2,2,2,2};
int tempo = 150;
void setup()
{
  pinMode(RED_PIN,OUTPUT);
  pinMode(GREEN_PIN,OUTPUT);
  pinMode(BLUE_PIN,OUTPUT);
  pinMode(buzzerPin,OUTPUT);
  pinMode(motorPin,OUTPUT);
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
}
void loop()
{
  int buttonState;
  int i, duration;
  int sensorValue;
  buttonState = digitalRead(buttonPin);
  sensorValue = analogWrite(sensorPin);
 Serial.print(sensorValue);
 Serial.print(duration);
 Serial.println();
 if(sensorPin<50)
 {
   motorPin = 50;
 }
 for(i = 0; i < songLength; i++)
 {
   duration = beats[i]*tempo;
   tone(buzzerPin, frequency(notes[i], duration);
   delay(sensorValue);
 }
 int frequency(char note);
   const int numNotes = 7;
   char names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b'};
  int frequencies[] = {262, 294, 330, 349, 392, 440, 494};
  for (i = 0; i , numNotes; i++) {
  if (names[i] == note)
{
  return(frequencies[i]);
}
  }
}
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4258
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  sensorValue = analogWrite(sensorPin);
analogWrite needs 2 arguments, a pin number and a value to write.  It also does not return a value so it cannot be used to set a variable as you have tried to do.
Code:
   tone(buzzerPin, frequency(notes[i], duration);
You are trying to use a variable  (frequency) that has not been declared in any scope, let alone in the loop() function.  In any case you are trying to use it like a function and even then have a bracket missing.
Code:
  if (names[i] == note)
Again, you are trying to use a variable (note) that has not been declared.
Code:
  return(frequencies[i]);
Here you try to return a value from a function (loop()) that is void, ie it does not return a value.  In any case, where in your program would it return it to ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4807
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

     These errors keep occurring even when I try to change the code. (Actually sometimes more appear when I mess with it)
 error: too few arguments to function 'void analogWrite(uint8_t, int)'

It is telling you that analogWrite needs 2 numbers. The 1st must be a byte and the second must be an int.
YOUR CODE has only one value and TBH looks like you used analogWrite where it should be analogRead.
Code:
 sensorValue = analogWrite(sensorPin);

Quote
sketch_oct28a:28: error: at this point in file

I'm not sure why this is there, but there's a good reason.

Quote
sketch_oct28a:42: error: 'frequency' was not declared in this scope

Here it's trying to figure out WTH to do with 'frequency'. Given that you train-wrecked the code for your function named frequency right into the code for loop(), that does make a certain amount of sense?
 
Quote
sketch_oct28a:83: error: 'note' was not declared in this scope

In your code you declare an array named notes[] but farther down you refer to note,

Code:
 if (names[i] == note)

You never declared note so the compiler is TELLING YOU THAT.

Quote
sketch_oct28a:85: error: return-statement with a value, in function returning 'void'

And that is due to the train-wreck code where you have the declaration of the function frequency() jammed into the unfinished code for loop().

All in all those error messages told me WHAT to look for in your code and sure enough, there are errors at those places.
       
Can you work this out or is the code something you copied somewhere and only have some clues about?

-IF- the latter then I can recommend you put this project on hold until you work through enough example sketches to comfortably know better what you are doing.
 
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

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

Thank you for the help. I created the code from scratch. This is my first term taking a robotics class and this is my mid-term project. I think I should be able to fix it now from your answers.
Logged

Offline Offline
God Member
*****
Karma: 7
Posts: 647
"In this house, we obey the Laws of Thermodynamics" Homer J. Simpson
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I created the code from scratch. This is my first term taking a robotics class

Compilers tell you where and what the errors are though it does take a while to learn how to understand the messages.

What you have done is write quite a bit of code then thow it into the compiler. Not surprisingly it had quite a few errors. Once you correct those and get the code to compile you are going to hope that it works, but it probably will not.

A better way to work is to start very simple. Write something that compiles and works correctly - just the famous "hello world" program for example. Then gradually add functionality to your program compiling and running as you go. If your program suddenly had problems compiling or stops working correctly you will know exactly what you did that caused the problem.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4807
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you for the help. I created the code from scratch. This is my first term taking a robotics class and this is my mid-term project. I think I should be able to fix it now from your answers.

Good job then! Most times it's "I grabbed this code, fix it please!".

The Arduino IDE has an Auto-Format that sets the indents right but your sketch has to compile for it to work. My usual method is to AF with every chunk of code I add right from the start. Having the braces on their own lines at matching level makes level-checking far easier.

if ( blahblah )
{
  do the thing
}

is easier to check than

if ( blahblah ) {
  do the thing
}

when there's many of them differently nested.

Same goes for leaving spaces around parenthesis to make them easily clear. At a glance (1) takes a fraction longer than ( 1 ) to scan and uses a grain less mental energy. When you spend hours and hours coding, it adds up.

These seem like little things but the difference made is in you (tiredness, longer time debugging) more than the code.

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Pages: [1]   Go Up
Jump to: