Can we do analog mapping and variable declaration inside a while loop?

i am not getting the correct value of analogRead which is used inside the while function.( value seem to freeze)

due to not getting the correct value, the LED are not getting turned ON.

NOTE: i am using teensy 3.5

is it possible to

while( int angle=analogRead(A8); angle=map(angle,0,1023,0,360);< int ref=analogRead(A9);ref=map(ref,0,1023,0,260)){
Serial.println(angle);
Serial.println(ref);

i tried but it didnt work

2pot.ino (1.15 KB)

while ( int angle = analogRead(A8); angle = map(angle, 0, 1023, 0, 360); < int ref = analogRead(A9); ref = map(ref, 0, 1023, 0, 260))

Can you please explain what the condition is for this while loop ?

allspark_warhead:
i tried but it didnt work

It's not clear what you intended to do but my guess it that is is more like:

  int angle, ref;
  do
  {
    angle = map( analogRead(A8), 0,1023, 0, 360);
    ref = map( analogRead(A9), 0,1023,0,260);
    if (angle < ref)
    {
      Serial.println(angle);
      Serial.println(ref);
    }
  } while (angle < ref);

A do-while loop is like a while loop but the body runs at least once and the test is done last instead of first. That way you get to calculate the values for angle and ref before you try to compare them.

I'm not sure but I think you can move the declarations inside the body:

  do
  {
    int angle = map( analogRead(A8), 0,1023, 0, 360);
    int ref = map( analogRead(A9), 0,1023,0,260);


    if (angle < ref) // This is just so it doesn't print the first time through the loop if the while() would not have run.
    {
      Serial.println(angle);
      Serial.println(ref);
    }
  } while (angle < ref);

If you don't like the do-while, maybe it can be done with a while, but maybe not with the declaration in the conditional:

  int angle, ref;
  while ((angle = map( analogRead(A8), 0,1023, 0, 360))
            <
            (ref = map( analogRead(A9), 0,1023,0,260)))
  {
      Serial.println(angle);
      Serial.println(ref);
  }

i have two potentiometer and one of the pot is connected on motor shaft(for angle measurement) and other is used to give input angle data.

UKHeliBob:

while ( int angle = analogRead(A8); angle = map(angle, 0, 1023, 0, 360); < int ref = analogRead(A9); ref = map(ref, 0, 1023, 0, 260))

Can you please explain what the condition is for this while loop ?

i am trying to map the value to 0 to 360 angle from potentiometer.
if my condition (angle> ref) does match , them keep creating PWM using analogWrite.

I am trying to reverse and forward the motor

Delta_G:
A while loop needs a condition, not a whole block of code. No you cannot do this. You could write a function that does all those things and returns the angle and use that? But you can't just write a whole mini-program as your while loop condition.

i tried the function but it doesnt refresh the value

i am trying to map the value to 0 to 360 angle from potentiometer.
if my condition (angle> ref) does match , them keep creating PWM using analogWrite.

You need to separate the test from the actions carried out while it returns true

while (this_is_true)
  {
    //do this
  }

HERE i am posting the updated code. it is not working either

const int pwmpin= 37;    // please change the analog pin and digital pin 
const int reverse = 13;
int angle=0;
int ref=0;

void setup() {
  Serial.begin(9600);
  pinMode(pwmpin,OUTPUT);
   pinMode(reverse,OUTPUT);
  
}

void loop() {

 do
  {
    int angle = map( analogRead(A8), 0,1023, 0, 360);
    int ref = map( analogRead(A9), 0,1023,0,360);


   // if (angle < ref) // This is just so it doesn't print the first time through the loop if the while() would not have run.
   // {
      Serial.println(angle);
      Serial.println(ref);
      analogWrite(pwmpin,100);// write pwm pulse until
   // }
  } while (angle < ref);








 do
  {
    int angle = map( analogRead(A8), 0,1023, 0, 360);
    int ref = map( analogRead(A9), 0,1023,0,360);


   // if (angle > ref) // This is just so it doesn't print the first time through the loop if the while() would not have run.
   // {
      Serial.println(angle);
      Serial.println(ref);
       digitalWrite(reverse,HIGH);
      analogWrite(pwmpin,100);
   // }
  } while (angle > ref);

  

    analogWrite(pwmpin,0);
    delay(20);




do
  {
    int angle = map( analogRead(A8), 0,1023, 0, 360);
    int ref = map( analogRead(A9), 0,1023,0,360);


    //if (angle == ref) // This is just so it doesn't print the first time through the loop if the while() would not have run.
    //{
      Serial.println(angle);
      Serial.println(ref);
       digitalWrite(reverse,LOW);
      analogWrite(pwmpin,0);
   // }
  } while (angle == ref);





    
    









  
  
    digitalWrite(reverse,LOW);
    analogWrite(pwmpin,0);
}

THIS CODE DOESNT WORK

const int led = 37;    //
const int led2 = 13;
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  delay(3000);
  pinMode(led,OUTPUT);
   pinMode(led2,OUTPUT);

  
  
}

// the loop routine runs over and over again forever:
void loop() {
 


  int sensorValue = analogRead(A8);
  sensorValue = map(sensorValue,0,1023,0,359);
 
   delay(10);
  
  int sensorValue2 = analogRead(A9);
  sensorValue2 = map(sensorValue2,0,1023,0,359);
 delay(10);
 
 
   
    while(sensorValue<sensorValue2){
      analogWrite(led,100);
       Serial.print("          loop1  A8 val            ") ;Serial.println(sensorValue);
       Serial.print(" loop1 A9 val  ") ;Serial.println(sensorValue2);
     
      }  
      analogWrite(led,0);



     
    while(sensorValue>sensorValue2){
    
     digitalWrite(led2,HIGH);
     analogWrite(led,100);
      Serial.print("          loop2  A8 val            ") ;Serial.println(sensorValue);
       Serial.print(" loop2 A9 val  ") ;Serial.println(sensorValue2);
     
     
      }  
      analogWrite(led,0);
       digitalWrite(led2,LOW);
      


}

allspark_warhead:
THIS CODE DOESNT WORK

// the loop routine runs over and over again forever:

void loop() {

int sensorValue = analogRead(A8);
 sensorValue = map(sensorValue,0,1023,0,359);
 
 int sensorValue2 = analogRead(A9);
 sensorValue2 = map(sensorValue2,0,1023,0,359);

while(sensorValue<sensorValue2){
     analogWrite(led,100);
      Serial.print("          loop1  A8 val            ") ;Serial.println(sensorValue);
      Serial.print(" loop1 A9 val  ") ;Serial.println(sensorValue2);
     }  
     analogWrite(led,0);
}

AT LEAST PARTLY BECAUSE YOU FORGOT TO UPDATE sensorValue AND sensorValue2 INSIDE THE LOOP.

const int led = 37;
const int led2 = 13;
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  delay(3000);
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
  int sensorValue = analogRead(A8);
  sensorValue = map(sensorValue, 0, 1023, 0, 359);
  int sensorValue2 = analogRead(A9);
  sensorValue2 = map(sensorValue2, 0, 1023, 0, 359);
  while (sensorValue < sensorValue2) {
    analogWrite(led, 100);
    Serial.print("          loop1  A8 val            ") ; Serial.println(sensorValue);
    Serial.print(" loop1 A9 val  ") ; Serial.println(sensorValue2);
    sensorValue = analogRead(A8);
    sensorValue = map(sensorValue, 0, 1023, 0, 359);
    sensorValue2 = analogRead(A9);
    sensorValue2 = map(sensorValue2, 0, 1023, 0, 359);
  }
  
  analogWrite(led, 0);
  while (sensorValue > sensorValue2) 
  {
    digitalWrite(led2, HIGH);
    analogWrite(led, 100);
    Serial.print("          loop2  A8 val            ") ; Serial.println(sensorValue);
    Serial.print(" loop2 A9 val  ") ; Serial.println(sensorValue2);
    sensorValue = analogRead(A8);
    sensorValue = map(sensorValue, 0, 1023, 0, 359);
    sensorValue2 = analogRead(A9);
    sensorValue2 = map(sensorValue2, 0, 1023, 0, 359);
  }
  
  analogWrite(led, 0);
  digitalWrite(led2, LOW);
}

How do i update the value inside the while loop. i also need to map the ADC value too.

Delta_G:
Define "doesn't work". That's a pretty useless statement that tells us NOTHING. Something happened that didn't meet with your expectations. Describe that.

sensorValue= input potentiometer for angle
sensorValue2= potentiometer for feedback

analogwrite is to generate PWM(pin3)
to reverse the motor(pin2)

allspark_warhead:
How do i update the value inside the while loop. i also need to map the ADC value too.

You could do it exactly the way I did it for you in sketch I posted.

johnwasser:
You could do it exactly the way I did it for you in sketch I posted.

Now values are getting updated.
But not analogWrite function is not working

allspark_warhead:
But not analogWrite function is not working

You told it to set 'led' to ~39% brightness (100/255ths) while the two analog inputs were different ((sensorValue < sensorValue2) or (sensorValue > sensorValue2)). When the two are exactly the same you tell it to set 'led' to 0%. Is it not doing that?!?

Still the code is not updating

const int sen = 37;    // the idea of this program is to use 2 potentiometer . one potentiometer to set the angle by mapppin it to 0-360  while
                        // other potentiometer will give feedback by connecting it to geared motor. the potentiometer value will be mapped from 0-360
const int sen2 = 13;


void setup() {

  Serial.begin(9600);
  delay(3000);
  pinMode(sen, OUTPUT);
  pinMode(sen2, OUTPUT);



}

// the loop routine runs over and over again forever:
void loop() {
  // int sensorValue = analogRead(A8);
  // int sensorValuef = map(sensorValue, 0, 1023, 0, 359);
  //int sensorValue2 = analogRead(A9);
  //int sensorValue2f = map(sensorValue2, 0, 1023, 0, 359);


   int sensorValue=0;          
  int sensorValuef=0;
  int sensorValue2=0;
  int sensorValue2f=0;
  
  delay(10);

  do {
    int sensorValue = analogRead(A8);
    int sensorValuef = map(sensorValue, 0, 1023, 0, 359);
    int sensorValue2 = analogRead(A9);
    int sensorValue2f = map(sensorValue2, 0, 1023, 0, 359);
    Serial.print("          loop1  A8 val            ") ; Serial.println(sensorValuef);
    Serial.print(" loop1 A9 val  ") ; Serial.println(sensorValue2f);
    analogWrite(sen, 100);
  } while (sensorValuef < sensorValue2f);







  analogWrite(sen, 0);

  do {
    int sensorValue = analogRead(A8);
    int sensorValuef = map(sensorValue, 0, 1023, 0, 359);
    int sensorValue2 = analogRead(A9);
    int sensorValue2f = map(sensorValue2, 0, 1023, 0, 359);
    digitalWrite(sen2, HIGH);
    analogWrite(sen, 100);
    Serial.print("          loop2  A8 val            ") ; Serial.println(sensorValuef);
    Serial.print(" loop2 A9 val ") ; Serial.println(sensorValue2f);
  } while (sensorValuef > sensorValue2f);




    analogWrite(sen, 0);
  digitalWrite(sen2, LOW);

  do {
    int sensorValue = analogRead(A8);
    int sensorValuef = map(sensorValue, 0, 1023, 0, 359);
    int sensorValue2 = analogRead(A9);
    int sensorValue2f = map(sensorValue2, 0, 1023, 0, 359);
    analogWrite(sen, 0);
    digitalWrite(sen, LOW);
  } while (sensorValuef == sensorValue2f);

  }
const int sen = 37;
    analogWrite(sen, 100);

What Arduino model are you using? The UNO doesn't have a "Pin 37" and the Pin 37 on the MEGA isn't an analog output pin.

What driver are you using for your gearmotor? How do you set direction? How do you set speed?

// the idea of this program is to use 2 potentiometer . one potentiometer to set the angle by mapppin it to 0-360  while
// other potentiometer will give feedback by connecting it to geared motor. the potentiometer value will be mapped from 0-360
const byte SetpointPotPin = A8;
const byte PositionPotPin = A9;

const int Kp = 17;  // Multiplier to make the motor more responsive.

void setup() {
  Serial.begin(9600);
  delay(3000);
}

void MotorForward(int speed)
{}

void MotorReverse(int speed)
{}

// the loop routine runs over and over again forever:
void loop() {
  int setpoint =  analogRead(SetpointPotPin);
  int position = analogRead(PositionPotPin);

  int error = setpoint - position;

  boolean direction = true; // Forward
  if (error < 0)
  {
    direction = false;  // Reverse
    error = -error;
  }
  int speed = constrain(error * Kp, 0, 255);

  if (direction)
    MotorForward(speed);
  else
    MotorReverse(speed);
}

Here is a sketch that is as close as I can figure to what you want to do. It doesn't include the parts that control the motor driver because I can't figure that out from what you have provided.

johnwasser:
What Arduino model are you using? The UNO doesn't have a "Pin 37" and the Pin 37 on the MEGA isn't an analog output pin.

allspark_warhead:
NOTE: i am using teensy 3.5