system
January 16, 2012, 8:14pm
1
hey all, im trying to write a simple script just to get used to the language and coding of the arduino, and i need a little guidance.
im just trying to make a little script that loops, and each time adds 1 to a variable, and then prints the number out. so far ive got this:
int timer = 100; //timer - high values make longer times (milliseconds)
int varVal = 0; //variable
void setup() {
Serial.begin(9600);
}
void loop() {
if (varVal == || != 0) {
int varVal++;
}
delay(timer);
Serial.println(varVal);
}
and i get this error message
sketch_jan16b.cpp: In function 'void loop()':
sketch_jan16b:8: error: expected primary-expression before '||' token
sketch_jan16b:8: error: expected primary-expression before '!=' token
sketch_jan16b:9: error: expected initializer before '++' token
id love any and all guidance available from the boffins here
system
January 16, 2012, 8:23pm
2
if (varVal == || != 0) {
If varVal equals what? If what not equal 0? Those are the parts that are missing.
int varVal++;
What's with the int keyword? What are you trying to do here?
system
January 16, 2012, 8:38pm
3
most of this code was totally guess work, its what i thought would work
PaulS:
If varVal equals what? If what not equal 0? Those are the parts that are missing.
basically this was me attempting to tell the code to add to the value no matter what its current value is
What's with the int keyword? What are you trying to do here?
as i said, i dont know the language so i thought i needed it to tell the interger to add up
system
January 16, 2012, 8:40pm
4
If you want to add something to something no matter what its value is, then you don't need a conditional.
system
January 16, 2012, 8:50pm
5
If you want to add something to something no matter what its value is, then you don't need a conditional.
And, you've already defined that varVal is an int.
system
January 16, 2012, 8:53pm
6
ok, so what command would i use to tell it to add? when i tried other things it gave me more errors, this script gave me the least amount of errors in return
system
January 16, 2012, 9:51pm
8
ah cool, dunno why i didnt do that before
well it compiled properly with no errors, but the print is wrong.
int timer = 100; //timer - high values make longer times (milliseconds)
int varVal = 0; //variable
void setup() {
Serial.begin(9600);
}
void loop() {
varVal++;
delay(timer);
Serial.println(varVal, DEC
);
}
prints out
ÿþþÿþþÿþÿþþþýýýýýýþþþÿÿþþþþþÿýýýýýþþþÿÿþþþþþÿÿýýýýüüþÿÿþÿþþþþÿÿýýýüþþÿÿþÿþþþþýýýýýüþþÿÿÿÿþþþýýýýýüüýùÿùÿøÿùþøþúþúúúûúøýøÿøþúþúûýùýùýùýùýøþøýøþúúýùÿøÿøþøÿúùýùýùÿùÿùÿùÿùÿøþøûúúýùþøýøþúûúùý
system
January 16, 2012, 10:05pm
9
Looks like a mismatch between the baud rate on the Arduino end and the baud rate on the PC end. What baud rate is the Serial Monitor set to?
system
January 16, 2012, 10:10pm
10
PaulS:
Looks like a mismatch between the baud rate on the Arduino end and the baud rate on the PC end. What baud rate is the Serial Monitor set to?
ive not heard of baud before so i couldnt tell you
is that what the number is for when you enter the line Serial.begin(9600);
?
system
January 16, 2012, 10:11pm
11
is that what the number is for when you enter the line
Yes.
The Serial Monitor has a drop down field on the lower, right that should be used to select the same value.
system
January 16, 2012, 10:16pm
12
haha! well i worked that much out successfully
i matched the numbers but its just printing wierd things, as in every character available to print on a default windows system.
each character on a new line but it goes through the alphabet, lower and upper case, then numbers, then symbols, then foreign characters etc, then loops.
is this because of the ending to my print line? should i use a different factor?
EDIT:
yeah that was the cause, i tampered it to be (varVal, 0) and i changed it to (varVal, DEC) and it works wonderfully, counting up
thanks for all the help!
should i change the title of the topic to SOLVED: ?
system
January 16, 2012, 11:26pm
14
one last little thing for this topic
can i do math? such as im just playing with this script now, and ive made it make a new line when varVal == 10, and then set the value to 0
can i make it work out if its a multiple of 10 and then make a new line to keep counting?
system
January 16, 2012, 11:29pm
15
can i do math?
I don't know. The Arduino can, though.
such as im just playing with this script now, and ive made it make a new line when varVal == 10, and then set the value to 0
if(varVal >= 10)
varVal = 0;
can i make it work out if its a multiple of 10 and then make a new line to keep counting?
if(varVal % 10)
{
// varVal is a multiple of 10...
}
system
January 16, 2012, 11:35pm
16
PaulS:
can i do math?
I don't know. The Arduino can, though.
how did i know you or someone else would put that lmao
can i make it work out if its a multiple of 10 and then make a new line to keep counting?
if(varVal % 10)
{
// varVal is a multiple of 10...
}
oh, easy as that? ill give it a whirl
EDIT:
well it just lists every number as a new line, which means its dividing everything by 10, except 11, 21, 31 etc, which is odd
i want it to only count incriments of 10, so every 10 numbers move onto a new line
i suppose i could do this with a second variable which isnt printed and resets on each 10'th?
system
January 16, 2012, 11:43pm
17
Try
if (!(varVal % 10))
{ /* varVal is a multiple of 10 */
}
system
January 16, 2012, 11:54pm
18
hi morris, thanks for your input
what does the ! symbol do? i know that != is "does not equal" so is it the same thing, or does it have a different meaning on its own?
EDIT:
that worked perfectly, now id just like to know why
learning is fun
system
January 16, 2012, 11:56pm
19
well it just lists every number as a new line, which means its dividing everything by 10, except 11, 21, 31 etc, which is odd
We need to see what you put in the if block and what you put in the else block. It should look like:
if(varVal % 10)
{
Serial.println(varVal, DEC);
}
else
{
Serial.print(varVal, DEC);
Serial.print(" ");
}
system
January 16, 2012, 11:59pm
20
The exclamation is a logical inversion operator. I used it to make the condition true only when the remainder of dividing varVal by 10 would be zero.