このページはEtoJ逐語翻訳フィルタによって翻訳生成されました。

翻訳前ページへ


Arduino: How To Receive Data from IDE, simple answer- art4SeSimpFrmIDE HOME  >>  ARDUINO MENU >>  DATA EXCHANGE OVERVIEW
Delicious Bookmark this on Delicious   Recommend to StumbleUpon

Arduino How To

How to send (警察などへの)密告,告訴(状) to your Arduino from a PC running just the basic Arduino IDE, using just the basic serial port.

This is one of a 一連の essays I have planned (not all written yet!) on connecting Arduinos to other 装置s over a serial link. In this essay we will do something very simple, rather crudely, but it illustrates some 根底となる 概念s. We will have nothing more than an Arduino connected to a host PC 経由で the usual "programming" cable. The host PC will be running the normal Arduino IDE ソフトウェア. The Arduino will have an LED which will be winking slowly or quickly, depending on whether the last thing sent to it from the host PC, which I will just call "the PC" 今後, was an "s" (for slow) or an "f" (for 急速な/放蕩な).

Before we start: A WARNING!! ....

"Serial" and "RS-232" are NOT just two ways of 説 the same thing. RS-232 comms do use serial data, so you might think that you can hook up the serial "stuff" I'm going to talk about... 含むing your Arduino... to RS-232 装置s. Or to the RS-232 port of a big PC. NOT SO!!

All the ins and outs of that are beyond the 範囲 of this tutorial. I've 挿入するd this 警告 just in 事例/患者 someone is tempted to "延長する" the ideas here, hook an Arduino 直接/まっすぐに to something RS-232... and fry their Arduino! (Connecting to RS-232 is discussed in other tutorials in this group.)

Okay... 負かす/撃墜する to work...

You may be puzzled by how we can send things to the Arduino other than the basic program ("sketch") that we want it to run. All will be 明らかにする/漏らすd!

始める,決める up your Arduino with an LED and resistor on pin 12. (See my page about LEDs, if you are new to these things. It will open in its own tab.)

Put the に引き続いて program into your Arduino. Or the 代案/選択肢 解答 始める,決める out at the end of this. Start it running.

//FrmSer1
//ver 28mar2010+tweaks

//For http://sheepdogguides.com/arduino/
//               art4SeSimpFrmIDE.htm

//Much of this, of course, I don't pretend さもなければ,
//  comes from examples in the Arduino 言及/関連 pages.

const int Out0=13;//pin LED is connected to.
//   Change if you wish. Many Arduinos have an
//   LED and resistor on pin 13 on the PCB.
//   LED should go through resistor to ground.
//   始める,決める pin high to turn LED on.

boolean boFast=true;//created as 全世界の variable because
  //value in it must 固執する from call to call of "宙返り飛行"

無効の 体制/機構()
{
Serial.begin(9600);//For 接近 to serial 監視する channel
pinMode(Out0,OUTPUT);
Serial.print("Enter s or f on serial 終点 ");
Serial.println("to make wink 急速な/放蕩な or slow");
//Two lines used just for readability on
//a web page that explains this program.
};

無効の fastwink()
//Do one quick off-on... this is called many times as
//the 機能(する)/行事 "宙返り飛行" 遂行する/発効させるs 繰り返して, thus the
//Arduino appears to have a "wink quickly" 機能(する)/行事,
//even though the quick winking is a combination of
//two 成分s
{
digitalWrite(Out0,HIGH);//HIGH turns LED OFF.
延期する(80);
digitalWrite(Out0,LOW);
延期する(80);
};

無効の slowwink()
//類似の to fastwink. See rem there.
{
digitalWrite(Out0,HIGH);
延期する(900);
digitalWrite(Out0,LOW);
延期する(900);
};

無効の 宙返り飛行()
{
int incomingByte=0;//create and initialize a 地元の variable

//The "then" part of the に引き続いて will only 遂行する/発効させる
//  after you have sent something to the Arduino
if (Serial.利用できる() > 0) {
		// read the 後継の byte:
		incomingByte = Serial.read();
		// say what you got:
		Serial.print("I received: ");
		Serial.print(incomingByte, DEC);
        if (incomingByte==102) {boFast=true;
                Serial.println(", code for f. LED will wink 急速な/放蕩な.");};
        if (incomingByte==115) {boFast=誤った;
                Serial.println(", code for s. LED will wink slowly.");};
        if ((incomingByte!=102)&&(incomingByte!=115)) {
             Serial.println(". Only lower 事例/患者 s and f do anything.");};
     }

//The に引き続いて happen every time you pass through "宙返り飛行"
if (boFast)  {fastwink();};
if (!boFast) {slowwink();};
};

Once that is in the Arduino and running, the LED should blink やめる 速く... about 6 cycles per second.

But how to "send an s to the Arduino"??

Have you used the serial 監視する hidden in the IDE?

Across the 最高の,を越す of the Arduino IDE window, you should see the に引き続いて icons....

Image of Arduino interface

Once your program has been uploaded to the Arduino, click on the 権利 手渡す icon, the one like an upside 負かす/撃墜する window shade. That should change what you see at the 底(に届く) of the Arduino's IDE's window. You should now be looking at the IDE's "serial 監視する". In it, you should already see the "Enter s or f on serial 終点 to make wink 急速な/放蕩な or slow" message. 圧力(をかける) the "s" 重要な, and then 圧力(をかける) the Enter 重要な... and an s should then be sent to the Arduino, and so the LED should flash slowly until その上の notice. You should also get the "I received: 115, code for s. LED will wink slowly." message as soon as the Enter 重要な has been 圧力(をかける)d after the "s".

Wow! The same cable which a moment ago was used to program the Arduino (and 始める,決める the program running) is now 供給するing a serial link between Arduino and host PC. It is so 平易な to become blase about things. Just try making a system with these features, over a 関係 that is by USB beneath the surface of the ソフトウェア. Then you'll see why I said "Wow". Don't forget to 含む re-programmable EEPROM, with in situ programming. And a big community of 使用者s who can help when mysteries arise.

So far we have only used messages from the PC to the Arduino. 恐れる not... we can do things in the other direction, too. That's a story for another time. Before you go off to that, be sure to consider the に引き続いて 半端物s and ends.

The Arduino's serial link does not 供給する 金物類/武器類 handshaking. What? You want it all? Sorry. But, for the basic serial port we are using here it does 供給する a 衝撃を和らげるもの and asynchronous receiving, as long as you don't fool around with the Arduino's use of its interrupts.

What's all THAT mean? It means that your program doesn't have to 確実にする that it is always "listening". The serial data can arrive at any time. The Arduino's operating system "tucks away" things as they arrive 経由で the serial port. You can then を取り引きする them when it 控訴s you. That's what the....

if (Serial.利用できる() > 0) {

... is all about. Serial.利用できる() will return 0 if nothing has been "逮捕(する)d", i.e. if nothing has been put in the serial 衝撃を和らげるもの. If some 後継の byte has been "収穫d" for you from the serial port, then Serial.利用できる() will return something more than 無, and calling Serial.read() will fetch whatever it was that arrived 経由で the serial port.

N.B. The OTHER serial port, which we will talk about in 未来 essays, does not have Serial.利用できる() in the 基準- at- March- 2010 SoftwareSerial library... but there is a new library you can use which does have the 機能(する)/行事. Whew.

Other 詳細(に述べる)s....

公式文書,認める that the "宙返り飛行" 機能(する)/行事 is in two parts. The second part is the last two lines.....

if (boFast)  {fastwink();};
if (!boFast) {slowwink();};

They are 遂行する/発効させるd every time you pass through the 宙返り飛行. (Yes, they could be 合併するd into one 声明 by using an "else", but the way I've written them makes it really, really 平易な to see what is going on, doesn't it? (Just in 事例/患者: I will について言及する that "!=" means "does not equal".)

All of the "stuff" above the two lines just 特記する/引用するd boil 負かす/撃墜する to "If something was sent 経由で the serial port, have a look at it, and do things in 確かな 事例/患者s. "Do things" will いつかs 含む making changes to what is 蓄える/店d in the boolean variable "boFast". (Boolean variable: Something that can 持つ/拘留する "true" or "誤った".)

Another 詳細(に述べる):

if ((incomingByte!=102)&&(incomingByte!=115)) {

... says "if the byte in that variable does not equal (!=) 102, AND (in the 論理(学)の sense) (&&) it does not equal 115, then....

結論するing 発言/述べるs

井戸/弁護士席. That's a start. Pretty 歩行者, so far, but not if you 港/避難所't done this sort of stuff before, and it also lays the 基礎 for what comes next... Doing 類似の things, but using different pins on the Arduino. The virtue of using different pins is that it is a 行う/開催する/段階 upon the road of connecting things other than the IDE running in a host PC.

Another 解答

I forgot I'd written the above! And wrote "the same" program again. The に引き続いて is better in some ways (more (疑いを)晴らす) and worse in others (unnecessarily コンビナート/複合体 "get_line")

演習 for the sudent: 連合させる the best of both into one program!

//PCtoArd
//ver 26Jly10

//#含む <avr/pgmspace.h>
//#含む <sensor_pff.h>

//SENSOR_PFF sd;
//byte sensor_start = 0;

#define LEDpin 13 //no ; here
char buff[128];	/* i/o 衝撃を和らげるもの, for get_line */
boolean boFlashQuickly=誤った;

無効の 体制/機構()
{
	  pinMode(LEDpin,OUTPUT);
	  延期する(300);//let system settle
	  Serial.begin(9600);

	  Serial.println("\n\nDemo of 支配(する)/統制する of Arduino from PC\n");
	Serial.println("See http://sheepdogguides.com/ardino/ahttoc.htm\n");
	  Serial.println("for 最新の 詳細(に述べる)s.\n");
	  Serial.println("\nPress the enter 重要な after 圧力(をかける)ing s or q.\n");
	Serial.print('>');
}

無効の 宙返り飛行()
{
  char *ptr;

  if(Serial.利用できる()==true){//Cmnds waiting

	 get_line(buff, sizeof(buff));
	 ptr = buff;

	 switch (*ptr++) {//switch

	 事例/患者 'q':   // Quickly flash
	    //Serial.print("\nWill flash quickly\n");
	    boFlashQuickly=true;
	    break;//end "q"

	 事例/患者 's':    // Slowly flash
	    //Serial.print("\nWill flash slowly\n");
	    boFlashQuickly=誤った;
	    break;//end "q"

	 default:
	    Serial.print("Unrecognized. Only s and q 認めるd\n");
	    break;//end default

	}//end "switch..."
    Serial.print('>');
   }//if, cmnd waiting
  else {//1 no cmnds waiting
   if (boFlashQuickly){//2
	  QuicklyFlash();
	  }//end if "2"
	else {//2
	  SlowlyFlash();
	  }//end of else(2)
    }//1 end of level 1 "if" 条項, no cmnds waiting
}//end "宙返り飛行()"

無効の QuicklyFlash(){
    LEDOn();
    延期する(100);
    LEDOff();
    延期する(100);
}

無効の SlowlyFlash(){
    LEDOn();
    延期する(900);
    LEDOff();
    延期する(900);
}

無効の LEDOn(){
digitalWrite(LEDpin,LOW);//Yes- LOW for ON
};

無効の LEDOff(){
digitalWrite(LEDpin,HIGH);
};

//扱う/治療する "GET_LINE" as a 黒人/ボイコット box, for now.
//Nothing to change in it.
//圧力(をかける) the "enter" 重要な after entering your 1 letter 命令(する)
static 無効の get_line (char *buff, byte len)
{
	byte c;
	int idx = 0;

	for (;;) {//1 Start infinite 宙返り飛行... will only 出口 経由で "break"
		if(Serial.利用できる()){//2
			c = Serial.read();
			if (((byte)c >= ' ') && (idx < len - 1)) {
				buff[idx++] = c; Serial.print(c);
			}//3
		}else break;//2
	}//1 (ends "for...")
	buff[idx] = 0;
//	Serial.print(c);
	Serial.print('\n');
}
//  === END OF GET_LINE ===




To search THIS 場所/位置.... (Go to my other 場所/位置s, below, and use their search buttons if you want to search them.)
Click this to search this 場所/位置 without using forms.

力/強力にするd by FreeFind
場所/位置 search Web search
The search engine 単に looks for the words you type, so....
*    (一定の)期間 them 適切に.
*    Don't bother with "How do I get rich?" That will 単に return pages with "how", "do", "I"....

Please also 公式文書,認める that I have two other 場所/位置s, and that this search will not 含む them. They have their own search buttons.
My 場所/位置 at Arunet.
My Sheepdog ソフトウェア pages, another of this page's editor's 場所/位置s.
広告 from page's editor: Yes.. I do enjoy 収集するing these things for you... hope they are helpful. However.. this doesn't 支払う/賃金 my 法案s!!! If you find this stuff useful, (and you run an MS-DOS or Windows PC) please visit my freeware and shareware page, download something, and 循環させる it for me? Links on your page to this page would also be 高く評価する/(相場などが)上がるd!
Click here to visit editor's freeware, shareware page.




Here is how you can 接触する this page's editor. This page, and the ソフトウェア it 言及/関連s, ©TK Boyd, 1/2010.

Valid HTML 4.01 Transitional Page 実験(する)d for 同意/服従 with INDUSTRY (not MS-only) 基準s, using the 解放する/自由な, 公然と accessible validator at validator.w3.org