Show Posts
|
|
Pages: [1] 2 3
|
|
2
|
Using Arduino / Project Guidance / Problem with converting this single line in a library example to Arduino 1.0
|
on: May 19, 2013, 01:35:18 pm
|
Hello, I downloaded and installed a SparkFun library for controlling this camera: https://www.sparkfun.com/products/10061but when I verified the code I got this error report: As of Arduino 1.0, the 'BYTE' keyword is no longer supported. Please use Serial.write() instead.
on the line of code that looks like this(line 73): MemoryCard.file.print(response[i], BYTE);
So I tried this: MemoryCard.file.write(response[i]);
but, that didn't work either as I got this error report: error: overriding 'virtual size_t Print::write(uint8_t)'
So my question is how do I fix that line to make it Arduino 1.0 compatible?
|
|
|
|
|
7
|
Using Arduino / Programming Questions / Re: How to reset the values in a char pointer array
|
on: May 09, 2013, 06:41:37 pm
|
My code is pretty long >130 lines but the problem is the first statement in void loop() so it's pretty easy to find. I really appreciate any help! The code is pretty fun to use, just need an Arduino with SD card and SD card shield. Run the code and open the Serial monitor. Then type in an 8 digit number ID (random numbers will work) and then type in an amount of money, (numbers and terminate the numbers with an 'e', like this --> 123e). It'll create the file and print some stuff. And then it'll ask for another ID again, the problem is when you run through it again it doesn't work, I need the id[] and money[] arrays to be cleared because the code doesn't work with the old values stored there. I hope this makes sense, feel free to run this code, it's really cool! #include <SD.h> #include <string.h> byte SD_CS = 4; //Chip select of SD reader File cust_file; //to call text file char id[50]; //name of text file int id_index = 0; char money[50]; //value in text file int money_index = 0; char money_read[50]; //value read from text file int money_read_index = 0; float valueFloat; //typecasted valueReceive int input; //user input byte id_counter = 0; byte money_counter = 0; float old_money, current_money, new_money; //old is stored, current is entered, new is to write char cl = '\0'; boolean clrd = 0; //only clears memory when data has already been written
void setup(){ Serial.begin(9600); //open serial port Serial.println("Initializing SD Card"); pinMode(SD_CS, OUTPUT); //CS for SD card pinMode(10, OUTPUT); //CS must be an output for SPI to work if(!SD.begin(SD_CS)){ //tests to see if SD card initialization failed Serial.println("SD card initialization failed"); return; } Serial.println("SD card initialization success"); Serial.println("Enter in ID:"); //prompts user to enter in a value to store id[id_index++] = '/'; // Root directory }
void loop(){ while(clrd){ //part that creates the problem! memset(id, '\0', 50); memset(money, '\0', 50); clrd = 0; } get_id(); get_current(); get_old(); get_new(); }
void get_id(){ //gets ID(filename) from user to name file while(Serial.available()){ //read data from Serial port input = Serial.read(); if(input>='0' && input<='9'){ id[id_index++] = input; } strcpy(id+id_index, ".txt"); //converts to pointer array id_counter++; } if(id_counter>=8){ //if filename complete Serial.print("ID Entered is: "); //print id entered to Serial Serial.println(id); Serial.println("Enter in money:"); //prompt user to enter in money } else{ get_id(); //otherwise go back to function } }
void get_current(){ //gets current input from user id_counter = 0; //prevents code from repeating itself over and over. while(Serial.available()){ //reads data from serial port and save in pointer array input = Serial.read(); if(input>='0' && input<='9'){ money[money_index++] = input; //saves user input to money index } else if(input=='e'){ money_counter = 1; } } if(money_counter==1){ strcpy(money+money_index, ""); //converts to pointer array current_money = atof(money); //current_money is saved from money pointer array after being converted to float Serial.print("Money entered is: "); //print money entered to Serial Serial.println(current_money); Serial.println(" "); //blank line } else{ get_current(); } }
void get_old(){ //gets old value from file if exists, if file doesn't exist create one first money_counter = 0; //prevents code from repeating itself over and over. cust_file = SD.open(id, FILE_READ); //open file at beginning if(!cust_file){ //if file not already present, create one cust_file = SD.open(id, FILE_WRITE); //create file Serial.print("Created file: "); //prints new file name Serial.println(id); Serial.println(" "); } if(cust_file){ //if file is available while(cust_file.available()){ for(byte c=0; c<50; c++){ money_read[money_read_index++] = cust_file.read(); //store file contents } } old_money = atof(money_read); //stores the array as a float in old_money Serial.print("File available, old money stored is: "); //prints old money stored Serial.print(old_money); //in file to Serial Serial.println(old_money); //print old_money Serial.println(" "); //blank line cust_file.close(); //close file } }
void get_new(){ //get new_money by computing current_money and old_money new_money = current_money + old_money; if(new_money>=100.0){ //if new_money greater than 100 than print coupon and subtract 100 new_money -= 100.0; Serial.println("You have a $5 discount!"); Serial.print("You have $"); Serial.print(new_money); Serial.println(" left."); Serial.println(" "); } else if(new_money<100.0){ //if new_money less than 100 print new_money Serial.print("You have $"); Serial.print(new_money); Serial.println(" now."); Serial.println(" "); } cust_file = SD.open(id, FILE_WRITE); cust_file.print(new_money); cust_file.close(); Serial.println("File written."); //shows user that file has been successfully written Serial.println(" "); //prints blank line Serial.println("Enter ID:"); //prompts user to enter next ID clrd = 1; //lets the data in id and money arrays clear loop(); }
|
|
|
|
|
10
|
Using Arduino / Programming Questions / How to reset the values in a char pointer array
|
on: May 09, 2013, 06:13:13 pm
|
Hello, in my code I need to reset a char pointer array while the Arduino is running (ex. char id[50]; ), after I fill it with data. I can't wait to reset it or relaunch it. I've tried the various methods listed below but none of them would work. I tried memset memset(id, '\0', 50); but that didn't work. I tried creating another char[]; and copying its blank contents into the original char char idc[50]; char id[50]; //is filled with data memcpy(id, idc, 50); but that also didn't work. I even tried the simple: char idc[50]; char id[50]; //is filled with data
id = idc
But of course that wouldn't work (I was desperate). So I must ask does anyone know how to reset a char pointer array while the Arduino is running without having to reset it? Thanks, any help would be greatly appreciated!
|
|
|
|
|
12
|
Using Arduino / Storage / Converting a float to a char* for writing to SD card
|
on: May 08, 2013, 06:19:44 pm
|
Hello so I'm trying to convert a float to a char* to write to an SD card. Here's a simplified example. The problem is that it won't print the float to the SD card, instead it prints two question marks. float new_money = 1.0; char new_money_char[10]; File cust_file;
void setup(){ Serial.begin(9600); sprintf(new_money_char, "%f", new_money); cust_file = SD.open("file.txt", FILE_WRITE); cust_file.write(new_money_char); cust_file.close(); }
void loop(){ }
Any help would be appreciated, thank you!
|
|
|
|
|
15
|
Using Arduino / Storage / Need help to input variables to SD card built in function.
|
on: May 04, 2013, 04:46:56 pm
|
Hello, I need to dynamically change the directory or file name in the SD.open() [ex. SD.open("/0/file.txt") ] function. I tried String typecasting into the function but I get an error. I need to dynamically change which directory I'm opening based on an externally inputted variable while the Arduino is running in the wild. For example, if the number I get in the wild is 1,2,3 I would need a way to do this SD.open(1/2/3); without having to hard code the "1/2/3" argument in the function. I tried typecasting the variables as a String, char, and char* but none of these worked and produced errors. If you know of a way to dynamically change the directory on the fly then that would be very much appreciated! Thanks, here's my code. #include <SD.h> byte SD_CS = 4; File test_file; void setup(){ Serial.begin(9600); //open serial port Serial.println("Initializing SD Card"); pinMode(SD_CS, OUTPUT); //CS for SD card pinMode(10, OUTPUT); //CS must be an output for SPI to work if(!SD.begin(SD_CS)){ Serial.println("SD card initialization failed"); return; } Serial.println("SD card ready"); test_file = SD.open(String(/0/test.txt), FILE_WRITE); //this line causes problems }
void loop(){ }
|
|
|
|
|