New guy wringing out the basics.

UNO … not certain of the model, came with a starter kit.

A bit of personal background if anyone is curious, if not … actual issue below the asterix’s below.

30+ Years of electronics, but as far as programming … haven’t touched it since the 6502. I’ve always found C to be nearly Greek.

Hopefully I’ll be able to lend a hand here with the hardware bits though.

Right now I’m going through the basics as if I was 15 again with an Apple 2.

Attempting to write short sketches to get a feel for the individual commands and syntax.

Ironically, I’ve got an intended (eventual) project wired up … a wrist watch utilizing HPDL 1416 displays and a RTC … but now I have to learn the language to make it work.


Today’s task simply to play with and utilize the “While” command.

The idea was to flash an LED ten times.

(I since realize that I’m not pointing to a break, but it doesn’t matter yet because it doesn’t work at all and tells me that I haven’t declared variable. I’m really for all intents day one here.)

The code:

Void setup( ){
pinmode (13, output);}
var = 0;
while (var < 10){
digitalwrite (13, high);
delay (100)
digitalwrite (13, low);
delay (100);
var++;
}

upon verifying, the message “exit status 1
‘var’ was not declared in this scope”.

I’ve tried playing a bit and declaring var in the setup, but I can’t seem to make it work.

What am I not getting?

Once I get over this hump, I’ll try using various other methods to test the While syntax … like an input pin state.

I don’t really know another way to learn besides hands on.

Thanks for any help.

Jim.

int var = 0;

Void setup( ){
pinmode (13, output);}
var = 0;
while (var < 10){
digitalwrite (13,  high);
delay (100)
digitalwrite (13, low);
delay (100);
var++;
}

read:
variable scope https://www.w3adda.com/cplusplus-tutorial/cpp-variable-scope

Didn’t it also complain about “Void”?
“pinmode”?
“digitalwrite”?
The missing loop function?

Please remember to use code tags when posting code

Hi Jim,

Next time be sure to post your code using code tags, otherwise it may not show up correctly.

You have a several issues:

  1. You prematurely ended your setup() function by including a brace after this line:
pinmode (13, output);}
  1. You didn’t include a type declaration for var.

  2. ‘Void’ should be ‘void’

  3. ‘digitalwrite’ should be ‘digitalWrite’

  4. case matters!

Try this:

void setup() {
  pinMode (13, OUTPUT);
  int var = 0;
  while (var < 10) {
    digitalWrite (13, HIGH);
    delay (100);
    digitalWrite (13, LOW);
    delay (100);
    var++;
  }
}

Ah, sorry yes .. will work out using the code tags in the future .. redface.

The case was my transcription error.

without the While .. I can make LEDs flash and utilize a pushbutton. Lol .. kinda sad as for a drunken exercise I could plow off a "BTTF" clock with a half pound of 80's MSI and a metric handful of 1N914's. But we all have to move into the 21st century eventually.

Your code doesn't work cut and paste .. but I glean that I have to declare val as an integer in setup? There's a part of me that thinks this might have been easier if I didn't know how Waterloo BASIC worked.

Help appreciated nevertheless, and I will go through that link.

Jim.

Micro-snob:
Your code doesn't work cut and paste .. but I glean that I have to declare val as an integer in setup? There's a part of me that thinks this might have been easier if I didn't know how Waterloo BASIC worked.

Hmmm. Worked fine for me. It only fast blinks for 2 seconds.

int var = 0;

void setup( ){
pinMode (13,OUTPUT);}
var = 0;
while (var < 10){
digitalWrite (13, HIGH);
delay (100)
digitalWrite (13, LOW);
delay (100);
var++;
}

Just checking if that’s how code works. Seems to.

Swapped out a bit of case, but getting an

“exit status 1
‘var’ does not name a type” message.

Sorry I’m so dense mate.

Jim.

Oh .. something funny here. Your case was indeed proper .. somehow my cut and paste.

New error message now though: exit status 1
Error compiling for board Arduino Uno.

Dunno if it's relevant but my dialog box shows version 1.8.11 and I'm on a win 7 laptop.

Jim.,

Edit: Just reloaded the tutorial "flash" sketch to confirm USB and stuff working - seems to be.

int var ;

void setup( )
{
  pinMode (13,OUTPUT);
  while (var < 10){
    digitalWrite (13, HIGH);
    delay (100)
    digitalWrite (13, LOW);
    delay (100);
    var++;
  }
}
void loop()
{}

(uncompiled, untested)

Make sure you include the loop() function and move the out of place brace. If you cut and paste my code and add the loop() function it should work.

You must ALWAYS have a setup() and loop() function.

Micro-snob:
30+ Years of electronics, but as far as programming .. haven't touched it since the 6502. I've always found C to be nearly Greek.

Similar to me. Started coding the 6502 in assembler, moved to the 8080, and did a lot of control code in HPL. I actually had my hands on a working Apple 1. (Wozniack was a neighbor). I never used C until I started using the Arduino about two years ago. (I look back at my early posts- boy did I ask a lot of really dumb questions).

I am certainly no C++ expert, but I am about as safe as a 100-hour pilot.

Start small. Get a function working, document it well then set it aside and work on the next one.

int led=13; int var=0;

void setup() {                
   pinMode(led,OUTPUT);     
}
void loop() {
  
{
  
  while (var < 20){
    digitalWrite (led, HIGH);
    delay (100);
    digitalWrite (led, LOW);
    delay (100);
    var++;
    }
 digitalWrite (13, LOW); 
}}

Okay, got it! This works for me … flashes 20 times then goes off.

Thanks much for the help so far.

I know it’s just a matter of buckling down and keeping at it … as well, getting a handle on the declarations … defining led as 13. How the compiler parses is a mystery to me … my assumption was that setup came first.

Now to get a pushbutton in there to reset the main loop!

I tend to learn by doing … so even this code is a joke - the idea is to expand it utilizing one command at a time until I get a handle on the syntax.

Jim.

int led=13; int var=0; int button=12; int reset=0; // declare variables.

void setup() {                
   pinMode(led,OUTPUT);pinMode(button,INPUT); // set pins to i/o.    
}
void loop() {
  
{
 
  while (var < 20){
    digitalWrite (led, HIGH);  // turn on LED.
    delay (100);  // wait.
    digitalWrite (led, LOW);  // turn off LED.
    delay (100);  // wait.
    var++; // increment var by one.
    }
 digitalWrite (led, LOW); // shut off LED after 20 flashes.
  reset = digitalRead (button); // look at input and assign value to variable 'reset'
  if reset = HIGH goto // How and Where?
  
   }}

Hmm … It’s coming somewhat. I’m old and crazy and I don’t learn as fast as I did in 1970 and spare change. The last line … And who thought that counting curly braces was way better then line numbers? Oh yeah … Gates. Despite the fact that the compiler doesn’t like the syntax in it’s entirety … how do I call - or rather send - back to the right of the first open curly of the “main” loop? Seems I cant GOTO “loop”

Steve … did you run into the same wall of trying to figure out how the ML is parsing the written code? Some of the curve is vestigial … x=x+1 when it never did … I was a C= man even though the fruit of the Job’s loom was admittedly superior. Any anecdotal or intuitive slaps to the head useful to make this easier? Is it about letting it all go?

I have to tell myself though … I’m still on day one. The carburetor has been a dead technology for 30 years. Who cares if I can tune a Rochester Q-jet bloody-arsed full in the whiskey cups.

I do wish however for a hard copy tutorial. I like highlighting, dog-earing, adding margin notes and post-its. Such a thing exist in 2020?

Despite the fact that the compiler doesn’t like the syntax in it’s entirety

sp. “Despite the fact that the compiler doesn’t like the syntax in its entirety”

And who thought that counting curly braces was way better then line numbers?

Is the answer “Everyone” ?
(I never noticed line numbers defining scope. Maybe I missed something)

(sp. “than line numbers”)

who thought that counting curly braces was way better then line numbers?

One does not count curly braces, one puts each one on its own line then uses Auto Format in the IDE to show the structure clearly. Even better, get Auto Format to put the curly braces on their own line for you.

If you put the editor cursor just after a curly brace (or any other type of bracket/brace) then its partner will be indicated in the IDE by a box round it

how do I call - or rather send - back to the right of the first open curly of the "main" loop?

You don't need to do anything. As its name implies the loop() function loops back to the start and runs again when it reaches the end. If you really do want to get back to the start of loop() somewhere else in the code other than the end of the function then return; will do the job nicely

Micro-snob:
Who cares if I can tune a Rochester Q-jet bloody-arsed full in the whiskey cups.

Possibly someone who has even the faintest idea what you're talking about?

The thing about programming in any language is you have to put in some effort to learn what the language does and how it does it. Trying to write stuff based on what you think it SHOULD do is a recipe for real frustration.

Steve

UKHeliBob:
One does not count curly braces, one puts each one on its own line then uses Auto Format in the IDE to show the structure clearly. Even better, get Auto Format to put the curly braces on their own line for you.

Wouldn't work. If your closing brace mismatch is because you forgot a terminating semicolon a few lines above the brace, how would the compiler know that the extra closing brace is is really extra?

Micro-snob:
.. how do I call - or rather send - back to the right of the first open curly of the "main" loop? Seems I cant GOTO "loop"

The Arduino IDE generates a main() function in the background. It kind of looks like this:

void main() {
  init();
  setup();
  while (1)
    loop();
}

(Wozniack was a neighbor)

Wait a second.. You grow up in Palo Alto?

-jim lee

Micro-snob:
And who thought that counting curly braces was way better then line numbers? Oh yeah .. Gates.

Knuth, Wirth, and a few others actually. Structured programming - Wikipedia

If braces are a new thing for you, then object-oriented programming is going to blow your mind, functional programming will erase it altogether.

But you don't need to count curly braces. Use ctrl-T and your code will be nicely indented. You can see, visually, which blocks are inside which other blocks.

Having said that, there's nothing at all stopping you from writing your code as a single big slab in the loop() method with all the logic in if(somecondition) goto somelabel; statements. It's just that the entire worldwide computing community doesn't do it this way anymore, and thank God for that.