Pages: [1]   Go Down
Author Topic: SOLVED - Can't use server.print. Can't figure out server.write- I'm a month deep  (Read 751 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Please bear with me, I've injured my wrist and I am dictating this post on my iPhone. If you see any typos please excuse them. This is also the reason why I didn't post my entire code, I don't think it's really relevant anyways.

So here is what's going on. I got this really cool app for my iPhone called Net IO. Among other things, it has fields that we'll send a user-defined string to the Arduino and display the response from the Ardolino. I was using print statements, but they would display random and garbled. After many weeks of pestering and bugging the developer, who is a very nice guy by the way, talking in to logging into my Ardolino and testing out my sketch. Should also mention that he didn't develop this app strictly for Arduino, it can communicate with a computer, an AVR board- whatever -anything that can receive strings and send responses. After he logged in, he asked me to upload an example sketch. I saw that it used a server.write function, so I tried that in my sketch. It actually worked, everything displayed where it should. However, it wasn't my entire sketch it was simply test strings within the write function. For example I had server.write(val). This works fine.... please keep reading.

My problem is this, I need to be able to take a variable such as one set equal to a mapped out analog reading, and write it followed by space and a percentage sign.

For example:
val = analogRead(A5);
lightper = map(val, 0, 1023, 0, 100);
server.write(????);

I was using server.print(lightper + " %") but unfortunately the app doesn't accept that. It's confusing and unfirtunately nothing that I can solve because I can't reprogram the iphone application.

I really don't see how the rest my code could be relevant but I'll post it later if anybody wants. It's just hard to do anything on the computer other than dictate because of my wrist.

Any thoughts? Thank you,
Joey
« Last Edit: April 28, 2012, 06:54:17 pm by JoeyPhxAZ » Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Look at the function:  sprintf()
http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/

See the example near the bottom.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset





Please forgive me further. These pain pills they've got me on have me feeling like a hippie at woodstock basking in the Sunshinesmiley-razz

I am new to Ardolino and programming as a whole for that matter so this is not coming to me very easily. My intoxicated state doesn't make things any easier. LOL. I've read that page a few times now and I'm just not quite getting it. Could I trouble you to give an example of how it would be implemented for my purposes. Providing you are proficient with the use of this function of course; I wouldn't ask anybody to sit and learn something just to make it easier for me to learn it.

If it helps at all, I'll be trying to do the same thing with a temperature. That is, formatting an analog read and then appending space F to the end.

Your help is very much appreciated. In my injured state I don't have much to do, so I'm using my time to work on my project. Almost makes it worth it, hardly. But it is nice to have time to work on my Arduino project.

Joey
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The easiest thing would be to have two Server.print() statements.
Code:
server.print(lightper);
server.print(" %");
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oops, I thought I had mentioned that the dev said I need to use just one. I had been using multiple print statements in the very beginning when I was testing and only had one field that displayed (only one because I was just experimenting). It was supposed to stop reading data for a display and move on to the next when it go the new line terminator. So I was, of course, using print statements and making the last one println. Then things got crazy! If I had 3 fields, 1, 2, and 3 that should display A, B, and C respectively, I would get all sort of displays like: 1:C 2:A 3:BA then at the next refresh interval it might have been 1:AB 2:B 3:<blank>. If I happened to push a button one of the 3 may have displayed the default OK response.

Anyways, I found someone's page with a simple tutorial on sprintf. I still am going to have to read all weekend to figure it out. If someone knows of a good page/tutorial, or even a YouTube video, that would be really helpful. If someone knows it by heart and would quick click out the one I described above that would be great! (88 % where 88 is an analog input put into an int variable then mapped to between 0 and 100). I could draw off of that to figure the other few out.

Thanks guys!
Joey
Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Oops, I thought I had mentioned that the dev said I need to use just one.
Why?

Anyways, I found someone's page with a simple tutorial on sprintf. I still am going to have to read all weekend to figure it out.
It isn't difficult.  It is the same as printf(), except that you send the string to a buffer.

Code:
char buf[16];  // needs to be long enough to hold the entire string plus one null character.
sprintf(buf, "Value = %d%%", YourVariableName);  // obviously, you change YourVariableName
server.print(buf);

%d = integer
%% = percent sign
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Wow, that easy... Thank you very much.

I haven't used the printf function either. In fact, until recently I didnt even know one coukd use C functions not shown on the Arduino site. I am...a noobie.

I'm assuming that goes in the loop, or because its a formatter and the variable is updated in the loop can I put it in setup and not have to run 3-5 (or more) of these every time 'round the loop?

Thanks!
Joey
Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

because its a formatter
That's a bad assumption.  

The function needs to be called whenever the variable is updated, otherwise, buf[] wouldn't get updated.

So yes, it will go into loop(), wherever you want buf[] to contain the updated message.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you very much!
Logged

Pages: [1]   Go Up
Jump to: