Show Posts
Pages: [1]
1  Using Arduino / Programming Questions / Re: Trying to reduce serial.print code on: June 26, 2012, 09:58:54 am
Code:
char tempStr[80];

sprintf(tempStr,"#S|THRMLOG|[%lu;%d;%d;%d;%d;%d;%d;%d;%d;%d;]#",
  tTest,Vin0,Vin1,Vin2,Vin3,Vin4,Vin5,Vin6,Vin7,Vin8);
Serial.println(tempStr);

That is assuming VinX are ints.  For float values it gets somewhat more complex as the sprintf code doesn't handle floats.  For that you will have to do some majoc using dtostrf() and more strings.

Okay thats great that helps me understand sprintf better.

Also its pretty clear that I need to start getting used to using arrays rather than declaring each variable separately.
2  Using Arduino / Programming Questions / Trying to reduce serial.print code on: June 26, 2012, 09:11:58 am
Hey guys, I am just wondering if there is any way to reduce this code and make it two or three lines. The vars without quotations have values assigned to them.  I need to print a space or a semi colon between the values for ease of exporting into excell. I have tried   Serial.print((tTest)";"(Vin0)";"(Vin1)";"(Vin2)";"(Vin3)";"(Vin4)";"); but it says expected ')' before string constant.

Code:
Serial.print("#S|THRMLOG|[");
    tTest = millis() - tStart;
    Serial.print(tTest);
    Serial.print(";");
    Serial.print(Vin0);
    Serial.print(";");
    Serial.print(Vin1);
    Serial.print(";");
    Serial.print(Vin2);
    Serial.print(";");
    Serial.print(Vin3);
    Serial.print(";");
    Serial.print(Vin4);
    Serial.print(";");
    Serial.print(Vin5);
    Serial.print(";");
    Serial.print(Vin6));
    Serial.print(";");
    Serial.print(Vin7);
    Serial.print(";");
    Serial.print(Vin8);
    Serial.print(";");
    Serial.println("]#");

SOLVED:Thank you again for all of the input.

my new code works great:

Code:
#include <math.h>
int TTLPin = 51;
long tTest = 0;
long tStart;
int myPins[] = {A0, A1, A2, A3, A4, A5, A6, A7, A8};
int myVin[9];

void setup()
{
  Serial.begin(9600);
  pinMode(TTLPin, INPUT);
 
}
void loop(){
 
  Serial.println("#S|CPBLTST|[]#");
  Serial.println("#S|THRMLOG|[Time(ms);Vin0;Vin1;Vin2;Vin3;Vin4;Vin5;Vin6;Vin7;Vin8]#");
  //while(digitalRead(TTLPin) == LOW){}
  tStart = millis();
  while(true){
         
    int i;
    for (i=0; i<9; i++){   
         myVin[i] = analogRead(myPins[i]);
    }
   
    Serial.print("#S|THRMLOG|[");
    tTest = millis() - tStart;
    Serial.print(tTest);
    Serial.print(";");
   
    int j;
    for(j=0; j<9; j++){
       Serial.print(myVin[j]);
       Serial.print(";");
    }
    Serial.println("]#"); 
   
    delay(1000);
  }
   
}




3  Using Arduino / Programming Questions / Re: millis starts counting in negative numbers on: June 25, 2012, 03:10:23 pm
Quote
Here is my code:
sp. "Here is a fragment of my code from which I have omitted much useful information"

My money is on "int"s too, but posting all the code in the first place would have saved the guess work.

My bad for omitting the code I just didnt want to put a whole bunch of stuff for no reason and look like a newb, but obviosly that backfired haha. You are partially correct on the ints. But the itoa function maxes out at 16 bits as well.
4  Using Arduino / Programming Questions / millis starts counting in negative numbers on: June 25, 2012, 02:28:26 pm
Hey guys i am using the millis() functiuon to create a timestamp, then keep track of time using "time = millis - timestamp" logic. Here is my code:

void loop(){
 
  Serial.println("#S|CPBLTST|[]#");
  Serial.println("#S|THRMLOG|[Time(ms);Vin0;Vin1;Vin2;Vin3;Vin4;Vin5;Vin6;Vin7;Vin8]#");
  tStart = millis();
  while(true){
         
    Vin0 = analogRead(v0Pin);    Vin1 = analogRead(v1Pin);    Vin2 = analogRead(v2Pin);   
    Vin3 = analogRead(v3Pin);    Vin4 = analogRead(v4Pin);    Vin5 = analogRead(v5Pin);   
    Vin6 = analogRead(v6Pin);    Vin7 = analogRead(v7Pin);    Vin8 = analogRead(v8Pin);
   
    char buffer[10];
       
    Serial.print("#S|THRMLOG|[");
    tTest = millis() - tStart;
    Serial.print(itoa((tTest), buffer, 10));
    Serial.print(";");
    Serial.print(itoa((Vin0), buffer, 10));
    Serial.print(";");
    Serial.print(itoa((Vin1), buffer, 10));
    Serial.print(";");
    Serial.print(itoa((Vin2), buffer, 10));
    Serial.print(";");
    Serial.print(itoa((Vin3), buffer, 10));
    Serial.print(";");
    Serial.print(itoa((Vin4), buffer, 10));
    Serial.print(";");
    Serial.println("]#");
   
    delay(1000);
  }
}

Here are the consecutive readouts approximately where things start to go wrong:

30255;901;898;903;901;902;
31257;901;898;903;902;902;
32258;901;898;903;903;902;
-32276;901;897;903;903;903;

-31274;902;898;903;901;902;
-30273;901;898;903;902;902;

It will loop all the way back to 31258 then go to -32276. Anyone know the reason this may be happening?

EDIT: I solved this by declaring tTest as a long rather than int. However, that is not the only thing I had to do, I also had to eliminate the itoa function, and I got lucky because for some reasonb gobetwino doesnt require the number to be converted to a string, although it says to in the user manual.
5  Using Arduino / Microcontrollers / I think I might have broken the External Referance Arduino Mega on: June 22, 2012, 08:36:27 am
Hey guys, I turned on my power supply this morning and for some reason it was set to 17 v I only had it on for a few seconds but now I dont think the external reference is working. it could be a problem with my circuit but I am just wondering if i possibly friend the Aref pin. Also even if I did ruin the Aref pin will the internal referances still have functionality?

Also about 16V went into the A0 pin. COuld that be damaged too? Only 3mA of current was going through the circuit.
6  Using Arduino / Programming Questions / Re: Creating a reference time on: June 19, 2012, 01:56:42 pm
Yeah I was thinking like a while(1 == 1){code} would do the trick. This may be stupid but could I just write while(true){code}? smiley-yell
7  Using Arduino / Programming Questions / Re: Creating a reference time on: June 19, 2012, 01:13:49 pm
Thank you for your reply. I want the timer to start as soon as the digital pin goes high, and I never want it to look for the status of that pin again. Im thinking i might need another void loop or something but I am not quite sure what the distinct purpose of a void loop is.
8  Using Arduino / Programming Questions / Creating a reference time on: June 19, 2012, 12:34:42 pm
Hello everybody,

I am using the arduino to take sensor readings from a series of 9 thermistors. I want to use a time that each reading was (approximately) taken at and Im not quite sure the best way to use the millis or icros function to do it. Logically I want to:

1 look for a digital pin to go high (do nothing until this happens)
2 start a timer
3 carry out a code until i hit the reset button

the problem is that i am using a ttl signal that is on for 5ms and off for 2 ms (or something like that) I dont want the code to ever be able to go back into that while loop where the TTL might be  low. heres my code just incase you want to see what i have done so far.

void setup()
{
  Serial.begin(9600);
  pinMode(TTLPin, INPUT);
  Serial.println("#S|CPBLTST|[]#");
  Serial.println("#S|THRMLOG|[Time;Vin0;Vin1;Vin2;Vin3;Vin4;Vin5;Vin6;Vin7;Vin8]#");
  int tStart = micros();
  }

void loop(){
  while(TTLPin== LOW){
  }
 
         
    Vin0 = analogRead(v0Pin);    Vin1 = analogRead(v1Pin);    Vin2 = analogRead(v2Pin);   
    Vin3 = analogRead(v3Pin);    Vin4 = analogRead(v4Pin);    Vin5 = analogRead(v5Pin);   
    Vin6 = analogRead(v6Pin);    Vin7 = analogRead(v7Pin);    Vin8 = analogRead(v8Pin);
       
    char buffer[7];
       
    Serial.print("#S|THRMLOG|[");
    tTest = micros() - tStart;
    Serial.print(itoa((tTest), buffer, 10));
    Serial.print(";");
    Serial.print(itoa((Vin0), buffer, 10));
    Serial.print(";");
    Serial.print(itoa((Vin1), buffer, 10));
    Serial.print(";");
    Serial.print(itoa((Vin2), buffer, 10));
    Serial.print(";");
    Serial.print(itoa((Vin3), buffer, 10));
    Serial.print(";");
    Serial.print(itoa((Vin4), buffer, 10));
    Serial.print(";");
    Serial.print(itoa((Vin5), buffer, 10));
    Serial.print(";");
    Serial.print(itoa((Vin6), buffer, 10));
    Serial.print(";");
    Serial.print(itoa((Vin7), buffer, 10));
    Serial.print(";");
    Serial.print(itoa((Vin8), buffer, 10));
    Serial.print(";");
    Serial.println("]#");
   


9  Using Arduino / Programming Questions / Serial question on: June 04, 2012, 12:04:36 pm
So I am going to use Gobetwino to store readings from the analog pins in a text file. to get the readings to be processed by gobetwino i have to conver the numbers to string. Here is an example of how to do it if your numbers are up to 4 digits

char buffer[5];
 
   Serial.print("#S|LOGTEST|[");
   Serial.print(itoa((value1), buffer, 10));
   Serial.print(";");

So i understand the char buffer is 5 because the analog read returns a value of up to 4 digits (between 1 and 1024) so you need one extra for the /0 or whatever. But I am also going to try to store the time of each reading in the text file by using the millis() function. So in the beggining the millis will need a buffer of 2 and by the end it will need a buffer of 7. can I just change the buffer to 7 and everything will just work out. and what does the 10 after buffer mean.
Pages: [1]