Error message "not declared in scope" SOLVED

Hi

Just starting my attempt at reading a sensor through SPI. So I wrote this skeleton code that errors out when I try to compile.

ss1” was not declared in this scope

In the setup I did declare a “const int ss1”, used it as “ss1” to set pinmode and wrote it high( just to be sure to be sure :slight_smile: )
That work fine but when I use “ss1” in the loop it errors out.

What am I missing here?

I did google and searched forum, this seems to be way its done: declare it in set up, then use in remainder of code.

Note:
I commented out the line: digitalWrite (ss1,LOW); The line that errors out in loop
then the next variable “Ang_ss1” errors out.

Declarations in setup does not work in main loop?

/* ******************************************************************************
 *  SPI read 3x A1335 rotation sensor chips with UNO and display on serial monitor 
 *  Slave select pins 10 >ss1, 9 >ss2, 8 >ss3
 */
#include <SPI.h>

void setup() {
  // set slave pins and variables
  const int ss1 = 10;
  const int ss2 = 9;
  const int ss3 = 8;
  int Ang_ss1 = 0;
  int Ang_ss2 = 0;
  int Ang_ss3 = 0;

  //set slave pins High and as outputs
  
  pinMode (ss1,OUTPUT);
  digitalWrite (ss1,HIGH);
  pinMode (ss2,OUTPUT);
  digitalWrite (ss2,HIGH);
  pinMode (ss3,OUTPUT);
  digitalWrite (ss3,HIGH);
  
  SPI.begin();           
  Serial.begin(9600);
  
}

void loop() {
  
  /*SPI settings incorparated here and not declared globaly for attaching
    spi devices with difrent settings. Call transaction,begin with new
    settings*/
SPI.beginTransaction(SPISettings(15000000,MSBFIRST,SPI_MODE0));

//select sensor 1 to 3 and read registers 0x20 and 0x21

digitalWrite (ss1,LOW);      
Ang_ss1 = SPI.transfer16(0x20);
digitalWrite (ss1,HIGH);
digitalWrite (ss2,LOW);
Ang_ss2 = SPI.transfer16(0x20);
digitalWrite (ss2,HIGH);
digitalWrite (ss3,LOW);
Ang_ss3 = SPI.transfer16(0x20);
digitalWrite (ss3.HIGH);

SPI.endTransaction()

// convert received angle values to angular degrees

Serial.print(Ang_ss1*(360/4096)"DEG   ");
Serial.print(Ang_ss2*(360/4096)"DEG   ");
Serial.println(Ang_ss3*(360/4096)"DEG");

}

Then move declerations infront of " void set up"
Now it errors out on “digitalWrite (ss3,HIGH);” (Its happy with the same statement for previous ss2 and ss1 lines)
Expected constructor, destructor or type conversion before ‘(’

/* ******************************************************************************
 *  SPI read 3x A1335 rotation sensor chips with UNO and display on serial monitor 
 *  Slave select pins 10 >ss1, 9 >ss2, 8 >ss3
 */
#include <SPI.h>

  // set slave pins and variables
  const int ss1 = 10;
  const int ss2 = 9;
  const int ss3 = 8;
  int Ang_ss1 = 0;
  int Ang_ss2 = 0;
  int Ang_ss3 = 0;

  //set slave pins High and as outputs
  
  pinMode (ss1,OUTPUT);
  digitalWrite (ss1,HIGH);
  pinMode (ss2,OUTPUT);
  digitalWrite (ss2,HIGH);
  pinMode (ss3,OUTPUT);
  digitalWrite (ss3,HIGH);
  
void setup() {
  
  SPI.begin();           
  Serial.begin(9600);
  
}

void loop() {
  
  /*SPI settings incorparated here and not declared globaly for attaching
    spi devices with difrent settings. Call transaction,begin with new
    settings*/
SPI.beginTransaction(SPISettings(15000000,MSBFIRST,SPI_MODE0));

//select sensor 1 to 3 and read registers 0x20 and 0x21

digitalWrite (ss1,LOW);      
Ang_ss1 = SPI.transfer16(0x20);
digitalWrite (ss1,HIGH);
digitalWrite (ss2,LOW);
Ang_ss2 = SPI.transfer16(0x20);
digitalWrite (ss2,HIGH);
digitalWrite (ss3,LOW);
Ang_ss3 = SPI.transfer16(0x20);
digitalWrite (ss3.HIGH);

SPI.endTransaction()

// convert received angle values to angular degrees

Serial.print(Ang_ss1*(360/4096)"DEG   ");
Serial.print(Ang_ss2*(360/4096)"DEG   ");
Serial.println(Ang_ss3*(360/4096)"DEG");

}

With minor exceptions that don't apply here, all C / C++ code must be in a function. You have code (pinMode(...) and digitalWrite(...)) that is not inside a function.

You probably need to move that code into the setup() function. Yes, the error message at this point is not clear.

It is showing you EXACTLY where the error is. Compare that line, to the previous line where you digitalWrite to ss3. What is different?

Regards,
Ray L.

:slight_smile:
Solved !

Declare variables and int globally (before setup)
BUT functions like digitalWrite, pinMode and such INSIDE setup.

Thanks vaj4088