Errors in acceleroemter code in Tinkercad

Hello! I am using Tinkercad (an online circuit building website) to build an accelerometer circuit which will store data. The reason for this is to identify when somebody is standing up or lying down at certain times. However I am getting error saying x,y and z inside the void loop cannot be used as functions and I do not understand how to fix that.

The code:

const int x_pin = A0;
const int y_pin = A1;
const int z_pin = A2;



//Volts per G-Force
const float sensitivity = 0.206;


float x[100]; //array of 100
float y[100]; //array of 100
float z[100]; //array of 100


int i = 0;
 
void setup() {
 analogReference(EXTERNAL);
 Serial.begin(9600);
}


void loop() {


 //Read pins and convert to G
 x(i) = (analogRead(x_pin) - 512) * 3.3 / (sensitivity * 1023);
 y(i) = (analogRead(y_pin) - 512) * 3.3 / (sensitivity * 1023);
 z(i) = (analogRead(z_pin) - 512) * 3.3 / (sensitivity * 1023);


 i = i + 1;
 
 //Display acceleration
 Serial.print("x: ");
 Serial.print(x);
 Serial.print("  y: ");
 Serial.print(y);
 Serial.print("  z: ");
 Serial.println(z);


 delay(60000); //take a reading every minute
}

x does not exsist! x[index] does.

x(i) = (analogRead(x_pin) - 512) * 3.3 / (sensitivity * 1023);

Subscripts use [ ] , not ( ).

Please remember to use code tags when posting code

Serial.print(x); Needs more subscript.

I tried replacing x(i) with x , but the code still says x, y and z cannot be used as functions
Also is there anything else I need to change in this line
x = (analogRead(x_pin) - 512) * 3.3 / (sensitivity * 1023);[/color]
And for the second comment how can I add more subscript and whta do I need to add?

We can't see your code.

Don't forget the code tags.

Sorry! I am a beginner and just started using the forum. I have fixed the code tags and now the code should be more clear! Thanks!

You need to post your code as it looks now, after your changes.
In code tags.

This is the new code after the changes I did. I think there are more errors now , but I am not 100% sure.

const int x_pin = A0;
const int y_pin = A1;
const int z_pin = A2;


 
//Volts per G-Force
const float sensitivity = 0.206;


float x[100]; //array of 100
float y[100]; //array of 100
float z[100]; //array of 100


int i = 0;


void setup() {
  analogReference(EXTERNAL);
  Serial.begin(9600);
}


void loop() {
      
  //Read pins and convert to G
  i = i + 1;
  
  x[i] = (analogRead(x_pin) - 512) * 3.3 / (sensitivity * 1023);
  y[i] = (analogRead(y_pin) - 512) * 3.3 / (sensitivity * 1023);
  z[i] = (analogRead(z_pin) - 512) * 3.3 / (sensitivity * 1023);


  //Display acceleration
  Serial.print(x);
  Serial.println(x(i));
  Serial.print(y);
  Serial.println(y(i));
  Serial.print(z);
  Serial.println(z(i));
  
  delay(60000); //take a reading every minute (ms)
}

Serial.print(x); you can't print a whole array like that, only a single element.

think there are more errors now , but I am not 100% sure.

The compiler will tell you.

The complier is telling me I need to declare codeSerial but I do not know how to declare it as. This is the new updated code..

const int x_pin = A0;
const int y_pin = A1;
const int z_pin = A2;


 
//Volts per G-Force
const float sensitivity = 0.206;


float x[100]; //array of 100
float y[100]; //array of 100
float z[100]; //array of 100


int i = 0;


void setup() {
  analogReference(EXTERNAL);
  Serial.begin(9600);
}


void loop() {
      
  //Read pins and convert to G
  i = i + 1;
  
  x[i] = (analogRead(x_pin) - 512) * 3.3 / (sensitivity * 1023);
  y[i] = (analogRead(y_pin) - 512) * 3.3 / (sensitivity * 1023);
  z[i] = (analogRead(z_pin) - 512) * 3.3 / (sensitivity * 1023);


  //Display acceleration
  [codeSerial.print(x);]
  Serial.println(x(i));
  [codeSerial.print(x);]
  Serial.println(y(i));
  [codeSerial.print(x);]
  Serial.println(z(i));
  
  delay(60000); //take a reading every minute (ms)
}

[codeSerial.print(x);] I have no idea what that is supposed to mean, or do, or where it came from.

If you see an error message, please share it

Remove the [ & ] you added.

Serial.println(x(i)) // < WRONG
serial.println(x[i]); // < correct

This is the updated code.

Now the problem is
In function 'void loop()':
32:17: error: no matching function for call to 'print(float [100])'

const int x_pin = A0;
const int y_pin = A1;
const int z_pin = A2;


 
//Volts per G-Force
const float sensitivity = 0.206;


float x[100]; //array of 100
float y[100]; //array of 100
float z[100]; //array of 100


int i = 0;


void setup() {
  analogReference(EXTERNAL);
  Serial.begin(9600);
}


void loop() {
      
  //Read pins and convert to G
  i = i + 1;
  
  x[i] = (analogRead(x_pin) - 512) * 3.3 / (sensitivity * 1023);
  y[i] = (analogRead(y_pin) - 512) * 3.3 / (sensitivity * 1023);
  z[i] = (analogRead(z_pin) - 512) * 3.3 / (sensitivity * 1023);


  //Display acceleration
  Serial.print(x);
  Serial.println(x[i]);
  Serial.print(x);
  Serial.println(y[i]);
 Serial.print(x);
  Serial.println(z[i]);
  
  delay(60000); //take a reading every minute (ms)
}

Lose the print lines that don't have a subscript , like this oneSerial.print(x);

You can't print a whole array like that.

This is the updated code.

This is the new error:
In function 'void loop()':
28:6: error: invalid types 'float[int]' for array subscript
29:6: error: invalid types 'float[int]' for array subscript
34:20: error: invalid types 'float[int]' for array subscript
35:21: error: invalid types 'float[int]' for array subscript
exit status 1

const int x_pin = A0;
const int y_pin = A1;
const int z_pin = A2;


 
//Volts per G-Force
const float sensitivity = 0.206;


float x;
float y;
float z;


int i = 0;


void setup() {
  analogReference(EXTERNAL);
  Serial.begin(9600);
}


void loop() {
      
  //Read pins and convert to G
  i = i + 1;
  
  x[i] = (analogRead(x_pin) - 512) * 3.3 / (sensitivity * 1023);
//  y[i] = (analogRead(y_pin) - 512) * 3.3 / (sensitivity * 1023);
  z[i] = (analogRead(z_pin) - 512) * 3.3 / (sensitivity * 1023);


  //Display acceleration


  Serial.println(x[i]); 
  Serial.println(y[i]);
  Serial.println(z[i]); 
  delay(60000); //take a reading every minute (ms)
}
float x;
float y;
float z;

You changed these lines - WHY? >:(

x, y and z are not arrays. So don't use [i] when printing or assigning values to them.

Complete - untested code.

const int x_pin = A0;
const int y_pin = A1;
const int z_pin = A2;
 
const float sensitivity = 0.206;

float x[100];
float y[100];
float z[100];

int i = 0;

void setup()
{
  analogReference(EXTERNAL);
  Serial.begin(9600);
}

void loop()
{
  i = i + 1;
  x[i] = (analogRead(x_pin) - 512) * 3.3 / (sensitivity * 1023);
  y[i] = (analogRead(y_pin) - 512) * 3.3 / (sensitivity * 1023);
  z[i] = (analogRead(z_pin) - 512) * 3.3 / (sensitivity * 1023);

  Serial.println(x[i]); 
  Serial.println(y[i]);
  Serial.println(z[i]); 
  if(i == 100)
  {
    Serial.print("Complete!");
    while(1); // This will stop here forever.
  }
  delay(60000); //take a reading every minute (ms)
}

int i = -1; :wink:

The code works well! Thank you for that! Just wanted to ask is it storing the data that is being recorded? The whole point of using the accelerometer is to determine if a person is laying down or not so I am looking at x , y and z coordinates and will add to that so the code knows when a person is laying down. When someone lays down, there should be no acceleration horizontally meaning x and y should be 0. So to do this the machine needs to be trained to understand what I am trying to achieve.