¥¢¥Õ¥£¥ê¥¨¥¤¥È¹­¹ð


¢£Arduino / ubuntu¤È¥·¥ê¥¢¥ëÄÌ¿®¤·¤Æ¤ß¤¿ -3-

¥·¥ê¥¢¥ëÄÌ¿®¤òÄ̤·¤Æ¥Ñ¥½¥³¥ó¤«¤é Arduino ¤Ø¿ôÃͤòÁ÷¤ê¤¿¤¤¤È¤¤¤¦Ïá£

 

Á÷¤é¤ì¤Æ¤¯¤ë¤Î¤Ïʸ»úÎó¤Ê¤Î¤Ç¡¢¤½¤¤¤Ä¤ò¿ôÃͤËÊÑ´¹¤¹¤ë¤¿¤á¤Ë¤¤¤í¤¤¤íºÙ¹©¤¬É¬Íפˤʤ롣¤Ç¤â¤­¤Ã¤È²¿¤«¡¢¤â¤Ã¤È´Êñ¤ÊÊýË¡¤¬¤¢¤ë¤Ï¤º¤À¤È»×¤Ã¤Æ¤¤¤¿¤ï¤±¤Ç¤¹¤¬¡¢¤Ê¤ó¤È¡¢¤¢¤ë¤¸¤ã¤Ê¤¤¤Ç¤¹¤«¡¢¿ôÃͤò¤½¤Î¤Þ¤Þ¼è¤ê½Ð¤·¤Æ¤¯¤ì¤ë´Ø¿ô¤¬¡£

 

parseInt()

Description

parseInt() returns the first valid (long) integer number from the serial buffer. Characters that are not integers (or the minus sign) are skipped.

 

¤·¤«¤âÉí°¤Î¥¹¥±¥Ã¥ÁÎã¤Ë¤³¤ó¤Ê¤Î¤¬¤¢¤ê¤Þ¤¹¡£

 

Read ASCII String

This sketch uses the Serial.parseInt() function to locate values separated by a non-alphanumeric character. Often people use a comma to indicate different pieces of information (this format is commonly referred to as comma-separated-values or CSV), but other characters like a space or a period will work too. The values are parsed into integers and used to determine the color of a RGB LED. You'll use the Arduino Software (IDE) serial monitor to send strings like "5,220,70" to the board to change the light color.

 

 

  1. /*
  2.   Reading a serial ASCII-encoded string.
  3.   This sketch demonstrates the Serial parseInt() function.
  4.   It looks for an ASCII string of comma-separated values.
  5.   It parses them into ints, and uses those to fade an RGB LED.
  6.   Circuit: Common-Cathode RGB LED wired like so:
  7.   - red anode: digital pin 3
  8.   - green anode: digital pin 5
  9.   - blue anode: digital pin 6
  10.   - cathode: GND
  11.   created 13 Apr 2012
  12.   by Tom Igoe
  13.   modified 14 Mar 2016
  14.   by Arturo Guadalupi
  15.   This example code is in the public domain.
  16. */
  17. // pins for the LEDs:
  18. const int redPin = 3;
  19. const int greenPin = 5;
  20. const int bluePin = 6;
  21. void setup() {
  22.   // initialize serial:
  23.   Serial.begin(9600);
  24.   // make the pins outputs:
  25.   pinMode(redPin, OUTPUT);
  26.   pinMode(greenPin, OUTPUT);
  27.   pinMode(bluePin, OUTPUT);
  28. }
  29. void loop() {
  30.   // if there's any serial available, read it:
  31.   while (Serial.available() > 0) {
  32.     // look for the next valid integer in the incoming serial stream:
  33.     int red = Serial.parseInt();
  34.     // do it again:
  35.     int green = Serial.parseInt();
  36.     // do it again:
  37.     int blue = Serial.parseInt();
  38.     // look for the newline. That's the end of your sentence:
  39.     if (Serial.read() == '¥n') {
  40.       // constrain the values to 0 - 255 and invert
  41.       // if you're using a common-cathode LED, just use "constrain(color, 0, 255);"
  42.       red = 255 - constrain(red, 0, 255);
  43.       green = 255 - constrain(green, 0, 255);
  44.       blue = 255 - constrain(blue, 0, 255);
  45.       // fade the red, green, and blue legs of the LED:
  46.       analogWrite(redPin, red);
  47.       analogWrite(greenPin, green);
  48.       analogWrite(bluePin, blue);
  49.       // print the three numbers in one string as hexadecimal:
  50.       Serial.print(red, HEX);
  51.       Serial.print(green, HEX);
  52.       Serial.println(blue, HEX);
  53.     }
  54.   }
  55. }

 

¥¹¥±¥Ã¥ÁÎã¤Ç¤Ï¥«¥é¡¼ LED ¤ò»ÈÍѤ·¤Æ¤¤¤Þ¤¹¤¬¡¢ÅÀÅô¤ò»î¤¹¤À¤±¤Ê¤é 3¸Ä¤Î LED ¤ò»È¤¨¤Ð¤¤¤¤¤Ç¤¹¡£¥·¥ê¥¢¥ë¥â¥Ë¥¿¡¼¤«¤é 3¤Ä¤Î¿ôÃͤò¥«¥ó¥Þ¶èÀÚ¤ê¤ÇÁ÷¿®¤¹¤ë¤È¡¢¤½¤ÎÃͤ˱þ¤¸¤¿¿§ (ÌÀ¤ë¤µ) ¤Ç LED ¤¬ÅÀÅô¤·¤Þ¤¹¡£

¥·¥ê¥¢¥ë¥Ý¡¼¥È¤«¤éÁ÷¤é¤ì¤Æ¤¯¤ë¥Ç¡¼¥¿¤¬Ê¸»úÎó¤À¤Î¿ôÃͤÀ¤Î¹Í¤¨¤Ê¤¯¤Æ¤â¡¢¤½¤Î¤Þ¤Þ¿ôÃͤȤ·¤Æ½ÐÎϤǤ­¤Þ¤·¤¿¡£

 

¤Ê¤ª¡¢Serial.parseInt() ¤Ï¥¿¥¤¥à¥¢¥¦¥È¤Ç½ªÎ»¤·¤Þ¤¹¤Î¤Ç¡¢cu ¤Î¤è¤¦¤ËÆþÎϤ·¤¿¿ôÃͤò¨Á÷¿®¤·¤Æ¤·¤Þ¤¦¾ì¹ç¤Ï¡¢¥¿¥¤¥à¥¢¥¦¥È¤¹¤ë¤Þ¤Ç¤Ë¤¹¤Ù¤Æ¤Î¥Ç¡¼¥¿¤òÁ÷¿®¤¹¤ëɬÍפ¬¤¢¤ê¤Þ¤¹¡£¥·¥ê¥¢¥ë¥â¥Ë¥¿¡¼¤Î¤è¤¦¤Ë¥Ç¡¼¥¿¤ò¤Þ¤È¤á¤ÆÁ÷¿®¤¹¤ë¤â¤Î¤Ç¤¢¤ì¤ÐÌäÂê¤Ï¤Ê¤¤¤Ç¤¹¡£

¥×¥í¥°¥é¥à¤«¤é¤Ê¤é¡¢¥Ç¡¼¥¿¤ò¤Þ¤È¤á¤ÆºÇ¸å¤Ë²þ¹Ô¥³¡¼¥É¤ò¤Ä¤±¤ÆÁ÷¿®¤¹¤ì¤Ð¤è¤¤¤Î¤Ç¡¢¤Ê¤ó¤È¤Ç¤â¤Ê¤ë¤È»×¤¤¤Þ¤¹¡£

 

¤È¤¤¤¦¤³¤È¤Ç¡¢¥·¥ê¥¢¥ëÄÌ¿®¤Ç¿ôÃͤòÁ÷¤ë¤³¤È¤âÆñ¤·¤¯¹Í¤¨¤ëɬÍפʤó¤Æ¤Ê¤¤¤³¤È¤¬¤ï¤«¤ê¤Þ¤·¤¿¡£¤ä¤Ã¤Ñ¤ê¤Ê¤¡¡¢ÆüËܸì¥ê¥Õ¥¡¥ì¥ó¥¹¤À¤±¤¸¤ã¤Ê¤¯¤Æ¡¢¤Á¤ã¤ó¤È REFERENCE ¤òÆɤޤʤ¤¤È¤¤¤±¤Ê¤¤¤ó¤À¤è¤Ê¤¡ (^_^;)

 


¥³¥á¥ó¥È
¥³¥á¥ó¥È¤¹¤ë








   
¤³¤Îµ­»ö¤Î¥È¥é¥Ã¥¯¥Ð¥Ã¥¯URL
¥È¥é¥Ã¥¯¥Ð¥Ã¥¯

¢£calendar

S M T W T F S
     12
3456789
10111213141516
17181920212223
24252627282930
31      
<< March 2024 >>

¢£search this site.

¢£selected entries

¢£categories

¢£archives

¢£recent comment

¢£recent trackback

¢£links

¢£profile

¢£others

¢£mobile

qrcode

¢£powered

̵ÎÁ¥Ö¥í¥°ºîÀ®¥µ¡¼¥Ó¥¹ JUGEM