Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
258 views
in Technique[技术] by (71.8m points)

How do I use functions in Arduino C++?

I'm an Arduino beginner and I want to write a little program that plays notes when I press a button (also prompts to press button on lcd).

To make the notes play, I want to write a little function that plays the note on the piezo and has a delay, but when I try to upload the code, this error appears:

enter image description here

Can somebody help me? Here's my code:

#include <LiquidCrystal.h>

int VO = 2;
int RS = 3;
int E = 4;
int D4 = 8;
int D5 = 9;
int D6 = 10;
int D7 = 11;
int PIEZO = 12;
int BUTTON = A0;
LiquidCrystal lcd(RS, E, D4, D5, D6, D7);

void setup() {
  pinMode(BUTTON, INPUT_PULLUP);
  analogWrite(VO, 0);
  lcd.begin(16, 2);
  lcd.print("Press the button");
}

void playTone(a, b) {
  c = b * 800;
  tone(PIEZO, a, c);
  delay(c);  
}

void loop() {
  if (analogRead(BUTTON) == 1023) {
    playTone(255, 1);
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to define what the variables are in the function.

void playTone(int a, int b)

In addition variable c needs to de defined somewhere before its used.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...