カテゴリ:Arduino
要望があったので、
Arduinoで10Keyをアナログ接お続でつかってみました。 のスケッチを晒します。 この記事を書いたとき、あまり自信がなかったので公開しなかったのですが。 他の方のスケッチをパクッて改造しただけのものですので、処理があまりよろしくないところもあるかもしれませんが、参考程度に見ていただければ、と思います。 ArduinoIDEのシリアルモニタに入力されたキーの文字を返すだけのスケッチです。 ただ、デジタル9番ピンにPWMでレベルが出力されています。 シリアルモニタに出力するところを7segLED表示器表示などに変更する等の応用をすれば実用的なものになる?と思われます。 (以下スケッチ、ArduinoIDEにコピペして試してみてください。全角の<は半角文字に置き換えてください。) /* Analog input, analog output, serial output Reads an analog input pin, maps the result to a range from 0 to 255 and uses the result to set the pulsewidth modulation (PWM) of an output pin. Also prints the results to the serial monitor. The circuit: * potentiometer connected to analog pin 0. Center pin of the potentiometer goes to the analog pin. side pins of the potentiometer go to +5V and ground * LED connected from digital pin 9 to ground created 29 Dec. 2008 modified 9 Apr 2012 by Tom Igoe This example code is in the public domain. */ // These constants won't change. They're used to give names // to the pins used: const int analogInPin = A0; // Analog input pin that the potentiometer is attached to const int analogOutPin = 9; // Analog output pin that the LED is attached to int sensorValue = 0; // value read from the pot int outputValue = 0; // value output to the PWM (analog out) void setup() { // initialize serial communications at 9600 bps: Serial.begin(9600); } void loop() { // read the analog in value: sensorValue = analogRead(analogInPin); // map it to the range of the analog out: outputValue = map(sensorValue, 0, 1023, 0, 255); // change the analog out value: analogWrite(analogOutPin, outputValue); // print the results to the serial monitor: Serial.print("sensor = " ); Serial.print(sensorValue); // //注意:以下の<は全て半角文字に置き換えてください。 // if(( 80<sensorValue)&&(sensorValue<=100)){Serial.print(" Pushed ");Serial.println("A");} if((100<sensorValue)&&(sensorValue<=135)){Serial.print(" Pushed ");Serial.println("B");} if((135<sensorValue)&&(sensorValue<=165)){Serial.print(" Pushed ");Serial.println("C");} if((165<sensorValue)&&(sensorValue<=190)){Serial.print(" Pushed ");Serial.println("D");} if((190<sensorValue)&&(sensorValue<=240)){Serial.print(" Pushed ");Serial.println("3");} if((240<sensorValue)&&(sensorValue<=300)){Serial.print(" Pushed ");Serial.println("6");} if((300<sensorValue)&&(sensorValue<=340)){Serial.print(" Pushed ");Serial.println("9");} if((340<sensorValue)&&(sensorValue<=390)){Serial.print(" Pushed ");Serial.println("#");} if((390<sensorValue)&&(sensorValue<=440)){Serial.print(" Pushed ");Serial.println("2");} if((440<sensorValue)&&(sensorValue<=510)){Serial.print(" Pushed ");Serial.println("5");} if((510<sensorValue)&&(sensorValue<=560)){Serial.print(" Pushed ");Serial.println("8");} if((560<sensorValue)&&(sensorValue<=640)){Serial.print(" Pushed ");Serial.println("0");} if((640<sensorValue)&&(sensorValue<=700)){Serial.print(" Pushed ");Serial.println("1");} if((700<sensorValue)&&(sensorValue<=750)){Serial.print(" Pushed ");Serial.println("4");} if((750<sensorValue)&&(sensorValue<=780)){Serial.print(" Pushed ");Serial.println("7");} if((780<sensorValue)&&(sensorValue<=850)){Serial.print(" Pushed ");Serial.println("*");} Serial.println(" "); // wait 2 milliseconds before the next loop // for the analog-to-digital converter to settle // after the last reading: delay(500); } お気に入りの記事を「いいね!」で応援しよう
最終更新日
2016.02.01 23:35:05
コメント(0) | コメントを書く |