Solved : Why a simple variable setting raises compile error ?

hi,
sometimes its difficult to guess whats may be wrong

/* test_H0_stellwk_3
  Testsketch for analog and digital Pins
   for ARDIUNO Mega 2560
   Status : 04.09.2023
   Release : 1.2.0
   Grund : 'Weichen rechts- u. linksabbiegend unterscheiden per Max- u. Min-Werte
*/
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>

//---------------Start PWMC-settings -------------------------------------------------------------
#include <Adafruit_PWMServoDriver.h>

Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver(0x40); //setup the board address 0
Adafruit_PWMServoDriver pwm2 = Adafruit_PWMServoDriver(0x41); //setup the board address 0
#define SERVOMIN  150 // This is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX  600 // This is the 'maximum' pulse length count (out of 4096)
#define SERVOCENTER 370 // = 0 degrees ?

//#define SERVOMIN_SW  350 // This is the 'minimum' pulse length count (out of 4096)
//#define SERVOMAX_SW  485 // 45 degrees

#define SERVOMIN_SW  100 // This is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX_SW  400 // 45 degrees

#define SERVOMIN_SW_LA  100 // This is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX_SW_LA  400 // 45 degrees

#define SERVOMIN_SW_RA  400 // This is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX_SW_RA  100 // 45 degrees

#define SW_DIR_TYPE_RIGHT true
#define SW_DIR_TYPE_LEFT false

#define SERVOMIN_FS  340 // This is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX_FS  390 // 10 degrees

//-- try to modify Angle
#define USMIN  100 // This is the rounded 'minimum' microsecond length based on the minimum pulse of 150
#define USMAX  700 // This is the rounded 'maximum' microsecond length based on the maximum pulse of 600

#define SERVO_FREQ 60 // Analog servos run at ~50-60 Hz updates
//#define MAX_CHANNELS 16 maximal verfĂĽgbare Kanaele
#define MAX_CHANNELS_SW 9
#define MAX_CHANNELS_FS 9

uint16_t pulselen = SERVOMIN;
uint16_t my_angle = 50;

uint8_t g1_event = 0;
uint8_t g2_event = 0;

//---------------Ende PWMC-settings -------------------------------------------------------------

// declare the lcd object for auto i2c address location
hd44780_I2Cexp lcd;

// LCD geometry
const int LCD_COLS = 20;
const int LCD_ROWS = 4;
int lcd_status = 0;
lcd_status = 1;   //- makes no sense here , only to test for error-message

have checked this code with 'cppcheckgui.exe' : no error, no warning
in IDE the compile error raises at 'lcd_status = 1' and says
'lcd_status does not name a type'

other settings of variables genarate the same error..

in 'Tools' of IDE processor is correctly set to 'Arduino Mega or Mega2560'..

what happens here ?

Your comment says it: that assignment makes no sense there - that's why you get an error there!

Assignment statements are only allowed within a function body.

This should be within a function

1 Like

in an older version of my code, setting variables worked well without
embedding in a function.. look here the code BEFORE setup-funtion

struct target_objects
{
  uint8_t idx;
  uint8_t object_io_type;
  uint8_t pin_no;
  bool hasPWM_SingleCon;
  String tgt_obj_name;
  bool isEnabled;
  bool isServo;  
};

target_objects tob_list[ANZ_T_OBJECTS];

uint8_t mobj_no = 0;
uint8_t tobj_no_arr[2] = {0,1};

// the setup function runs once when you press reset or power the board
void setup() {
  Serial.begin(9600);

  lcd_status = lcd.begin(LCD_COLS, LCD_ROWS);
  //#Serial.print("Status : " + String(lcd_status));

???

You can provide an initial value when you define the variable. That’s what you do there. But you can’t define the variable and then as a separate statement assign some values unless this is done within a function.

Ie you can’t do that

But you could do

int lcd_status = 1;

But, as @J-M-L said, those are not assignment statements - those are variable definitions with initialisation.

That's why the compiler complained about wanting a type: because the only thing valid at that point is a declaration or definition - and that has to start with a type.

hi all,
many thanks !
have solved by setting values only in setup() and a additional function..
now all works and i can proceede..

have good live..

Have fun

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.