Having trouble saving data from the arduino into a csv file, using the below link walkthrough;
I can get the software running as shown in the image in the attachment, but I can't get a working result from the code that the link says to put into arduino, shown below. I put it under all my code to acquire the data in the first place, and it doesn't like the void functions or says that the serial doesn't name a type. Am I putting the code in the wrong place? int row_excel = 0; // number of lines int test = 123; //test variable to be passed to Excel int test_2 = 456; // the second test variable that will be passed to excel
void setup(){
Serial.begin(9600); //transfer speed
Serial.println("CLEARDATA"); // clear excel sheet
Serial.println("LABEL,Time,Test 1, Test 2, Num Rows"); // column headers
}
void loop(){
row_excel++; // row number + 1
Serial.print("DATA,TIME,"); // excel record current date and time
Serial.print(test);
Serial.print(",");
Serial.print(test_2);
Serial.print(",");
Serial.println(row_excel);
// if rows are more than 50, then start filling the rows again if (row_excel > 50){
row_excel = 0;
Serial.println("ROW,SET,2");
}
delay(1000); //delay
}
#include "TimerOne.h" // Timer Interrupt set to 2 second for read sensors
#include <math.h>
#define WindSensorPin (2) // The pin location of the anemometer sensor
#define WindVanePin (A4) // The pin the wind vane sensor is connected to
#define VaneOffset 0; // define the anemometer offset from magnetic north
int VaneValue; // raw analog value from wind vane
int Direction; // translated 0 - 360 direction
int CalDirection; // converted value with offset applied
int LastValue; // last direction value
volatile bool IsSampleRequired; // this is set true every 2.5s. Get wind speed
volatile unsigned int TimerCount; // used to determine 2.5sec timer count
volatile unsigned long Rotations; // cup rotation counter used in interrupt routine
volatile unsigned long ContactBounceTime; // Timer to avoid contact bounce in isr
float WindSpeed; // speed miles per hour
void setup() {
LastValue = 0;
IsSampleRequired = false;
TimerCount = 0;
Rotations = 0; // Set Rotations to 0 ready for calculations
Serial.begin(9600);
pinMode(WindSensorPin, INPUT);
attachInterrupt(digitalPinToInterrupt(WindSensorPin), isr_rotation, FALLING);
Serial.println("Davis Anemometer Test");
Serial.println("Speed (MPH)\tDirection\tStrength");
// Setup the timer interupt
Timer1.initialize(500000);// Timer interrupt every 2.5 seconds
Timer1.attachInterrupt(isr_timer);
}
void loop() {
getWindDirection();
// Only update the display if change greater than 5 degrees.
if(abs(CalDirection - LastValue) > 5) {
LastValue = CalDirection;
}
if(IsSampleRequired) {
// convert to mp/h using the formula V=P(2.25/T)
// V = P(2.25/2.5) = P * 0.9
WindSpeed = Rotations * 0.9;
Rotations = 0; // Reset count for next sample
IsSampleRequired = false;
Serial.print(WindSpeed); Serial.print("\t\t");
Serial.print(CalDirection);
getHeading(CalDirection); Serial.print("\t\t");
getWindStrength(WindSpeed);
}
}
// isr handler for timer interrupt
void isr_timer() {
TimerCount++;
if(TimerCount == 6)
{
IsSampleRequired = true;
TimerCount = 0;
}
}
// This is the function that the interrupt calls to increment the rotation count
void isr_rotation() {
if((millis() - ContactBounceTime) > 15 ) { // debounce the switch contact.
Rotations++;
ContactBounceTime = millis();
}
}
// Convert MPH to Knots
float getKnots(float speed) {
return speed * 0.868976;
}
// Get Wind Direction
void getWindDirection() {
VaneValue = analogRead(WindVanePin);
Direction = map(VaneValue, 0, 1023, 0, 359);
CalDirection = Direction + VaneOffset;
if(CalDirection > 360)
CalDirection = CalDirection - 360;
if(CalDirection < 0)
CalDirection = CalDirection + 360;
}
// Converts compass direction to heading
void getHeading(int direction) {
if(direction < 22)
Serial.print(" N");
else if (direction < 67)
Serial.print(" NE");
else if (direction < 112)
Serial.print(" E");
else if (direction < 157)
Serial.print(" SE");
else if (direction < 212)
Serial.print(" S");
else if (direction < 247)
Serial.print(" SW");
else if (direction < 292)
Serial.print(" W");
else if (direction < 337)
Serial.print(" NW");
else
Serial.print(" N");
}
// converts wind speed to wind strength
void getWindStrength(float speed) {
if(speed < 2)
Serial.println("Calm");
else if(speed >= 2 && speed < 4)
Serial.println("Light Air");
else if(speed >= 4 && speed < 8)
Serial.println("Light Breeze");
else if(speed >= 8 && speed < 13)
Serial.println("Gentle Breeze");
else if(speed >= 13 && speed < 18)
Serial.println("Moderate Breeze");
else if(speed >= 18 && speed < 25)
Serial.println("Fresh Breeze");
else if(speed >= 25 && speed < 31)
Serial.println("Strong Breeze");
else if(speed >= 31 && speed < 39)
Serial.println("Near Gale");
else
Serial.println("RUN");
}
int row_excel = 0; // number of lines
int test = 123; //test variable to be passed to Excel
int test_2 = 456; // the second test variable that will be passed to excel
void setup(){
Serial.begin(9600); //transfer speed
Serial.println("CLEARDATA"); // clear excel sheet
Serial.println("LABEL,Time,Test 1, Test 2, Num Rows"); // column headers
}
void loop(){
row_excel++; // row number + 1
Serial.print("DATA,TIME,"); // excel record current date and time
Serial.print(test);
Serial.print(",");
Serial.print(test_2);
Serial.print(",");
Serial.println(row_excel);
// if rows are more than 50, then start filling the rows again
if (row_excel > 50){
row_excel = 0;
Serial.println("ROW,SET,2");
}
delay(1000); //delay
}
Sorry, there we go, I've entered the entire sketch! Here's the first error;
exit status 1
redefinition of 'void setup()'
I'm sure this is because void setup is already at the top, I'm just not sure where to put the bottom section of code starting with int row_excel = 0.
I'm also sure the basic setup works since if that code isn't included it produces values fine.
Yeah I think the problem is I'm quite new to the software so I'm not sure how to merge it? Could you point me in the right direction on how to do that?
It sounds like you are trying to run before learning to walk.
The best way to merge code is to first go through and understand what each line of each program does (and why it is important), then write one program that accomplishes what you want.
It is often, but not always safe to cut and paste blocks of code, as long as you fully understand the individual blocks.
What do you mean with " I put it under all my code to acquire the data in the first place, and it doesn't like the void functions or says that the serial doesn't name a type. Am I putting the code in the wrong place? " That looks confusing!
with the statement "int row_exel" starts a complete Arduino sketch/program. That should not be mixed with other things.
As said above: You must learm the basics of Arduino, before you may solve a data transfer to Excel!