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

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

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

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

Q&A

2回答

2237閲覧

Arduinoのクラス作成の方法を教えていただきたいです

TeTeTeT

総合スコア0

Arduino

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

0グッド

0クリップ

投稿2022/01/09 02:51

Arduinoでのクラス作成

・質問の内容
Arduinoでクラスを作成して、LEDアレイを用いた演出に使うコードを組んでいたのですが、以下のエラーメッセージが発生しました。
有識者の方教えていただきたいです。

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

base.h:9:5:warning: extra qualification 'Kikn::' on member 'Kikn' [-fpermissive] Kikn::Kikn(int _colorcode[]) base.h:12:3:error: 'Serial' was not declared in this scope Serial.begin( 9600 ); base.h:19:18: error: 'OUTPUT' was not declared in this scope pinMode(pin, OUTPUT); base.h:19:5: error: 'pinMode' was not declared in this scope pinMode(pin, OUTPUT); base.h:20:23: error: 'LOW' was not declared in this scope digitalWrite(pin, LOW); base.h:20:5: error: 'digitalWrite' was not declared in this scope digitalWrite(pin, LOW); base.h:28:11: error: 'A3' was not declared in this scope pinMode(A3, OUTPUT); base.h:28:15: error: 'OUTPUT' was not declared in this scope pinMode(A3, OUTPUT); base.h:28:3: error: 'pinMode' was not declared in this scope pinMode(A3, OUTPUT); base.h:30:11: error: 'A4' was not declared in this scope pinMode(A4, OUTPUT); base.h:32:11: error: 'A5' was not declared in this scope pinMode(A5, OUTPUT); base.cpp:5:7: error: 'Serial' was not declared in this scope if (Serial.available() > 0) base.cpp:12:22: error: 'random' was not declared in this scope pindata = (int)random(2, 12); base.cpp:15:36: error: expected primary-expression before ')' token for (int rgb = 0; rgb < 3; rgb + ) base.cpp:20:48: error: 'LOW' was not declared in this scope if (cPin == rgb) digitalWrite(cPin + 17, LOW); base.cpp:20:24: error: 'digitalWrite' was not declared in this scope if (cPin == rgb) digitalWrite(cPin + 17, LOW); base.cpp:20:24: error: 'digitalWrite' was not declared in this scope if (cPin == rgb) digitalWrite(cPin + 17, LOW); base.cpp:22:36: error: 'HIGH' was not declared in this scope else digitalWrite(cPin + 17, HIGH); base.cpp:22:12: error: 'digitalWrite' was not declared in this scope else digitalWrite(cPin + 17, HIGH); base.cpp:26:17: error: 'pindata' was not declared in this scope digitalWrite( pindata, HIGH ); base.cpp:26:26: error: 'HIGH' was not declared in this scope digitalWrite( pindata, HIGH ); base.cpp:26:3: error: 'digitalWrite' was not declared in this scope digitalWrite( pindata, HIGH ); base.cpp:28:3: error: 'delay' was not declared in this scope delay(5); base.cpp:28:3: note: suggested alternative: 'display' delay(5); base.h:9:5: warning: extra qualification 'Kikn::' on member 'Kikn' [-fpermissive] Kikn::Kikn(int _colorcode[]) last_mission:9:17: error: expected primary-expression before ']' token Kikn kikn(color[]) = new Kikn; last_mission:9:20: error: expected ',' or ';' before '=' token Kikn kikn(color[]) = new Kikn; exit status 1 'Serial' was not declared in this scope

該当のソースコード

Arduino

1<lastmission.ino> 2#include "Arduino.h" 3#include "base.h" 4 5int color[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; 6 7Kikn kikn(color[]) = new Kikn; 8 9 10void setup() { 11 12} 13 14void loop() { 15 kikn.display(); 16} 17<base.cpp> 18#include "base.h" 19 20void Kikn::display(void) 21{ 22 if (Serial.available() > 0) 23 { 24 int pindata = Serial.read(); 25 //数値データを読み込む 26 pindata = pindata % 12; 27 if (pindata == 0 || pindata == 1) 28 { 29 pindata = (int)random(2, 12); 30 } 31 } 32 for (int rgb = 0; rgb < 3; rgb + ) 33 { 34 for (int cPin = 0; cPin < 3; cPin++) 35 { 36 //カソードの出力状況 37 if (cPin == rgb) digitalWrite(cPin + 17, LOW); 38 //光る1つの条件クリア 39 else digitalWrite(cPin + 17, HIGH); 40 } 41 } 42 43 digitalWrite( pindata, HIGH ); 44 //アノードの出力状態・光る2つ目の条件クリア 45 delay(5); 46} 47 48<base.h> 49#ifndef base_h 50#define base_h 51class Kikn 52{ 53 public: 54 void display(); 55 private: 56 int colorcode[10]; 57 Kikn::Kikn(int _colorcode[]) 58 //インスタンス化(1度だけ通す) 59{ 60 Serial.begin( 9600 ); 61 for (int x = 0; x < 10; x++) 62 { 63 colorcode[x] = _colorcode[x]; 64 } 65 for (int pin = 2; pin <= 11; pin++) 66 { 67 pinMode(pin, OUTPUT); 68 digitalWrite(pin, LOW); 69 //アノード初期化 70 } 71 /* 72 以下3行で出力ピンを設定※アナログピンをデジタルとして使用 73 A3=17,A4=18,A5=19として設定することも可能 74 カソード 75 */ 76 pinMode(A3, OUTPUT); 77 //赤・17 78 pinMode(A4, OUTPUT); 79 //青・18 80 pinMode(A5, OUTPUT); 81 //緑・19 82} 83 84/* 85 1または10番ピンをGND、11~20番ピンに電圧を印加すると赤色に光ります。 86 また、同様に2または9番ピンをGNDで青色、3または8番ピンをGNDで緑色に光ります。 87*/ 88 89}; 90#endif 91//piblicが終わった後にセミコロンをつける

試したこと

エラーを検索にかけて修正していました。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

回答2

0

まず、

private:

Kikn::Kikn(int _colorcode[])

クラスのコンストラクタをprivateにしてどーすんねん!、とおっしゃってます
そして、

base.h:12:3:error: 'Serial' was not declared in this scope

Serialが定義されてない、とおっしゃってます

投稿2022/01/09 05:17

y_waiwai

総合スコア87719

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

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

TeTeTeT

2022/01/09 05:47

ありがとうございます!確かに突っ込まれた通りコンストラクタがprivateにあるのはアウトでした、、 SerialはArduino.hをインクルードすることで改善されました!
y_waiwai

2022/01/09 06:29

んでもひとつ、 Arduinoではnewは(ほとんど)使えません
TeTeTeT

2022/01/09 07:00

それではArduinoでクラスによって一つ新たなセット?を作成するときはnewではなく何を用いるのでしょうか?
y_waiwai

2022/01/09 07:20 編集

使えない、というより使わないという方が正確かも。 CでもC++でもなく、Arduino言語、と言われるゆえんですが、オリジナル(?)のArduinoUnoなどはRAMが2KByteしかないです。そのため(そのうえ)、動的にメモリを確保するという関数は実装されない、という制限があります まあ、C++の何でもかんでも使えるわけでなく、いわばC++のサブセットという感じですね IDEを入れていれば、ソースファイルも入ってるはずなんで、ArduinoのWireとかSerialの実装を見てみれば参考になるかと思います
TeTeTeT

2022/01/09 07:45

IDEは入っているので、探してみます 丁寧にありがとうございます。
guest

0

Arduino.hをインクルードしてください

投稿2022/01/09 04:02

ozwk

総合スコア13512

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

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

TeTeTeT

2022/01/09 04:28 編集

コメントありがとうございます、cppとhの先頭にインクルードしました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問