simple script, trying to learn

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 :smiley:

  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?

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

If you want to add something to something no matter what its value is, then you don't need a conditional.

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.

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 :stuck_out_tongue:

varVal++;

Would suffice

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

ÿþþÿþþÿþÿþþþýýýýýýþþþÿÿþþþþþÿýýýýýþþþÿÿþþþþþÿÿýýýýüüþÿÿþÿþþþþÿÿýýýüþþÿÿþÿþþþþýýýýýüþþÿÿÿÿþþþýýýýýüüýùÿùÿøÿùþøþúþúúúûúøýøÿøþúþúûýùýùýùýùýøþøýøþúúýùÿøÿøþøÿúùýùýùÿùÿùÿùÿùÿøþøûúúýùþøýøþúûúùý

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?

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); ?

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.

haha! well i worked that much out successfully :stuck_out_tongue:

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 :slight_smile:

thanks for all the help!
should i change the title of the topic to SOLVED: ?

should i change the title of the topic to SOLVED: ?

Sure.

one last little thing for this topic :stuck_out_tongue:

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?

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...
}

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?

Try

if (!(varVal % 10))
{  /* varVal is a multiple of 10 */
}

hi morris, thanks for your input :smiley:

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 :smiley:
learning is fun :stuck_out_tongue:

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(" ");
}

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.