質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Arduino

Arduinoは、AVRマイコン、単純なI/O(入出力)ポートを備えた基板、C言語を元としたArduinoのプログラム言語と、それを実装した統合開発環境から構成されたシステムです。

Q&A

解決済

1回答

3869閲覧

ArduinoでサーボとLEDと効果音(raw)を動作したいが、”play”で引っかかる。。

takupi-2

総合スコア7

Arduino

Arduinoは、AVRマイコン、単純なI/O(入出力)ポートを備えた基板、C言語を元としたArduinoのプログラム言語と、それを実装した統合開発環境から構成されたシステムです。

0グッド

0クリップ

投稿2017/11/15 02:58

編集2017/11/15 05:06

###前提・実現したいこと
Arduino nanoでサーボとLEDと効果音(raw)を動作させるコードを作っています。
コンパイルすると下記のエラーメッセージがでます。
exit status 1
'play' was not declared in this scope

最初に作ったコードはエラーはでませんでした。

###発生している問題・エラーメッセージ

exit status 1 'play' was not declared in this scope

###該当のソースコード
//動作確認済み。コンパイルもOK
#include <Servo.h>
Servo myservo;

int pos = 100;

int speakerPin = 3;

int ledpin5 = 5;

const uint8_t sample05[] PROGMEM = {
0x7b,0x71,0x78,0xff,0x9e,0x00,0xe0,0x72,0x45,0x7f,0x88,0xae,0xa1,0xb3,0xa6,0xaf,
0x99,0x98,0x9b,0x95,0xa0,0xb5,0xd0,0xa3,0x00,0x00,0x00,0x98,0xcc,0xff,0xff,0xff,
0xd0,0xff,0xff,0xb3,0xae,0xdd,0xff,0xa6,0x17,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
・・・・・・

0x80,0x80,0x80,0x80,0x80,0x7f,0x80,0x7f,0x80
};

unsigned int sample05_len = 9000;

void setup() {

myservo.attach(9); // サーボのピンを9番に接続

pinMode(ledpin5, OUTPUT);

{
pinMode(3,OUTPUT);
analogWrite( speakerPin, 255);

TCCR2A = _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
TCCR2B = _BV(CS20);

play();}
}

void play() {
for (int i = 0; i < sample05_len; i++) {
OCR2B = pgm_read_byte_near(&sample05[i]);
delayMicroseconds(160);
}
}

void loop() {

digitalWrite(ledpin5, HIGH);

delay(1000);

for (pos = 99; pos <= 150; pos += 1) { // goes from 0 degrees to 180 degrees

// in steps of 1 degree

myservo.write(pos); // tell servo to go to position in variable ‘pos’

delay(15); // waits 15ms for the servo to reach the position

}

for (pos = 150; pos >= 30; pos -= 1) { // goes from 180 degrees to 0 degrees

myservo.write(pos); // tell servo to go to position in variable ‘pos’

delay(15); // waits 15ms for the servo to reach the position

}

for (pos = 30; pos <= 99; pos += 1) { // goes from 0 degrees to 180 degrees

// in steps of 1 degree

myservo.write(pos); // tell servo to go to position in variable ‘pos’

delay(15); // waits 15ms for the servo to reach the position

}
}

//エラーメッセージがでるコード #include <Servo.h> int mid_Pos=90; int max_Pos=128; // Bigger value would make it go to further end int min_Pos=50; // smaller value would make it go to another end further. int LED_Power=0; int LED_NormalPower=20; int LED_MaxPower=245; int random_num; int speakerPin = 5; Servo myservo; // create servo object to control a servo // twelve servo objects can be created on most boards int pos = mid_Pos; // variable to store the servo position int ledpin = 3; //Pin for LED, with PWM control const uint8_t sample05[] PROGMEM = { 0x7b,0x71,0x78,0xff,0x9e,0x00,0xe0,0x72,0x45,0x7f,0x88,0xae,0xa1,0xb3,0xa6,0xaf, 0x99,0x98,0x9b,0x95,0xa0,0xb5,0xd0,0xa3,0x00,0x00,0x00,0x98,0xcc,0xff,0xff,0xff, 0xd0,0xff,0xff,0xb3,0xae,0xdd,0xff,0xa6,0x17,0x00,0x00,0x00,0x00,0x00,0x00,0x00, ・・・・・・ 0x80,0x80,0x80,0x80,0x80,0x7f,0x80,0x7f,0x80 }; unsigned int sample05_len = 9000; void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object pinMode(ledpin, OUTPUT); randomSeed(analogRead(0)); pinMode(3,OUTPUT); analogWrite( speakerPin, 255); TCCR2A = _BV(COM2B1) | _BV(WGM21) | _BV(WGM20); TCCR2B = _BV(CS20); play();} } void play() { for (int i = 0; i < sample05_len; i++) { OCR2B = pgm_read_byte_near(&sample05[i]); delayMicroseconds(160); } } void loop() { int eyeMoveTime=0; int maxEyeMoveTime=25; startUp(); normalMove(); delay(500); random_num=0; for (eyeMoveTime=0; eyeMoveTime<maxEyeMoveTime;eyeMoveTime++){ if (random_num>50){ //Panic Mode code panicMove(); } else { // Normal Mode //normalMove(); randomNormalMove(); } random_num=random(0, 100); } LED_rampDown(); delay(5000); }

###試したこと
上のコードに対し、下のコードに変更したところ、
exit status 1
'play' was not declared in this scope
となってしまいました。

どなたか、コードの間違いを訂正してくださる方、よろしくお願いいします。。

###補足情報(言語/FW/ツール等のバージョンなど)
sample のRAWファイルは9993 bytesです。。

LEDが光る前にRAWファイルを再生 サーボが動く・・
つまり、loopの中に「RAW再生→LED点灯(明滅)→サーボも稼働→LED消灯→RAW再生→LED・・」
としたいのです。。

お手数おかけします。

スケッチの添削願います。。

追記です。
#include <Servo.h>

int mid_Pos=90;
int max_Pos=128; // Bigger value would make it go to further end
int min_Pos=50; // smaller value would make it go to another end further.
int LED_Power=0;
int LED_NormalPower=20;
int LED_MaxPower=245;
int random_num;
int speakerPin = 5;

Servo myservo; // create servo object to control a servo

// twelve servo objects can be created on most boards

int pos = mid_Pos; // variable to store the servo position

int ledpin = 3; //Pin for LED, with PWM control

const uint8_t sample05[] PROGMEM = {
0x7b,0x71,0x78,0xff,0x9e,0x00,0xe0,0x72,0x45,0x7f,0x88,0xae,0xa1,0xb3,0xa6,0xaf,
0x99,0x98,0x9b,0x95,0xa0,0xb5,0xd0,0xa3,0x00,0x00,0x00,0x98,0xcc,0xff,0xff,0xff,
・・・・・・・
0x7f,0x80,0x7f,0x80,0x7f,0x80,0x80,0x80,0x80,0x7f,0x7f,0x7f,0x7f,0x80,0x80,0x7f,
0x80,0x80,0x80,0x80,0x80,0x7f,0x80,0x7f,0x80
};

unsigned int sample05_len = 6000;

void setup() {

myservo.attach(9); // attaches the servo on pin 9 to the servo object

pinMode(ledpin, OUTPUT);
randomSeed(analogRead(0));

pinMode(3,OUTPUT);
analogWrite( speakerPin, 255);

TCCR2A = _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
TCCR2B = _BV(CS20);

play();

}

}
}

void loop() {

int eyeMoveTime=0;
int maxEyeMoveTime=25;

void play() {
for (int i = 0; i < sample05_len; i++) {
OCR2B = pgm_read_byte_near(&sample05[i]);
delayMicroseconds(160);

startUp();
normalMove();
delay(500);
random_num=0;
for (eyeMoveTime=0; eyeMoveTime<maxEyeMoveTime;eyeMoveTime++){
if (random_num>50){ //Panic Mode code
panicMove();
} else { // Normal Mode
//normalMove();
randomNormalMove();
}

random_num=random(0, 100);

}

LED_rampDown();

delay(5000);
}

/////
上記のようにすると
'play' was not declared in this scope
となってしまいます。

単純に真ん中のスケッチからカット&ペーストしただけです。
これでは、うまく働かないので、困っておりまして、質問させていただきました。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ozwk

2017/11/15 04:44 編集

loopの中でRAW再生したいなら、loopの中にRAW再生する処理書けばいいだけなんじゃないのと思うんですが何がわからんのでしょう?
takupi-2

2017/11/15 05:07

大変申し訳ございません。RAWファイル処理のスケッチが分からないのです。
guest

回答1

0

ベストアンサー

C++

1play();} // <- 変な}ついてる

C++

1void play(){ // play()はこういう処理だよと言う定義 2 // 内容 3}

C++

1play(); // 定義したplay()の実行

「関数loop内で関数playを実行する」:

C++

1void loop(){ 2 play(); 3}

基本的な文法すら怪しいというあなたのレベルに対して
あなたがやろうとしていることは内容が高度すぎます。
なので、もう少し基本から勉強してからやったほうが、
結果的に早くできます。

投稿2017/11/15 03:23

編集2017/11/15 05:43
ozwk

総合スコア13512

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

takupi-2

2017/11/15 04:26

あっ!(汗 ありがとうございます(__;) コンパイルできました・・ ですが、肝心のRAWが再生されません。 loopの中にRAW再生いれるには、どうスケッチすればよいですか?? お手数おかけしますが、よろしくお願いいたします。
takupi-2

2017/11/15 06:25

ありがとうございます。。 ちょっとした工夫で、うまく動作するスケッチができました。 アドバイスありがとうございます。 大変助かりました。。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問