Data fra Arduino til Debug Vinduet på PC-en. Når der skal bruges kommunikation til Debugvinduet, kan man ikke bruge Arduino s pin 0 og 1, idet de bruges til den serielle transmission mellem PC og Arduino. Her følger et antal eksempler: /* * Math is fun! */ #include "math.h" // include the Math Library int a = 3; int b = 4; int h; // run once, when the sketch starts // set up Serial library at 9600 bps Serial.println("Lets calculate a hypoteneuse"); // send data til // Debugvinduet Serial.print("a = "); Serial.println(a); // ln efter print tilføjer en linefeet Af: Valle Thorø Fil.: Side 1 af 11
Serial.print("b = "); Serial.println(b); h = sqrt( a*a + b*b ); Serial.print("h = "); Serial.println(h); // we need this to be here even though its empty Når der sendes data fra Arduinoen til Debug-vinduet, vil det, der vises være afhængig af datatypen. Dvs. at hvis indholdet i en integer fx er 38 decimal, vil der blive sendt 33h og 38h til PC-en, så den kan skrive 38 på skærmen. Serial.print sender karakterer, der repræsenterer de enkelte cifre i et tal, dvs. deres ascii-værdier til skærmen, så de er menneske-læsbare // run once, when the sketch starts // set up Serial library at 9600 bps Serial.print("Test value is: "); Hvis værdien i variable test er int test = 32767; // en Int-variabel er jo 16 bit!! Serial.println(test); // Der skrives 32767 på debug-vinduet Hvis et tal er af typen Float, dvs. flydende kommatal, vises default 2 decimaler. Serial.write sender binary data. Enten som en byte eller en serie af bytes. Serial.write(45); // send a byte with the value 45 Eksempler: Af: Valle Thorø Fil.: Side 2 af 11
/* * Serial Formatting * Print values in various formats to the serial port */ char chrvalue = 65; // these are the starting values to print byte bytevalue = 65; int intvalue = 65; float floatvalue = 65.0; Serial.println("chrValue: "); // chrvalue: Serial.println(chrValue); // A Serial.write(chrValue); // A Serial.println(chrValue,DEC); // 65 Serial.println("byteValue: "); // bytevalue: Serial.println(byteValue); // 65 Serial.write(byteValue); // A Serial.println(byteValue,DEC); // 65 Serial.println("intValue: "); // intvalue: Serial.println(intValue); // 65 Serial.println(intValue,DEC); // 65 Serial.println(intValue,HEX); // 41 Serial.println(intValue,BIN); // 1000001 Serial.println("floatValue: "); // floatvalue: Serial.println(floatValue); // 65.00 Serial.print(78, BIN) // viser "1001110" Serial.print(78, OCT) // viser "116" Serial.print(78, DEC) // viser "78" Serial.print(78, HEX) // viser "4E" Serial.println(1.23456, 0) // viser "1", dvs. 0 decimaler Serial.println(1.23456, 2) // viser "1.23" Serial.println(1.23456, 4) // viser "1.2346" Serial.print("\t"); // prints a tab Af: Valle Thorø Fil.: Side 3 af 11
Transmit a byte. You can also use Serial.write(buffer, length) to send more than one byte at a time, for very fast and efficient data transmission. Data fra Debug Vinduet på PC-en til Arduino Ligesom man kan sende data fra Arduino til PC-en, kan man sende data modsatte vej. Det man vil sende, indskrives i øverste rude i Debugvinduet, og sendes serielt via USB-kablet. Her er et par eksempler på, hvordan det kan bruges: Det, der sendes fra Debug-vinduet, sendes som ASCII-værdien af tallene eller bogstaverne! /* * SerialReceive sketch * Blink the LED at a rate proportional to the received digit value */ const int ledpin = 13; // pin the LED is connected to int blinkrate=0; // blink rate stored in this variable pinmode(ledpin, OUTPUT); // Initialize serial port to send and receive // at 9600 baud // set this pin as output if ( Serial.available()) // Check to see if at least one character // is available char ch = Serial.read(); if(ch >= '0' && ch <= '9') // is this an ascii digit between 0 and 9? Af: Valle Thorø Fil.: Side 4 af 11
blinkrate = (ch - '0'); // ASCII værdien for 0, dvs. 48 trækkes // fra for at konvertere til et tal digit blink(); blinkrate = blinkrate * 100; // actual blinkrate is 100 ms // times received void blink() digitalwrite(ledpin,high); delay(blinkrate); digitalwrite(ledpin,low); delay(blinkrate); // blink the LED with the on and off times // determined by blinkrate // delay depends on blinkrate value int value = 0; int sign = 1; if( Serial.available()) char ch = Serial.read(); if(ch >= '0' && ch <= '9') // is this an ascii digit between 0 // and 9? value = (value * 10) + (ch - '0'); // yes, accumulate the value else if( ch == '-') sign = -1; else value = value * sign ; Serial.println(value); value = 0; sign = 1; // this assumes any char not a digit or // minus sign terminates the value // set value to the accumulated value // reset value to 0 ready for the next // sequence of digits if( isdigit(ch) ) // er ASCII-tegn mellem Zeichen zwischen 0 und 9? Af: Valle Thorø Fil.: Side 5 af 11
/* Dette eksempel modtager data fra PC-en og udfører en handling afhængig af, hvad der sendes til Arduinoen */ if (Serial.available()) char ch = Serial.read(); if (ch >= '0' && ch <= '7') // kun hvis mellem ASCII 30 og 37 int led = ch - '0'; bitset(leds, led); // Do something updateshiftregister(); // Do something Serial.print("Turned on LED "); Serial.println(led); if (ch == 'x') // Hvis lig ASCII-værdien for X // Do.. leds = 0; updateshiftregister(); Serial.println("Cleared"); Se fx Youtube: http://www.youtube.com/watch?v=t8u1cm2hkia Eksempel 2: int inbyte = 0; int outputpin = 13; // incoming serial byte // start serial port at 9600 bps: pinmode(outputpin, OUTPUT); if (Serial.available() > 0) inbyte = Serial.read(); if (inbyte == 'E') // get incoming byte: Af: Valle Thorø Fil.: Side 6 af 11
digitalwrite(outputpin, HIGH); else if (inbyte == 'F') digitalwrite(outputpin, LOW); else Serial.print('H'); delay(1000); Serial.print('L'); delay(1000); http://forum.arduino.cc/index.php?topic=45952.0 Se in depth tutorial : http://forums.trossenrobotics.com/tutorials/how-to-diy-128/complete-control-of-an-arduino-via-serial-3300/ Eksempel: /* ------------------------------------------------ * SERIAL COM - HANDELING MULTIPLE BYTES inside ARDUINO - 01_simple version * by beltran berrocal * * this program establishes a connection with the pc and waits for it to send * a long string of characters like "hello Arduino!". * Then Arduino informs the pc that it heard the whole sentence * * this is the first step for establishing sentence long conversations between * arduino and the pc. * * serialread() reads one byte at a time from the serial buffer. * so in order to print out the whole sentence at once * (it is actually still printing one byte at a time but the pc will receive it * not interupted by newlines or other printstring inside your loop) * You must loop until there are no more bytes in the serial buffer and * print right away that byte you just read. * After that the loop can continue it's tasks. * * created 15 Decembre 2005; * copyleft 2005 Progetto25zero1 <http://www.progetto25zero1.com> * * --------------------------------------------------- */ int serin; //var that will hold the bytes in read from the serialbuffer Af: Valle Thorø Fil.: Side 7 af 11
//auto go_to_the_line function //void printnewline() // Serial.print(13, BYTE); // Serial.print(10, BYTE); // void loop () //simple feedback from Arduino Serial.println("Hello World"); if(serial.available()) // execute the following code only if there are // bytes in the serial buffer Serial.print("Arduino heard you say: "); // Tell PC, that Arduino heard you send something // keep reading and printing from serial until // there are bytes in the serial buffer while (Serial.available()>0) serin = Serial.read(); //read Serial Serial.print(serIn, BYTE); //prints the character just read Serial.println(); //the serial buffer is over just go to the line // (or pass your favorite stop char) delay(1000); //slows down the visualization in the terminal Kilde: http://my.safaribooksonline.com/book/hobbies/9781449399368/4dot-serial-communications/receiving_serial_data_in_arduino ( Der er flere eksempler på siden, linket herover! ) Med følgende kode kan man indskrive en tekst i Debugvinduet på PC-en. if (Serial.available()) delay(100); lcd.clear(); // when characters arrive over the serial port... // wait a bit for the entire message to arrive // clear the screen Af: Valle Thorø Fil.: Side 8 af 11
while (Serial.available() > 0) // read all the available characters lcd.write(serial.read()); // display each character to the LCD Se: http://arduino.cc/en/tutorial/liquidcrystalserial Følgende kode Fragment afslutter beendet das Einlesen der Ziffern bei jedem Zeichen, das keine Ziffer ist (oder bei vollem Puffer): const int MaxChars = 5; char strvalue[maxchars+1]; int index = 0; // En int-string besår af indtil 5 cifre // og afsluttes med et 0, EOS // Skal være stor nok til cifrene og // afsluttende nul. // Array-Index til at lagre det // modtagne ciffer if( Serial.available()) char ch = Serial.read(); if( index < MaxChars && isdigit(ch) ) strvalue[index++] = ch; // ASCII-tegn tilføjes til String; Else // Buffer fuld, eller første Non-ciffer strvalue[index] = 0; // String afsluttes med et 0 blinkrate = atoi(strvalue); // String omregnes til et heltal ( int ) // med atoi index = 0; blink(); // Kald sub Kilde: https://www.inkling.com/read/arduino-cookbook-michael-margolis-2nd/chapter-4/recipe-4-3 http://forum.arduino.cc/index.php?topic=150720.0;wap2 Af: Valle Thorø Fil.: Side 9 af 11
http://books.google.dk/books?id=ligw4ihn0ruc&pg=pa104&lpg=pa104&dq=%22einlesen+de r+ziffern+bei+jedem+zeichen,+das+keine+ziffer+ist+(oder+bei+vollem+puffer):%22&source=bl &ots=7o2v_bjbq3&sig=8imxec6cennaepi- BEwSGaX4Tgc&hl=da&sa=X&ei=i1BWVL56hvBojeiA2AI&ved=0CB8Q6AEwAA Serial event!! Skal nok undersøges nærmere! Er det et interrupt, der fyres af?? /* * SerialReceive Sketch * LED mit einer Rate proportional zur empfangenen Ziffer blinken lassen */ const int ledpin = 13; int blinkrate=0; pinmode(ledpin, OUTPUT); blink(); // Pin 13 er forbundet til LED // Blinkrate i denne Variable // Seriel Port sender og modtager med 9600 Baud // Definer Pin som udgang void serialevent() while(serial.available()) char ch = Serial.read(); Serial.write(ch); if( isdigit(ch) ) // ASCII-tegn mellem 0 und 9? blinkrate = (ch - '0'); // ASCII-værdi omregnes til numerisk blinkrate = blinkrate * 100; // Blink-Rate er 100mS * modtaget ciffer void blink() digitalwrite(ledpin,high); // Blink subrutine Af: Valle Thorø Fil.: Side 10 af 11
delay(blinkrate); digitalwrite(ledpin,low); delay(blinkrate); // Delay afhængig af modtaget tal Kilder: https://www.thali.ch/files/shop/documents/100137_leseprobe.pdf https://www.safaribooksonline.com/library/view/arduino-cookbook-2nd/9781449321185/ch04.html Send Data til buffer: Se: https://www.pjrc.com/teensy/td_serial.html Af: Valle Thorø Fil.: Side 11 af 11