Array - for storing the first value

I have written a C++ code for my project. I am having trouble storing values in arrays. I have written a code for measuring the value from hardware. For storing first value I have used Array, that is working fine. For example when the program get started, I got initial value lets say 53.6854.

Now I want to take this value into another void for comparing with other values. So according to first value, I am going to check remaining values. I can't fix the constant value because Initial value will change every time.

I want to take that first value to void Motion::Synchronization(QString motiontype) else if(type == 2)

Then according to stored first value I will check remaining value. If that is not equal to that value I will create the other logic to reach that position. For that I need first value. I don't know how to change, or else any other ways to get the first value ? Assist me to solve this issue.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void check(float a)
{
    std::cout << a << std::endl;
}


void Motion::Synchronization(QString type)
{
  if(type == 1){
 }
 else if(type == 2)
{
  delay(20);
  }
}

void Motion::slow()
{
    float n[ ] = {Thread::data};
    check(n[0]);
}
Last edited on
I'm having trouble following your explanation of the problem. Can you give some sample input and what should happen with that input?
Thread::data = 54.7678

Whenever the program get started that time all thread will get started. From main thread I have defined it will go to

void Motion::slow()

so inside

float n[ ] = {Thread::data};

this will read the first value from hardware. After that it will move to

1
2
void Motion::Synchronization(QString type)
{


Then this condition will work continuously

1
2
3
4
 else if(type == 2)
{
 delay(20);
  }


So inside of this also I have written

Thread::data for further value and position identification

Now I want to compare with first value from

void Motion::slow()

I don't know how to take that from here to there. Because values will change every time If I don't have first value, I can't compare. So now I got the first value problem how to take that value from

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Motion::slow()
{
    float n[ ] = {Thread::data}; // from here
    check(n[0]);
}

void Motion::Synchronization(QString type)
{
  if(type == 1){
 }
 else if(type == 2)
{
  // to here
  delay(20);
  }
}
It sounds like you need to share data between methods of the Motion class. That means the data should be a member variable:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Motion {
    float n[1];  // n is a member variable
    void slow();
    void Synchronize(Qstring type);
};

void Motion::slow()
{
    n[0] = Thread::data;
    check(n[0]);
}

void Motion::Synchronization(QString type)
{
  if(type == 1){
 }
 else if(type == 2)
{
  // to here
  doSomething(n);
  delay(20);
  }
}
Topic archived. No new replies allowed.