¢£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.
/*
Reading a serial ASCII-encoded string.
This sketch demonstrates the Serial parseInt() function.
It looks for an ASCII string of comma-separated values.
It parses them into ints, and uses those to fade an RGB LED.
Circuit: Common-Cathode RGB LED wired like so:
- red anode: digital pin 3
- green anode: digital pin 5
- blue anode: digital pin 6
- cathode: GND
created 13 Apr 2012
by Tom Igoe
modified 14 Mar 2016
by Arturo Guadalupi
This example code is in the public domain.
*/
// pins for the LEDs:
const int redPin = 3;
const int greenPin = 5;
const int bluePin = 6;
void setup() {
// initialize serial:
Serial.begin(9600);
// make the pins outputs:
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
// if there's any serial available, read it:
while (Serial.available() > 0) {
// look for the next valid integer in the incoming serial stream:
int red = Serial.parseInt();
// do it again:
int green = Serial.parseInt();
// do it again:
int blue = Serial.parseInt();
// look for the newline. That's the end of your sentence:
if (Serial.read() == '¥n') {
// constrain the values to 0 - 255 and invert
// if you're using a common-cathode LED, just use "constrain(color, 0, 255);"
red = 255 - constrain(red, 0, 255);
green = 255 - constrain(green, 0, 255);
blue = 255 - constrain(blue, 0, 255);
// fade the red, green, and blue legs of the LED:
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
// print the three numbers in one string as hexadecimal:
Serial.print(red, HEX);
Serial.print(green, HEX);
Serial.println(blue, HEX);
}
}
}
¥¹¥±¥Ã¥ÁÎã¤Ç¤Ï¥«¥é¡¼ LED ¤ò»ÈÍѤ·¤Æ¤¤¤Þ¤¹¤¬¡¢ÅÀÅô¤ò»î¤¹¤À¤±¤Ê¤é 3¸Ä¤Î LED ¤ò»È¤¨¤Ð¤¤¤¤¤Ç¤¹¡£¥·¥ê¥¢¥ë¥â¥Ë¥¿¡¼¤«¤é 3¤Ä¤Î¿ôÃͤò¥«¥ó¥Þ¶èÀÚ¤ê¤ÇÁ÷¿®¤¹¤ë¤È¡¢¤½¤ÎÃͤ˱þ¤¸¤¿¿§ (ÌÀ¤ë¤µ) ¤Ç LED ¤¬ÅÀÅô¤·¤Þ¤¹¡£
¥·¥ê¥¢¥ë¥Ý¡¼¥È¤«¤éÁ÷¤é¤ì¤Æ¤¯¤ë¥Ç¡¼¥¿¤¬Ê¸»úÎó¤À¤Î¿ôÃͤÀ¤Î¹Í¤¨¤Ê¤¯¤Æ¤â¡¢¤½¤Î¤Þ¤Þ¿ôÃͤȤ·¤Æ½ÐÎϤǤ¤Þ¤·¤¿¡£
¤Ê¤ª¡¢Serial.parseInt() ¤Ï¥¿¥¤¥à¥¢¥¦¥È¤Ç½ªÎ»¤·¤Þ¤¹¤Î¤Ç¡¢cu ¤Î¤è¤¦¤ËÆþÎϤ·¤¿¿ôÃͤò¨Á÷¿®¤·¤Æ¤·¤Þ¤¦¾ì¹ç¤Ï¡¢¥¿¥¤¥à¥¢¥¦¥È¤¹¤ë¤Þ¤Ç¤Ë¤¹¤Ù¤Æ¤Î¥Ç¡¼¥¿¤òÁ÷¿®¤¹¤ëɬÍפ¬¤¢¤ê¤Þ¤¹¡£¥·¥ê¥¢¥ë¥â¥Ë¥¿¡¼¤Î¤è¤¦¤Ë¥Ç¡¼¥¿¤ò¤Þ¤È¤á¤ÆÁ÷¿®¤¹¤ë¤â¤Î¤Ç¤¢¤ì¤ÐÌäÂê¤Ï¤Ê¤¤¤Ç¤¹¡£
¥×¥í¥°¥é¥à¤«¤é¤Ê¤é¡¢¥Ç¡¼¥¿¤ò¤Þ¤È¤á¤ÆºÇ¸å¤Ë²þ¹Ô¥³¡¼¥É¤ò¤Ä¤±¤ÆÁ÷¿®¤¹¤ì¤Ð¤è¤¤¤Î¤Ç¡¢¤Ê¤ó¤È¤Ç¤â¤Ê¤ë¤È»×¤¤¤Þ¤¹¡£
¤È¤¤¤¦¤³¤È¤Ç¡¢¥·¥ê¥¢¥ëÄÌ¿®¤Ç¿ôÃͤòÁ÷¤ë¤³¤È¤âÆñ¤·¤¯¹Í¤¨¤ëɬÍפʤó¤Æ¤Ê¤¤¤³¤È¤¬¤ï¤«¤ê¤Þ¤·¤¿¡£¤ä¤Ã¤Ñ¤ê¤Ê¤¡¡¢ÆüËܸì¥ê¥Õ¥¡¥ì¥ó¥¹¤À¤±¤¸¤ã¤Ê¤¯¤Æ¡¢¤Á¤ã¤ó¤È REFERENCE ¤òÆɤޤʤ¤¤È¤¤¤±¤Ê¤¤¤ó¤À¤è¤Ê¤¡ (^_^;)
- 2019.09.17 Tuesday
- ÅŻҹ©ºî
- 22:51
- comments(0)
- trackbacks(0)
- -
- by meyon
¢£calendar
¢£search this site.
¢£selected entries
¢£categories
- Æü¾ïÃãÈÓ (218)
- Æî̵°¤ÌïÂËÊ© (2)
- ¥É¥é¥¤¥Ö ι¹Ô (1)
- ²»³Ú (99)
- ¥Ñ¥½¥³¥ó°ìÈÌ (132)
- Ubuntu Server (24)
- linuxBean (9)
- Puppy Linux (18)
- Vine Linux (34)
- ¥²¡¼¥à (16)
- ¥×¥í¥°¥é¥ß¥ó¥° (9)
- ÅŻҹ©ºî (48)
¢£archives
- 2019ǯ
- 2019ǯ11·î (1)
- 2019ǯ10·î (1)
- 2019ǯ09·î (10)
- 2019ǯ08·î (3)
- 2019ǯ07·î (8)
- 2019ǯ06·î (4)
- 2019ǯ05·î (16)
- 2019ǯ04·î (9)
- 2019ǯ03·î (5)
- 2019ǯ02·î (6)
- 2019ǯ01·î (7)
¢£recent comment
- ²¡¤·¥Ü¥¿¥ó¥¹¥¤¥Ã¥Á¤Î¥¢¥¯¥·¥ç¥ó¤Î¸¡½Ð
¢Í ¤«¤¸ (04/04) - linuxBean¤òUSB¥á¥â¥ê¡¼¤ËÆþ¤ì¤Æ¤ß¤¿
¢Í USB Boot Agent (03/10) - Raziko ÃÏ°èȽÄê
¢Í Ä®ÅĤ¼¤ë¤µ¤Ý (05/19) - Windows 8 / Ubuntu 12.10 ¥Ç¥å¥¢¥ë¥Ö¡¼¥È¤Îµ¬ÄêÃͤòÊѹ¹¤¹¤ë
¢Í a (08/08) - Windows 8 / Ubuntu 12.10 ¤Î¥Ç¥å¥¢¥ë¥Ö¡¼¥È
¢Í yabu¡÷¿ÍÃ줷¤Ê¤¤¥Ò¥È¥Ð¥·¥é¡¼´ÉÍý¿Í (05/10) - Puppy Linux ²½ÀÐPCÉü³è·×²è Google Chrome ¤ò¥¤¥ó¥¹¥È¡¼¥ë¤¹¤ë
¢Í ¤Þ¤Ã¤µ¤ó (11/03) - GOM PLAYER flash.ocx ¤¬Ìµ¤¤¤È¼¸¤é¤ì¤ë¤È¤
¢Í ¤Ä¤¸ (04/27) - I-O DATA ±Õ¾½¥Ç¥£¥¹¥×¥ì¥¤ LCD-A155GW ¤ò½¤Íý¤¹¤ë
¢Í K.Y (09/13) - ¥¨¥ê¥¢³°¤«¤é radiko ¤òÄ°¤¯ÊýË¡
¢Í meyon (07/19) - ¥¨¥ê¥¢³°¤«¤é radiko ¤òÄ°¤¯ÊýË¡
¢Í Æࡹ»á¤Î¤´¤óÊÆ (07/07)
¢£recent trackback
- 2NE1 NOLZA
¢Í ¤³¤ì¹¥¤¡ª KoreaFan (01/31) - ¡î-ute ĶWONDERFUL!¦
¢Í Nowpie (¤Ê¤¦¤Ô¡¼) ¥¢¥¤¥É¥ë (01/06) - Perfume Album ¡ÖJPN¡×
¢Í Nowpie (¤Ê¤¦¤Ô¡¼) J-POP (12/04) - Perfume ¥Ô¡¼¥¿¡¼¥é¥Ó¥Ã¥È¤È»ä¡¡ #prfm #TGSJP
¢Í Nowpie (¤Ê¤¦¤Ô¡¼) J-POP (01/16)