2 years, 3 months ago.

My pc.readable() code is not working properly?

I am working with accelerometer and in that code, I want to update the code by asking from the user about input. I made one function to implement the code but that function is not running properly. this is my code. void value_ADXL() { if(pc.readable()) { pc.printf("enter input"); t = pc.getc(); if(t=='2') { float b[3] = {0.11216024447519345, 0.11216024447519345, 0}; } } } I have declared the variable 'b' as global variable but the main function is not using this value Please suggest me what should I do? I am also confused that should I use MODEL SERIAL to complete the action.

1 Answer

2 years, 3 months ago.

 void value_ADXL() { 
  if(pc.readable()) {
     pc.printf("enter input");
// replace this line
//     t = pc.getc();
// with this
     int t = pc.getc();
     if(t=='2') {
//      replace this line
//        float b[3] = {0.11216024447519345, 0.11216024447519345, 0}; 
//     with this
         b[0] = 0.11216024447519345;
         b[1] =  0.11216024447519345; 
         b[2] = 0;

b may be declared as global somewhere else but your code was creating a new local variable called b and then setting that value without ever changing the value of the global b.

Also t is only used locally, it should be defined as local so that you don't accidentally change the value of some other variable called t elsewhere in your code.

And as a general style note - use variable names longer than 1 letter. It makes the code a lot more readable. a couple of seconds longer typing in a sensible name can save lots of time debugging later.

Finally use

Your code

when posting code so that it's readable.

Accepted Answer

Thanks a lot, Andy.

posted by Nivedita Singh 17 Jul 2019