BEGINNERS = Serial.print()

Hello .

Iam still have a problem about Serial.print on servo.
Here my code

#include <Servo.h> 

Servo servoleg1A;
Servo servoleg2A;
Servo servoleg3A;

int posA = 0;
// SERVO LEG A \\\

int LED = 13;


void setup()
{
  servoleg1A.attach(2);
  servoleg2A.attach(3);
  servoleg3A.attach(4);
  
  Serial.begin(9600);

  pinMode(LED, OUTPUT);
}

void loop()
{  
  for ( posA= 0; posA <= 90; posA += 1)
  {
    servoleg1A.write(posA);

    delay(30);
    Serial.println(posA, DEC);
    digitalWrite(LED, HIGH);
    }
  
  
  for ( posA= 90; posA >=0; posA -=1)
  {
    servoleg1A.write(posA);
    delay(30);
    Serial.println(posA, DEC);
    digitalWrite(LED, LOW);

    if (posA == 90)
    {
      Serial.println("1");
      delay(30);
    }
  }
}

I have put the Serial.println on the end of servo

if (posA == 90)
    {
      Serial.println("1");
      delay(30);
    }
  }

my question is, on Serial.monitor why when servo equal to 90 ( posA == 90)
its always shown 1?
I want when servo equal to 90, its not just shown 1,
i want counting the servo movement, from 1 to 10 times when servo move to 90.
Regards:)

Wait what? You tell it to print "1" if posA == 90 but you think it's weird if that is what happens?

What do you really want? If you want it to print 1 to 10 you need to code that...

@septillion

Sorry sir, i didnt means that was weird.

on "serial monitor" when servo move to 90 its just shown 1 and always 1 when the servo rotates repeatedly.
I want the number 1 to be changed to 2 to 10 when the servo rotates repeatedly.

So when the servo moves between and 90 you want to display 1 to 10?

Something like:

for ( posA= 90; posA >=0; posA -=1)
  {
    servoleg1A.write(posA);
    delay(30);
    Serial.println(posA, DEC);
    digitalWrite(LED, LOW);
    
    if(posA % 10 == 0){
      Serial.println(posA / 10);
    }
  }

nielyay:
@septillion

Sorry sir, i didnt means that was weird.

on "serial monitor" when servo move to 90 its just shown 1 and always 1 when the servo rotates repeatedly.
I want the number 1 to be changed to 2 to 10 when the servo rotates repeatedly.

Do you want the serial monitor to count how many times the servo sweeps from 0 to 90?
Tom... :slight_smile:

Serial.println("1");

It only prints "1" because you toldnit to print "1". If you want it to count then make some variable and have it print that instead. If you want the variable to count up then add one to it in the loop function whenever the servo gets to 90. But when you write this line:

Serial.println("1");

That is only going to ever print a "1" because that's what it says to print.

This will sweep the servo 10 times and report the count after each sweep:

#include <Servo.h>


Servo servoleg1A;
Servo servoleg2A;
Servo servoleg3A;


int posA = 0;  // SERVO LEG A


const int LEDPin = 13;


void setup()
{
  Serial.begin(9600);


  servoleg1A.attach(2);
  servoleg2A.attach(3);
  servoleg3A.attach(4);


  pinMode(LEDPin, OUTPUT);
}


void loop()
{
  //Sweep 10 times
  for (int i = 0; i < 10; i++)
  {
    // Sweep 0 to 90
    digitalWrite(LEDPin, HIGH);
    for ( posA = 0; posA <= 90; posA += 1)
    {
      servoleg1A.write(posA);
      Serial.println(posA, DEC);
      delay(30);
    }


    // Sweep 90 to 0
    digitalWrite(LEDPin, LOW);
    for ( posA = 90; posA >= 0; posA -= 1)
    {
      servoleg1A.write(posA);
      Serial.println(posA, DEC);
      delay(30);
    }


    // Report sweep count
    Serial.println(i + 1);
  }
}

@TomGeorge
Yes sir that was totally what i want:(.. can you help me?

@johnwasser
Thank you somuch sir it very helpful to learn more about Serial.print

@Delta_G
Oh ya sir iam sorry, ya i want when servo move to 90, it just shown "1", but can changes like count how many times the servo sweep from 0 to 90, just like @TomGeorge said. Sorry for bad english:(

Okay, then just print a variable. And increment the variable every time a sweep is completed (aka, you set it to 90 degree).