Advertisement
Guest User

Untitled

a guest
Jan 19th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. /*
  2. Sending Data to Processing via the Serial Port
  3. This sketch provides a basic framework to send data from Arduino to Processing over a Serial Port. This is a beginning level sketch.
  4.  
  5. Hardware:
  6. * Sensors connected to Arduino input pins
  7. * Arduino connected to computer via USB cord
  8.  
  9. Software:
  10. *Arduino programmer
  11. *Processing (download the Processing software here: https://www.processing.org/download/
  12.  
  13. Additional Libraries:
  14. *Read about the Software Serial library here: http://arduino.cc/en/Reference/softwareSerial
  15.  
  16. Created 12 November 2014
  17. By Elaine Laguerta
  18. http://url/of/online/tutorial.cc
  19. */
  20.  
  21. /*To avoid overloading the Arduino memory, and to encourage portability to smaller microprocessors, this sketch
  22. does not timestamp or transform data. In this tutorial, timestamping data is handled on the processing side.
  23.  
  24. Whether you process data on the Arduino side is up to you. Given memory limitations of the Arduino, even a few computations and mapping of values can
  25. max out the memory and fail. I recommend doing as little as possible on the Arduino board.*/
  26.  
  27. //#include SoftwareSerial.h
  28.  
  29. /*Declare your sensor pins as variables. I'm using Analog In pins 0 and 1. Change the names and numbers as needed
  30. Pro tip: If you're pressed for memory, use #define to declare your sensor pins without using any memory. Just be careful that your pin name shows up NOWHERE ELSE in your sketch!
  31. for more info, see: http://arduino.cc/en/Reference/Define
  32. */
  33. int sensor1Pin = 5;
  34.  
  35.  
  36. /*Create an array to store sensor values. I'm using floats. Floats use 4 bytes to represent numbers in exponential notation. Use int if you are representing whole numbers from -32,768 to 32,767.
  37. For more info on the appropriate data type for your sensor values, check out the language reference on data type: http://arduino.cc/en/Reference/HomePage
  38. Customize the array's size to be equal to your number of sensors.
  39. */
  40. /*
  41.  * Prueba de dejar esto afuera
  42.  float sensorVals[] = {0,0,0}
  43. */
  44. /*Pro tip: if you have a larger number of sensors, you can use a for loop to initialize your sensor value array. Here's sample code (assuming you have 6 sensor values):
  45. float sensorVals[6];
  46. int i;
  47. for (i=0; i<6; i++)
  48. {
  49. sensorVals[i] = 0;
  50. }
  51. */
  52.  
  53. void setup(){
  54. Serial.begin(9600); //This line tells the Serial port to begin communicating at 9600 bauds
  55. }
  56.  
  57. //
  58. void loop(){
  59. //read each sensor value. We are assuming analog values. Customize as nessary to read all of your sensor values into the array. Remember to replace "sensor1Pin" and "sensor2Pin" with your actual pin names from above!
  60. int sensorVal = digitalRead(sensor1Pin);
  61.  
  62. /*If you are reading digital values, use digitalRead() instead. Here's an example:
  63. sensorVal[0] = digitalRead(sensor1Pin);
  64. */
  65.  
  66. //print over the serial line to send to Processing. To work with the processisng sketch we provide, follow this easy convention: separate each sensor value with a comma, and separate each cycle of loop with a newline character.
  67. //Remember to separate each sensor value with a comma. Print every value and comma using Serial.print(), except the last sensor value. For the last sensor value, use Serial.println()
  68. //Serial.print(sensors[0]);
  69. //Serial.print(",");
  70. //Serial.print(sensors[1]);
  71. //Serial.print(",");
  72. Serial.println(sensorVal);
  73. delay(1000);
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement