Show Posts
|
|
Pages: 1 2 3 [4]
|
|
49
|
Using Arduino / Programming Questions / Re: Save a string came from Serial
|
on: January 03, 2013, 09:52:34 am
|
Oh god, I didn't see  That's work but it's not what I wan't... But I think the last code I have posted is good but I have a just a problem with my array or with string. I was testing it on Gobetwino when I see "is not a well formed command string" for all I wrote on my program... Can you help me ?
|
|
|
|
|
51
|
Using Arduino / Programming Questions / Re: Save a string came from Serial
|
on: January 03, 2013, 08:10:57 am
|
// this constant won't change: const int buttonPin = 2; // the pin that the pushbutton is attached to const int ledPin = 13; // the pin that the LED is attached to const int buttonPin2 = 3; // second button to know the numbers of everydays const int ledPin2 = 8;
// Variables will change: int buttonPushCounter = 0; // counter for the number of button presses int buttonState = 0; // current state of the button int buttonState2 = 0; int lastButtonState = 0; // previous state of the button int lastButtonState2 = 0;
double time = 0; // will contain the hour
int numberSaving = 0; //permite to know if the number have been already saved
int i; //to explore the array
//Array which will contain de number of the push by day int day = 0; //not one because the programm will automatically increment float counterByDay[] = {}; //this array will begin on 1, not on 0
//Prepare control by console int numberWanted; char readString; boolean stringComplete = false; int charAvailable;
void setup() { // initialize the button pin as a input: pinMode(buttonPin, INPUT); pinMode(buttonPin2, INPUT); // initialize the LED as an output: pinMode(ledPin, OUTPUT); pinMode(ledPin2, OUTPUT); // initialize serial communication: Serial.begin(9600); // little home message Serial.println("------COMPTEUR D'IMPULSIONS-------"); Serial.println("Nb: appuyer sur le bouton pour obternir les resultats des derniers jours."); Serial.println(""); }
void loop() { // read the pushbutton input pin: buttonState = digitalRead(buttonPin); // turns on the LED when button pushed digitalWrite(ledPin, buttonState); // compare the buttonState to its previous state if ((buttonState != lastButtonState) && (millis() > 3000)) //"millis() > 3000" permite to avoid the inconvenience of starting { // if the state has changed, increment the counter if (buttonState == HIGH) { // if the current state is HIGH then the button // wend from off to on: buttonPushCounter++; Serial.print("Compteur: "); Serial.println(buttonPushCounter); Serial.println(""); } else { // if the current state is LOW then the button // wend from on to off: Serial.println("+1"); } } // save the current state as the last state, //for next time through the loop lastButtonState = buttonState; //actualise time time = millis(); //preparing time to verify that is a multiple of one day time = time/(round(time / 15000)); //if time = one day, save the number if ((time >= 15000) && (time <= 15100) && (numberSaving == 0) && (millis() > 3000)) //REAL: if(time >= 86400000 && time <= 86402000) { //operations to save the number with the date in an array: day++; counterByDay[day] = buttonPushCounter; //initialise counter buttonPushCounter = 0; //write it on the sreen Serial.println(""); Serial.println("-----"); Serial.print("Jour "); Serial.print(day); Serial.print(", nombre de basculement: "); Serial.println(round(counterByDay[day])); Serial.println("-----"); Serial.println(""); numberSaving = 1; } //If the number have been saved, and if won't be save a second time, we initialise numberSaving else if (((time < 15000) || (time > 15100)) && (numberSaving == 1)) { numberSaving = 0; } //This part must show all the array when button 2 is pressed // read the pushbutton2 input pin: buttonState2 = digitalRead(buttonPin2); // turns on the LED 2 when button pushed digitalWrite(ledPin2, buttonState2); // compare the buttonState2 to its previous state if ((buttonState2 != lastButtonState2) && (millis() > 3000)) { // if the state has changed, if (buttonState2 == HIGH) { // if the current state is HIGH then the button // wend from off to on //Here the programm personnalise the announcement for 3 category : the first day, the second, more than 2 Serial.println(""); Serial.println("-----"); if (day == 0) { Serial.print("Compteur du premier jour (jour actuel): "); Serial.print(buttonPushCounter); Serial.println(""); } else if (day == 1) { Serial.println("Compteur d'hier et d'aujourd'hui : "); Serial.print("- Hier : "); Serial.println(round(counterByDay[1])); Serial.print("- Depuis ce matin : "); Serial.println(buttonPushCounter); } else //this explains that there are a lot of day { Serial.print("Compteur "); Serial.print(day); Serial.println(" derniers jours: "); //listing all days with an array for (i = 1; i < (day+1); i = i + 1) { Serial.print("- Jour "); Serial.print(i); Serial.print(" : "); Serial.println(round(counterByDay[i])); //round could be delete } Serial.print("- Depuis ce matin : "); Serial.println(buttonPushCounter); } Serial.println("-----"); Serial.println(""); } } // save the current state as the last state, //for next time through the loop lastButtonState2 = buttonState2; if (stringComplete) { //if readString is a number, show the counter of this day Serial.println(""); Serial.println("-----"); if (isDigit(readString)) { numberWanted = (readString - '0'); Serial.print("Le nombre de basculement du jour "); Serial.print(numberWanted); Serial.print(" est : "); Serial.println(round(counterByDay[numberWanted])); } else //command unknow { Serial.println("Ceci n'est as un nombre."); } Serial.println("-----"); Serial.println(""); //inizialise string; readString = 0; stringComplete = false; } }
//Now, what prog must do if commands are sent void serialEvent() { //creation of the string while (Serial.available()) { delay(10); char c = Serial.read(); readString += c; charAvailable = Serial.available(); if (charAvailable == 0) { stringComplete = true; } } }
I'm a new user and I must learn quickly because I'm working on a project baccalaureate, sorry if I made strange mistakes 
|
|
|
|
|
52
|
Using Arduino / Programming Questions / Re: Save a string came from Serial
|
on: January 03, 2013, 07:53:41 am
|
Finally, I reorganized my code as in the example of the doc, it seems cleaner: //Prepare control by console int numberWanted; char readString; boolean stringComplete = false; int charAvailable;
void setup() { Serial.begin(9600); }
void loop() { [......] if (stringComplete) { //if readString is a number, show the counter of this day if (isDigit(readString)) { numberWanted = (readString - '0'); Serial.print("Le nombre de basculement du jour "); Serial.print(numberWanted); Serial.print(" est : "); Serial.println(round(counterByDay[numberWanted])); } else //command unknow { Serial.println("Ceci n'est as un nombre."); } //inizialise string; readString = 0; stringComplete = false; } }
//Now, what prog must do if commands are sent void serialEvent() { //creation of the string while (Serial.available()) { delay(10); char c = Serial.read(); readString += c; charAvailable = Serial.available(); if (charAvailable == 0) { stringComplete = true; } } } I show the trick "numberWanted = (readString - '0');" to convert readString in a number. That's not possible ? Also, I tried charAvailable = Serial.available(); if (c == \0) { stringComplete = true; } but it didn't work, why ?
|
|
|
|
|
53
|
Using Arduino / Programming Questions / Re: Save a string came from Serial
|
on: January 03, 2013, 06:44:24 am
|
I took the code this morning, here is what I did : void serialEvent() {
while (Serial.available()) { delay(10); char c = Serial.read(); readString += c; } Serial.println(""); Serial.println("-----"); if (isDigit(readString)) { numberWanted = (readString - '0'); Serial.print("Pushes number of the day "); Serial.print(numberWanted); Serial.print(" is : "); Serial.println(round(counterByDay[numberWanted])); } else { Serial.println("This is not a number."); } Serial.println("-----"); Serial.println(""); readString = 0; } But now, I realize that I have errors in the rest of the program, and I doubt that it comes from this last piece. Every day I record the number of pulses and I stock it all in a table, here is my method of storage: //actualise time time = millis(); //preparing time to verify that is a multiple of one day, a day = 15000 ms (to test time = time/(round(time / 15000)); //if time = one day, save the number if ((time >= 15000) && (time <= 15100) && (numberSaving == 0) && (millis() > 3000)) { //operations to save the number with the date in an array: day++; counterByDay[day] = buttonPushCounter; //initialise counter buttonPushCounter = 0; numberSaving = 1; } //If the number have been saved, and if won't be save a second time, we initialise numberSaving else if (((time < 15000) || (time > 15100)) && (numberSaving == 1)) { numberSaving = 0; }
If I don't have problem on that 2 parts, I'll show you how I write on the screen. See you and thank you 
|
|
|
|
|
55
|
Using Arduino / Programming Questions / Re: Save a string came from Serial
|
on: January 02, 2013, 06:40:05 pm
|
|
I don't really know how string works, it's true. But this mistakes comes from my copy / paste.
I add char* message;
but I don't know how I can correct message = message, messageString;
it is message = message + messageString;
or message = message . messageString;
?
|
|
|
|
|
56
|
Using Arduino / Programming Questions / Save a string came from Serial
|
on: January 02, 2013, 06:17:38 pm
|
Hello ! I'm a new french Arduino adept therefore please forget my mistakes  I'm working for a system which will be control by Serial commands. I must retrieve the command that I entered on the console. Example : I write "on" and Arduino light the led. But it is a real headache... That's what I've done and that doesn't work: int character_available = 0; char character;
int i = 0;
void setup() { Serial.begin(9600); } void loop() { }
void serialEvent() //declaration of interrupt function on the serial port { character_available = Serial.available(); // variable containing the number of characters available in the buffer char messageString[character_available]; //array will contain this message i = 0; while(character_available > 0) // so long there are characters to read { character = Serial.read(); messageString[i] = character; character_available = Serial.available(); i++; message = message, messageString[i]; //this must register the string and if we do Serial.print(message); it show "Hello" for example } }
It's getting later in France. Maybe I made a mistake beast... But thanks you and see you soon 
|
|
|
|
|