'val' not declared in this scope, simple solenoid sketch.

Hello all,

Finally have the training wheels off and am trying to alter some code (have been learning through 'Arduino Dummies', still feeling like the latter term....).

The basic aim is to control a solenoid from Max/msp over serial. Sending an 'G' makes the value go high, and and sending a 'S' makes the value go low.

I'm combining a basic sketch from a 'how to control a solenoid' exercise, with some code that someone else helped me make for another project, and I've clearly missed something.

The error that I'm getting is ''val' was not declared in this scope', which highlights the last line of code.

I understand that you need to declare things at the start of the code in terms of the ins/outs of the Arduino, but I'm at a loss as to how I'm supposed to declare that part of the code.

Can someone point out the obvious to me?

int solenoidPin = 9 ;                   

void setup() 
{
  pinMode(solenoidPin, OUTPUT);         
  Serial.begin(57600);
  
}

void loop() 
{
  if (Serial.available() > 0) {char val = Serial.read();}
  if (val == 'S'){digitalWrite(solenoidPin,LOW);} 
  if (val == 'G'){digitalWrite(solenoidPin,HIGH);}
  
}

https://www.arduino.cc/reference/en/language/variables/variable-scope--qualifiers/scope/

Hopefully will also resolve the structure problem.

Thanks for that @Coding Badly, I didn't know about the idea of local variables, so that's a big help in terms of the learning.

But what I still don't understand is why its an issue, or how the structure needs to change.

val is local to what?

I thought it was local to that 'if' statement, or do I completely have my brain on wrong? :slight_smile:

I'm only using that as it was in the code that I was given previously for another arduino/Max project, so I'm not entirely sure why that is important (very new to this, better with Max).

Is there a better way to that using global variables?

toddak:
I thought it was local to that 'if' statement, or do I completely have my brain on wrong? :slight_smile:

It is.

What else is in that scope?

The scope of the if statement is defined by the curly braces "{ ... }". Consider moving the closing one elsewhere.

The only other things that i can see is Serial.available, which from what (little) I understand is a command checking to see if there is anything being sent over serial at that point in the loop.

Serial.read I'm still unsure about, but I assume that is has to with the fact that it then defines 'val' as being whatever the item being read through the serial connection is.

Oh, and if serial.available is greater than zero, then it should store the value in 'val'.

I suspect your answer is correct but a yes / no question will be concrete. Is this line within the scope containing val...

if (val == 'S'){digitalWrite(solenoidPin,LOW);}

Consider @jremington's post before answering.

Ah, now I get it. It's only local to that 'if' statement, the others can't access it.

And that's something else that I didn't really understand before in terms of my Arduino Vocabulary, why the brackets where often at the end. I guess it's kind of like the use of brackets in mathetmatics in a way?

So it should be:

int solenoidPin = 9 ;                   

void setup() 
{
  pinMode(solenoidPin, OUTPUT);         
  Serial.begin(57600);
  
}

void loop() 
{
  if (Serial.available() > 0) {char val = Serial.read();
  if (val == 'S'){digitalWrite(solenoidPin,LOW);
  if (val == 'G'){digitalWrite(solenoidPin,HIGH);}
  }}
}

What's the etiquette in terms of where the closing brackets are placed visually speaking?

And then extending on the questions, I assumed that I should be able to type the letters 'S' and 'G' into the serial window, and would be able to see the Solenoid moving, but this doesn't seem to work.

Another incorrect assumption?

You still have braces that are incorrectly matched.

For one line "if" statements you don't need them at all.

Remove the braces from the secondary if statements (like that below) and it should work.

  if (val == 'G'){digitalWrite(solenoidPin,HIGH);}

Wonderful, that is working a treat.

In terms of the braces, is that more of a grammar thing, or is does it have to do with information ordering? or something else? Just asking to extend my knowledge :slight_smile:

Many thanks to both of you, I really appreciate the 'teaching a person to fish' approach to learning :slight_smile:

Glad it is working!

Braces are used to group blocks of code into logical units, so, for example a single "if" statement can cause several otherwise unrelated actions to be taken. Many people like to group them like this:

if (something) {
do_this;
then_that;
execute_function_y();
}

As you have seen, local variables can be defined within such blocks without worry that the values will propagate and/or interfere with code elsewhere.

toddak:
I guess it's kind of like the use of brackets in mathetmatics in a way?

Yes.

What's the etiquette in terms of where the closing brackets are placed visually speaking?

My preference (note the added const)...

const int solenoidPin = 9;                   

void setup() 
{
  pinMode(solenoidPin, OUTPUT);         
  Serial.begin(57600);
}

void loop() 
{
  if (Serial.available() > 0) 
  {
    char val = Serial.read();

    if (val == 'S')
    {
      digitalWrite(solenoidPin,LOW);
    }

    if (val == 'G')
    {
      digitalWrite(solenoidPin,HIGH);
    }
  }
}

An else is a good choice...

const int solenoidPin = 9;                   

void setup() 
{
  pinMode(solenoidPin, OUTPUT);         
  Serial.begin(57600);
}

void loop() 
{
  if (Serial.available() > 0) 
  {
    char val = Serial.read();

    if (val == 'S')
    {
      digitalWrite(solenoidPin,LOW);
    }
    else if (val == 'G')
    {
      digitalWrite(solenoidPin,HIGH);
    }
  }
}

Wonderful, thanks for the added information too!

You are welcome.

toddak:
And that's something else that I didn't really understand before in terms of my Arduino Vocabulary, why the brackets where often at the end. I guess it's kind of like the use of brackets in mathetmatics in a way?

What's the etiquette in terms of where the closing brackets are placed visually speaking?

The code you write is processed by a compiler, which mechanically goes though the code and converts it into machine language which a microprocessor can execute. Compilers don't think, and a C compiler doesn't really care about how the code is laid out visually (in some languages it does matter, in C it doesn't). To a C compiler, spaces, tabs, line breaks, and comments are all 'white space' and are all treated in the same way (except when they occur in a "string" or a #preprocessor directive). When you declare a variable, for instance:

int var;

The only thing that matters is that there must be white space between the keyword 'int' and the identifier 'var'. This:

   int 

var;

will do the same thing.

As for how it should look visually speaking, there are a number of different formatting styles for code, and plenty of good-natured banter about the relative merits of each. If you press command-T (on a mac) in the arduino IDE, it will auto-format your code according to the rules in formatter.conf.

Thanks for those tips too, I didn't know about the auto-format option.

I've had some comments on 'incorrectly formatted code' on the forums, so I felt there was an underlying visual grammar that I needed to understand. I know about a similar issue in terms of visual organisation on the Max/MSP forums, but that's a little easier to 'see' in terms of mess :slight_smile:

Thanks again!