# Save data from Arduino to a .csv file

**URL:** https://discourse.processing.org/t/save-data-from-arduino-to-a-csv-file/11827
**Category:** Electronics (Arduino, etc.)
**Created:** [June 4, 2019, 6:18am UTC](https://discourse.processing.org/t/save-data-from-arduino-to-a-csv-file/11827 "2019-06-04T06:18:25Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Arturo18](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@Arturo18](https://discourse.processing.org/u/Arturo18)
#### Post date: [June 4, 2019, 6:18am UTC](https://discourse.processing.org/t/save-data-from-arduino-to-a-csv-file/11827/1 "2019-06-04T06:18:25Z")

</div>

Hello, everyone. I’m trying to save data from an Arduino to a .csv file. I don’t know how to do it, so, couls someone help me? Please? Code down below.

```auto
#define BINS_SIZE 256
#define CALIBRATION_SIZE 50000

#define NO_BIAS_REMOVAL 0
#define EXCLUSIVE_OR 1
#define VON_NEUMANN 2

#define ASCII_BYTE 0
#define BINARY 1
#define ASCII_BOOL 2

/ ***Configure the RNG************** /
int bias_removal = NO_BIAS_REMOVAL;
int output_format = BINARY;
int baud_rate = 19200;
/ ************************************* /

 
unsigned int bins[BINS_SIZE];
int adc_pin = 0;
int led_pin = 13;
boolean initializing = true;
unsigned int calibration_counter = 0;

void setup(){
  pinMode(led_pin, OUTPUT);
  Serial.begin(baud_rate);
  for (int i=0; i < BINS_SIZE; i++){
    bins[i] = 0; 
  }  
}

void loop(){
  byte threshold;

  int adc_value = analogRead(adc_pin);
  byte adc_byte = adc_value >> 2;
  if(calibration_counter >= CALIBRATION_SIZE){
    threshold = findThreshold();
    initializing = false;
  }
  if(initializing){
    calibrate(adc_byte);
    calibration_counter++;
  } else{
    processInput(adc_byte, threshold);
  }
}

void processInput(byte adc_byte, byte threshold){
  boolean input_bool;
  input_bool = (adc_byte < threshold) ? 1 : 0;
  switch(bias_removal){
    case VON_NEUMANN:
      vonNeumann(input_bool); 
      break;
    case EXCLUSIVE_OR:
      exclusiveOr(input_bool);
      break;
    case NO_BIAS_REMOVAL:
      buildByte(input_bool);
      break;
  }
}

void exclusiveOr(byte input){
  static boolean flip_flop = 0;
  flip_flop = !flip_flop;
  buildByte(flip_flop ^ input);
}

void vonNeumann(byte input){
  static int count = 1;
  static boolean previous = 0;
  static boolean flip_flop = 0;
  
  flip_flop = !flip_flop;

  if(flip_flop){
    if(input == 1 && previous == 0){
      buildByte(0);
    }
    else if (input == 0 && previous == 1){
      buildByte(1); 
    }
  }
  previous = input;
}

void buildByte(boolean input){
  static int byte_counter = 0;
  static byte out = 0;

  if (input == 1){
    out = (out << 1) | 0x01;
  }
  else{
    out = (out << 1); 
  }
  byte_counter++;
  byte_counter %= 8;
  if(byte_counter == 0){
    if (output_format == ASCII_BYTE) Serial.println(out, DEC);
    if (output_format == BINARY) Serial.print(out, BIN);
    out = 0;  
  }
  if (output_format == ASCII_BOOL) Serial.print(input, DEC);
}

void calibrate(byte adc_byte){
  bins[adc_byte]++;  
  printStatus();
}

unsigned int findThreshold(){
  unsigned long half;
  unsigned long total = 0;
  int i;

  for(i=0; i < BINS_SIZE; i++){
    total += bins[i];
  }	

  half = total >> 1;
  total = 0;
  for(i=0; i < BINS_SIZE; i++){
    total += bins[i];
    if(total > half){
      break;
    }	
  }
  return i;
}

//Blinks an LED after each 10th of the calibration completes
void printStatus(){
  unsigned int increment = CALIBRATION_SIZE / 10;
  static unsigned int num_increments = 0; //progress units so far
  unsigned int threshold;

  threshold = (num_increments + 1) * increment;
  if(calibration_counter > threshold){
    num_increments++;
    //Serial.print("*");
    blinkLed();
  }   
}

void blinkLed(){
  digitalWrite(led_pin, HIGH);
  delay(30);
  digitalWrite(led_pin, LOW);
}

```

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [June 5, 2019, 10:12am UTC](https://discourse.processing.org/t/save-data-from-arduino-to-a-csv-file/11827/2 "2019-06-05T10:12:10Z")

</div>

that looks like a arduino code ?  
can you please format it using in the forum post editor the

```auto
</> code tag

```

looks like  
```  
type or paste code here  
```

* * *

and you post at the processing forum?  
because you want use processing to catch the send lines by arduino and  
save them to csv file?

-a- get the communication established:

> **[Serial / Libraries](https://processing.org/reference/libraries/serial/index.html)**
>
> Send data between Processing and external hardware through serial communication (RS-232).

-b- make/send lines already in csv style

> 1023,1023,1023,

if you need to understand the lines in processing use the

> **[splitTokens() / Reference](https://processing.org/reference/splitTokens_.html)**
>
> The splitTokens() function splits a String at one or many character delimiters or "tokens". The delim parameter specifies the character or characters to be used as a boundary.

and store the values to a

> **[Table / Reference](https://processing.org/reference/Table.html)**
>
> Table objects store data with multiple rows and columns, much like in a traditional spreadsheet. Tables can be generated from scratch, dynamically, or using data from an existing file. Tables c…

else just use the

> **[saveStrings() / Reference](https://processing.org/reference/saveStrings_.html)**
>
> Writes an array of Strings to a file, one line per String. By default, this file is saved to the sketch's folder. This folder is opened by selecting "Show Sketch Folder" from the "Sketch" menu. \<…
