Go Down

Topic: Trying to understand given code errors. (Read 182 times) previous topic - next topic

Krysta

     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: [Select]

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]);
}
  }
}

UKHeliBob

Code: [Select]
  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: [Select]
   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: [Select]
  if (names[i] == note)Again, you are trying to use a variable (note) that has not been declared.
Code: [Select]
  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 ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

GoForSmoke


     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: [Select]

 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: [Select]

 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.

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

Krysta

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.

radman

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.

Go Up