HOW DO I: Print when there is a change in value only. ARDUBLOCK

I am trying to make a remote which controls a motor speed based on the angle of an accelerometer. I am using ArduBlock as I am a newbie to programming. I have it printing out the angle of the sensor, and sending the proper signal to the motor board, but want to pause the printing unless there has been a change in the angle. I want to avoid the scroll. I would also like to take the ‘delay’ command out if there is a way to do so.

How do I make it print the received value ONLY if it is different from the previously received value while still ensuring the control voltage is fed to the motor board?

Thank you for your help!

int _ABVAR_1_XAxis = 0 ;

void setup()
{
  pinMode( 12 , OUTPUT);
  Serial.begin(9600);
  _ABVAR_1_XAxis = 0 ;

}

void loop()
{
  _ABVAR_1_XAxis = constrain( map ( analogRead(1) , 245 , 420 , -90 , 90 )  , -90 , 90 )  ;
  analogWrite(12 , map ( _ABVAR_1_XAxis , -90 , 90 , 0 , 255 ) );
  if (( ( ( _ABVAR_1_XAxis ) >= ( -10 ) ) && ( ( _ABVAR_1_XAxis ) <= ( 10 ) ) ))
  {
    analogWrite(12 , 128);
    Serial.print("Straight");
    Serial.print(" ");
    Serial.println();
    delay( 1000 );
  }
  if (( ( _ABVAR_1_XAxis ) > ( 10 ) ))
  {
    Serial.print("Turning Right");
    Serial.print(" ");
    Serial.print(_ABVAR_1_XAxis);
    Serial.print(" ");
    Serial.println();
    delay( 250 );
  }
  else
  {
    if (( ( _ABVAR_1_XAxis ) < ( -10 ) ))
    {
      Serial.print("Turning Left");
      Serial.print(" ");
      Serial.print(_ABVAR_1_XAxis);
      Serial.print(" ");
      Serial.println();
      delay( 250 );
    }
  }
}

To print it only when it changed, save the "old" value before you read the new value. Compare the two, are they different then you print it.

For the delay(), see Blink without delay :slight_smile:

Awesome. Very helpful. A couple of questions. How do I store the old value? How do I compare the old to the new? How do I tell it to only print out if they are different?
Thanks!
(I don't suppose you could post some sample code?)

Take a look at the 'if' statement:if - Arduino Reference

if(oldValue != newValue)
{
  Serial.println(newValue);
  oldValue = newValue;
}

Time to go through the IDE examples so you learn how to do some basic programming.

.

mrmitee@hotmail.com:
Awesome. Very helpful. A couple of questions. How do I store the old value?

With a variable just like you would store anything else.

int oldValue = currentValue;
currentValue = getNewValue();

mrmitee@hotmail.com:
How do I compare the old to the new?

How do I tell it to only print out if they are different?

How about an if statement. Think that might work?

if(oldValue != currentValue) {
//do something
}

mrmitee@hotmail.com:
Thanks!
(I don't suppose you could post some sample code?)

Why is it that everyone wants us to rewrite all the basic tutorials just special for them. Does nobody go out and try to learn anything on their own? Every time we make a suggestion that could lead someone to a learning experience and a better understanding of writing code, they always just come back with, "won't you just do it for me?"

they always just come back with, "won't you just do it for me?"

"Just say no".