The tone problem

Hi
I have a beep from Uy but I need it from Uythr





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

}

void loop() {
  int Ux = analogRead(PA0);
  int Uy = analogRead(PA1);
  
//++++++++++++++++++  
int Uythr = Uy;  // ????????????????????????????????????????????????
//+++++++++++++++++++ 

  Serial.print(" Ux =  ");
  Serial.print(Ux);
  Serial.print("  Uy=  ");
  Serial.print(Uy);

  if (Ux > 2040)       //
  {
    Serial.print("     Uythr =  ");
    Serial.println(Uythr);
  }
  else
  {
    Serial.print("  Uythr =  ");
    Serial.println(10);
  }
    ////////////////
  if( Uythr > 2020 && Uythr < 2030)
  {
   
    digitalWrite(PC13, 0);
    tone (PA3, 4400);
  }
  else
  {
  
    digitalWrite(PC13, 1);
    noTone(PA3);
  }
  ////////////////
  delay (100);
}

Which Arduino are you using?

stm32f103

Good to know, thanks. Now, does analogRead ever return values between 2020 and 2030?

if( Uythr > 2020 && Uythr < 2030)

yes

int Uythr = Uy; 

  if (Ux > 2040)  

those two line need to be somehow combined
int Uythr = Uy if (Ux > 2040) // ??

Excellent!

Should beep in those cases, provided you have something connected to PA3, that actually beeps. But if another value immediately comes along that is NOT in that range, the tone will be shut off.

Try this:

  if( Uythr > 2020 && Uythr < 2030)
  {
   
    digitalWrite(PC13, 0);
    tone (PA3, 4400);
    delay(200);
  }
  if( Uythr > 2020 && Uythr < 2030)
  {
   
    digitalWrite(PC13, 0);
    tone (PA3, 4400);
     delay(200);

There is no difference between those two, they respond the same way

  if( Uy> 2020 && Uy < 2030)
  {
   
    digitalWrite(PC13, 0);
    tone (PA3, 4400);
     delay(200);

There is 2 conditions for Uythr
A . Uythr = Uy
B. A= true if Ux > 2040

Indeed, the two snippets are identical.

Like this?

void setup() 
{
  Serial.begin(115200);
}

void loop() 
{
  int Ux = analogRead(PA0);
  int Uy = analogRead(PA1);
  int Uythr;

  Serial.print(" Ux =  ");
  Serial.print(Ux);
  Serial.print("  Uy=  ");
  Serial.print(Uy);

  if (Ux > 2040)       //
  {
    Uythr = Uy;
    Serial.print("     Uythr =  ");
    Serial.println(Uythr);
  }
  else
  {
    Serial.print("  Uythr =  ");
    Serial.println(10);
  }
  ////////////////
  if ( Uythr > 2020 && Uythr < 2030)
  {

    digitalWrite(PC13, 0);
    tone (PA3, 4400);
  }
  else
  {

    digitalWrite(PC13, 1);
    noTone(PA3);
  }
  ////////////////
  delay (100);
}

Also I don't understand why you have this?

  else
  {
    Serial.print("  Uythr =  ");
    Serial.println(10);
  }

Thanks, it is working.

it is easier to read serial monitor

But it prints "Uythr = 10" when Uythr doesn't equal 10.

yes
Uytr 2

OK :face_with_raised_eyebrow:. Just seems weird to print it as having a value it doesn't have. I don't see the utility in that.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.