Strange Serial Monitor results

Hey there, i'm trying to get this code to work on my arduino. its strange because it was working earlier, then all of a sudden it isnt reading the pot value. I have tried this on an uno and a duemilanove. I've tested other sketches and they are working fine.

Here is the code:

  const int LED = 3;
int pot = A4;
void setup()
{
 //pinMode (pot, INPUT);
 Serial.begin(9600);
}

void loop()
{
 
  float in, out;
  float val = analogRead(pot);
    //float val = analogRead(0);  
  val = map(val, 0, 1023, 0, 1000.000);
  //analogWrite(pot, val);
  Serial.print('potval ');
    Serial.println(val);
  
  for (in = 0; in < 6.283; in = in + (.0001 + val/100000))
  {
    out = sin(in) * 127.5 + 127.5;
    analogWrite(LED,out);
    Serial.print('ledOut ');
    Serial.println(out);
  }
}

And here is what its displaying on the serial monitor.

29728127.53
29728127.54
29728127.55
29728127.56
29728127.58
29728127.59
29728127.60
29728127.61
29728127.63
29728127.64
29728127.65
29728127.67
29728127.68
29728127.69
29728127.70
29728127.72
29728127.73
29728127.74
29728127.75
29728127.77
29728127.78
29728127.79
29728127.81
29728127.82
29728127.83
29728127.84
29728127.86
29728127.87
29728127.88
29728127.90
29728127.91
29728127.92
29728127.93
29728127.95
29728127.96
29728127.97
29728127.98
29728128.00
29728128.01
29728128.02
29728128.04
29728128.05
29728128.06
29728128.07
29728128.09
29728128.10
29728128.11
29728128.12
29728128.14
29728128.15
29728128.16
29728128.18
29728128.19
29728128.20
29728128.21
29728128.23
29728128.24
29728128.25
29728128.27
29728128.28
29728128.29
29728128.30
29728128.32
29728128.33
29728128.34
29728128.35
29728128.37
29728128.38
29728128.39
29728128.41
29728128.42
29728128.43
29728128.44
29728128.46
29728128.47
29728128.48
29728128.49
29728128.51
29728128.52
29728128.53
29728128.55
29728128.56
29728128.57
29728128.58
29728128.60
29728128.61
29728128.62
29728128.63
29728128.65
29728128.66
29728128.67
29728128.69
29728128.70
29728128.71
29728128.72
29728128.74
29728128.75
29728128.76
29728128.77
29728128.79
29728128.80
29728128.81
29728128.83
29728128.84
29728128.85
29728128.86
29728128.88
29728128.89
29728128.90
29728128.92
29728128.93
29728128.94
29728128.95
29728128.97
29728128.98
29728128.99
29728129.00
29728129.02
29728129.03
29728129.04
29728129.06
29728129.07
29728129.08
29728129.09
29728129.11

Any idea why this code isn't functioning properly? like i mentioned, other code is compiling and running fine. this code uploads fine but it doesn't react to a potentiometer and isn't printing out anything near what i am specifying it to print in the serial monitor.

Thanks in advance for your wisdom.
-D

  1. analogRead() returns an integer, not a float
  2. map only works on integers, not float
  3. analogWrite() only accepts integers, not float

Notice the recurring problem with your code?

@James C4S, the data-type issue is a reasonable theory and certainly a valid concern but it is not the cause of @Drc3p0's problem. This sketch illustrates that map works as expected when out_max is a floating-point constant...

void setup( void )
{
  Serial.begin(9600);
}

void loop( void )
{
  float val; // = analogRead(pot);
  int ar;
  
  for ( ar=0; ar <= 1023; ++ar )
  {
    val = ar; // = analogRead(pot);
    val = map(val, 0, 1023, 0, 1000.000);
    Serial.print( ar );
    Serial.write( '\t' );
    Serial.println( val );
  }
  
  while ( true );
}
0	0.00
1	0.00
2	1.00
3	2.00
4	3.00
5	4.00
6	5.00
7	6.00
8	7.00
9	8.00
10	9.00
11	10.00
12	11.00
13	12.00
14	13.00
15	14.00
16	15.00
17	16.00
18	17.00
19	18.00
20	19.00
21	20.00
22	21.00
23	22.00
24	23.00
25	24.00
26	25.00
27	26.00
28	27.00
29	28.00
30	29.00
31	30.00
32	31.00
33	32.00
34	33.00
35	34.00
36	35.00
37	36.00
38	37.00
39	38.00
40	39.00
41	40.00
42	41.00
43	42.00
44	43.00
45	43.00
46	44.00
47	45.00
48	46.00
49	47.00
50	48.00
51	49.00
52	50.00
53	51.00
54	52.00
55	53.00
56	54.00
57	55.00
58	56.00
59	57.00
60	58.00
61	59.00
62	60.00
63	61.00
64	62.00
65	63.00
66	64.00
67	65.00
68	66.00
69	67.00
70	68.00
71	69.00
72	70.00
73	71.00
74	72.00
75	73.00
76	74.00
77	75.00
78	76.00
79	77.00
80	78.00
81	79.00
82	80.00
83	81.00
84	82.00
85	83.00
86	84.00
87	85.00
88	86.00
89	86.00
90	87.00
91	88.00
92	89.00
93	90.00
94	91.00
95	92.00
96	93.00
97	94.00
98	95.00
99	96.00
100	97.00
101	98.00
102	99.00
103	100.00
104	101.00
105	102.00
106	103.00
107	104.00
108	105.00
109	106.00
110	107.00
111	108.00
112	109.00
113	110.00
114	111.00
115	112.00
116	113.00
117	114.00
118	115.00
119	116.00
120	117.00
121	118.00
122	119.00
123	120.00
124	121.00
125	122.00
126	123.00
127	124.00
128	125.00
129	126.00
130	127.00
131	128.00
132	129.00
133	130.00
134	130.00
135	131.00
136	132.00
137	133.00
138	134.00
139	135.00
140	136.00
141	137.00
142	138.00
143	139.00
144	140.00
145	141.00
146	142.00
147	143.00
148	144.00
149	145.00
150	146.00
151	147.00
152	148.00
153	149.00
154	150.00
155	151.00
156	152.00
157	153.00
158	154.00
159	155.00
160	156.00
161	157.00
162	158.00
163	159.00
164	160.00
165	161.00
166	162.00
167	163.00
168	164.00
169	165.00
170	166.00
171	167.00
172	168.00
173	169.00
174	170.00
175	171.00
176	172.00
177	173.00
178	173.00
179	174.00
180	175.00
181	176.00
182	177.00
183	178.00
184	179.00
185	180.00
186	181.00
187	182.00
188	183.00
189	184.00
190	185.00
191	186.00
192	187.00
193	188.00
194	189.00
195	190.00
196	191.00
197	192.00
198	193.00
199	194.00
200	195.00
201	196.00
202	197.00
203	198.00
204	199.00
205	200.00
206	201.00
207	202.00
208	203.00
209	204.00
210	205.00
211	206.00
212	207.00
213	208.00
214	209.00
215	210.00
216	211.00
217	212.00
218	213.00
219	214.00
220	215.00
221	216.00
222	217.00
223	217.00
224	218.00
225	219.00
226	220.00
227	221.00
228	222.00
229	223.00
230	224.00
231	225.00
232	226.00
233	227.00
234	228.00
235	229.00
236	230.00
237	231.00
238	232.00
239	233.00
240	234.00
241	235.00
242	236.00
243	237.00
244	238.00
245	239.00
246	240.00
247	241.00
248	242.00
249	243.00
250	244.00
251	245.00
252	246.00
253	247.00
254	248.00
255	249.00
256	250.00
257	251.00
258	252.00
259	253.00
260	254.00
261	255.00
262	256.00
263	257.00
264	258.00
265	259.00
266	260.00
267	260.00
268	261.00
269	262.00
270	263.00
271	264.00
272	265.00
273	266.00
274	267.00
275	268.00
276	269.00
277	270.00
278	271.00
279	272.00
280	273.00
281	274.00
282	275.00
283	276.00
284	277.00
285	278.00
286	279.00
287	280.00
288	281.00
289	282.00
290	283.00
291	284.00
292	285.00
293	286.00
294	287.00
295	288.00
296	289.00
297	290.00
298	291.00
299	292.00
300	293.00
301	294.00
302	295.00
303	296.00
304	297.00
305	298.00
306	299.00
307	300.00
308	301.00
309	302.00
310	303.00
311	304.00
312	304.00
313	305.00
314	306.00
315	307.00
316	308.00
317	309.00
318	310.00
319	311.00
320	312.00
321	313.00
322	314.00
323	315.00
324	316.00
325	317.00
326	318.00
327	319.00
328	320.00
329	321.00
330	322.00
331	323.00
332	324.00
333	325.00
334	326.00
335	327.00
336	328.00
337	329.00
338	330.00
339	331.00
340	332.00
341	333.00
342	334.00
343	335.00
344	336.00
345	337.00
346	338.00
347	339.00
348	340.00
349	341.00
350	342.00
351	343.00
352	344.00
353	345.00
354	346.00
355	347.00
356	347.00
357	348.00
358	349.00
359	350.00
360	351.00
361	352.00
362	353.00
363	354.00
364	355.00
365	356.00
366	357.00
367	358.00
368	359.00
369	360.00
370	361.00
371	362.00
372	363.00
373	364.00
374	365.00
375	366.00
376	367.00
377	368.00
378	369.00
379	370.00
380	371.00
381	372.00
382	373.00
383	374.00
384	375.00
385	376.00
386	377.00
387	378.00
388	379.00
389	380.00
390	381.00
391	382.00
392	383.00
393	384.00
394	385.00
395	386.00
396	387.00
397	388.00
398	389.00
399	390.00
400	391.00
401	391.00
402	392.00
403	393.00
404	394.00
405	395.00
406	396.00
407	397.00
408	398.00
409	399.00
410	400.00
411	401.00
412	402.00
413	403.00
414	404.00
415	405.00
416	406.00
417	407.00
418	408.00
419	409.00
420	410.00
421	411.00
422	412.00
423	413.00
424	414.00
425	415.00
426	416.00
427	417.00
428	418.00
429	419.00
430	420.00
431	421.00
432	422.00
433	423.00
434	424.00
435	425.00
436	426.00
437	427.00
438	428.00
439	429.00
440	430.00
441	431.00
442	432.00
443	433.00
444	434.00
445	434.00
446	435.00
447	436.00
448	437.00
449	438.00
450	439.00
451	440.00
452	441.00
453	442.00
454	443.00
455	444.00
456	445.00
457	446.00
458	447.00
459	448.00
460	449.00
461	450.00
462	451.00
463	452.00
464	453.00
465	454.00
466	455.00
467	456.00
468	457.00
469	458.00
470	459.00
471	460.00
472	461.00
473	462.00
474	463.00
475	464.00
476	465.00
477	466.00
478	467.00
479	468.00
480	469.00
481	470.00
482	471.00
483	472.00
484	473.00
485	474.00
486	475.00
487	476.00
488	477.00
489	478.00
490	478.00
491	479.00
492	480.00
493	481.00
494	482.00
495	483.00
496	484.00
497	485.00
498	486.00
499	487.00
500	488.00
501	489.00
502	490.00
503	491.00
504	492.00
505	493.00
506	494.00
507	495.00
508	496.00
509	497.00
510	498.00
511	499.00
...

@Drc3p0, what board are you using?

Try changing this line...

Serial.print('potval ');

...to this (double-quotes)...

Serial.print("potval ");

Changing the serial potval to use "potVal" instead of 'potval' cleaned up the serial monitor! Strange because both ways of indicating makes the text turn to blue, indicating a recognized function. Strange that that would affect it in such a way that it would prevent it from uploading successfully. I was having an issue with Arduino saying that it had uploaded sucessfully, then discovering that it hadn't erased the previous sketch at all, and was just performing the old sketch that was already on the board from a previous experiment.

This discussion does raise a good point though. I did not know that the map function couldn't return a decimal #. How would i get it to return a decimal number to the 2nd power (however you say that properly.) I want to get values that range from .001 to .1
how would I go about obtaining that range of values?

how would I go about obtaining that range of values?

Have it return a value between 1 and 1000, and then divide by 1000.0.

int pot = A4;
void setup()
{
 //pinMode (pot, INPUT);
 Serial.begin(9600);
}

void loop()
{
 
  float in, out;
  float val = analogRead(pot);
    //float val = analogRead(0);

A glance at you code (above) makes it look like you where using A0 and are now using A4. Look at the value of "pot" and the commented out analogRead(). Which analog input do you have the pot wired to?

Mark

I changed the analog out and Led pins according to what boards I was testing with. I have shields already built for testing from different experiments.

Drc3p0:
This discussion does raise a good point though. I did not know that the map function couldn't return a decimal #.

Yeah, map needs some work.

How would i get it to return a decimal number to the 2nd power (however you say that properly.) I want to get values that range from .001 to .1
how would I go about obtaining that range of values?

Give this a try...

#define bettermap( value, fromLo, fromHi, toLo, toHi ) \
    ({ \
      typeof (value) _value = (value); \
      typeof (fromLo) _fromLo = (fromLo); \
      typeof (fromHi) _fromHi = (fromHi); \
      typeof (toLo) _toLo = (toLo); \
      typeof (toHi) _toHi = (toHi); \
      (value - fromLo) * (toHi - toLo) / (fromHi - fromLo) + toLo; \
    })
         
  const int LED = 3;
const int pot = A4;
void setup()
{
 //pinMode (pot, INPUT);
 Serial.begin(9600);
}

void loop()
{
 
  float in, out;
  float val = analogRead(pot);
    //float val = analogRead(0);  
  val = bettermap(val, 0, 1023, 0, 0.1);
  //analogWrite(pot, val);
  Serial.print('potval ');
    Serial.println(val);
  
  for (in = 0; in < 6.283; in = in + (.0001 + val/100000))
  {
    out = sin(in) * 127.5 + 127.5;
    analogWrite(LED,out);
    Serial.print('ledOut ');
    Serial.println(out);
  }
}

Yes, that bettermap "function" works great! Thank you so much Coding Badly!

This code is working well now. thanks for all the insight!

Drc3p0:
Changing the serial potval to use "potVal" instead of 'potval' cleaned up the serial monitor! Strange because both ways of indicating makes the text turn to blue, indicating a recognized function.

I totally ignore the colours that the IDE changes things to. This is recognized:

while (true)
  { }

But it won't do much useful, unless you want your code to stop. The colouring doesn't prove much.

I use that fairly often. It's usually when I want to check the hardware (pin voltages, postion of a servo, whatever) at a particular point in the code.

#define HALT while (1){}

Thankfully I put in the qualification:

But it won't do much useful, unless you want your code to stop.

Drc3p0:
Yes, that bettermap "function" works great! Thank you so much Coding Badly!

You are welcome and thank you for the follow-up. (And thank you to @bperrybap for the inspiration.)

Replace pot=A4; by pot=4;

The values A1, A2 ... are there to use the analog input pins as digital input/output pins with pinMode, digitalRead, digitalWrite.
Just use the plain pin number with analogRead.

The Arduino folks seem to be encouraging the use of the A* constants with analogRead...

I'm inclined to follow their lead.

Ah yes, you are right - after reading again very carefully the docs, I have to come to the conclusion that the pin mapping is way more complicated than I thought.
My mistake !