Turn on LEDs based on servo angle; check for case while loop in case

Hello,
Im making remote switch and volume control via servo for my amplifier, I also added 7 leds that sweep left when volume goes down and sweep right when volume goes up. Now I have two problems that I cant figure out.
First is how do I read servo angle and light leds according to it.

What I would like to achieve:
Servo at 0 - 25°: Led1 is on
Servo at 26 - 51°: Leds1,2 are on
Servo at 52- 77°: Leds1,2,3 are on
Servo at 78- 103°: Leds12,3,4 are on
Servo at 79 - 104°: Leds1,2,3,4,5 are on
Servo at 105 - 130°: Leds1,2,3,4,5,6 are on
Servo at 131 - 180°: Leds1,2,3,4,5,6,7 are on

I just need some commads to read servo angle and digitalwrite if angle = x.

Now I want to turn on leds when I let go of the volume + or - button and I want them to stay on for lets say 3 seconds.
I know how to turn them off after 3 sec (delay then digitalwrite LOW), no problem.
But how can I turn them on after I release the volume buttons?
How do I make the script to turn on leds when case was 0x10 or 0x11 and it isn`t anymore because I released the button?
I cant just continue with commands after my servo movement, led sweep loop under case(0x10) or case(0x11) because as long as I hold the button leds must sweep and servo must move.

Now the other problem:
Once I release the button I get some delay because of delay commands inside my led sweep code. Nothing wrong, but sometimes when I release the button it still does one led sweep and one servo step. I want to break the loop under case(0x10) and case(0x11) as soon as I release the button.

Thank you

dela vse plus duble ledice.txt (3.38 KB)

What type of servo are you using? Are you programming the servo movement? If so, you KNOW where the servo arm is stopped at.

Paul

Paul_KD7HB:
What type of servo are you using? Are you programming the servo movement? If so, you KNOW where the servo arm is stopped at.

Paul

I˙m using mini servo 180° comand is to move servo for +3 or -3 when ir reciever reads the right HEX value from remote. I dont know where servo arm is because it moves multiple times when I hold the button.

You can use servo.read(). Just be aware that it will tell you what you told the servo to do using servo.write() - the physical servo may not have completed the move yet and it's possible it never will if you're asking for values beyond its range.

0212995500229:
I˙m using mini servo 180° comand is to move servo for +3 or -3 when ir reciever reads the right HEX value from remote. I dont know where servo arm is because it moves multiple times when I hold the button.

So, count the number of + moves and subtract the number of - moves.
Paul

Paul_KD7HB:
So, count the number of + moves and subtract the number of - moves.
Paul

I would, but I dont know how to program it. I dont know the commands. Tried searching on google but I cant find anything. I dont know what to type in statement:
if "what to type here" 0-25 - how to implement value of position...

Just found this:

 [color=#cc6600]if[/color] (myservo.[color=#cc6600][b]read[/b][/color]() >= 90) 
 { 
 [color=#7e7e7e]// turn on the LED[/color]
 [color=#cc6600][b]digitalWrite[/b][/color](myled, [color=#006699]HIGH[/color]);
 }
}

Will try that way.

Even if I count moves it will count moves even when servo is at 0 or 180 already
 I think and if I hold the button when servo is at deadend it will mess the reading up.

Are you saying you move the servo -3 when it is already at zero? Why? Your logic has need of a flow chart.

Paul

Paul_KD7HB:
Are you saying you move the servo -3 when it is already at zero? Why? Your logic has need of a flow chart.

Paul

Sorry I didn`t really know how to approach to this.
I tought that I would need to do seperate command under case(0xxx) that counts every time the case is true.

I would made myself more clear in the previous post, but my first language isnt english and sometimes I dont find right words to express myself.

0212995500229:
What I would like to achieve:
Servo at 0 - 25°: Led1 is on
Servo at 26 - 51°: Leds1,2 are on
Servo at 52- 77°: Leds1,2,3 are on
Servo at 78- 103°: Leds12,3,4 are on
Servo at 79 - 104°: Leds1,2,3,4,5 are on
Servo at 105 - 130°: Leds1,2,3,4,5,6 are on
Servo at 131 - 180°: Leds1,2,3,4,5,6,7 are on

I just need some commads to read servo angle and digitalwrite if angle = x.
....

I went thru ur code but it is not clear to me what you are doing in it (e.g what does 'relay' do?) but you is this what you want to achieve? This code uses the serial monitor as input to receive 'u' or 'd' instead of the IR device.
(compiles, NOT tested!)

#include <IRremote.h>
#include <Servo.h>

Servo myservo;
int ledPin1 = 2;
int ledPin2 = 3;
int ledPin3 = 4;
int ledPin4 = 5;
int ledPin5 = 6;
int ledPin6 = 7;
int ledPin7 = 8;
int ledPin_arr[7] = {ledPin1, ledPin2, ledPin3, ledPin4, ledPin5, ledPin6, ledPin7};
int relay = 12;
int receiver = 11;
IRrecv irrecv(receiver);
decode_results results;
int code = 0;
int x = 0;
int cont = 0;
int volume = 0;
int y = 0;

void light_leds(uint8_t n) {
  for (uint8_t i = 0; i < 7; ++i) {
    digitalWrite(ledPin_arr[i], n);
    n >>= 1;
  }
}

void setup() {
  pinMode(12, OUTPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  pinMode(ledPin6, OUTPUT);
  pinMode(ledPin7, OUTPUT);
  Serial.begin(9600);
  //irrecv.enableIRIn();
  myservo.attach(9);
  myservo.write(0);                  // initialise the servo to position 0
  Serial.println("***Serial Monitor Test***");


}

void loop() {
  if (Serial.available()) {
    char c = Serial.read();
    if (c == 'up') {
      Serial.print("Volume UP! ");
      x = constrain(x + 3, 0, 180);
      Serial.println(x);
    }
    else if (c == 'd') {
      Serial.print("Volume DOWN! ");
      x = constrain(x - 3, 0, 180);
      Serial.println(x);
    }

    myservo.write(x);
    volume = map(x, 0, 180, 0, 100);
  }

  /*
    Servo at <3 : all Leds is off
    Servo at 3 - 25°: Led1 is on
    Servo at 26 - 51°: Leds1,2 are on
    Servo at 52- 77°: Leds1,2,3 are on
    Servo at 78- 103°: Leds1, 2,3,4 are on
    Servo at 79 - 104°: Leds1,2,3,4,5 are on
    Servo at 105 - 130°: Leds1,2,3,4,5,6 are on
    Servo at 131 - 180°: Leds1,2,3,4,5,6,7 are on
  */
  if (x < 3) {
    light_leds(0b00000000);
  }
  else if (x < 26) {
    light_leds(0b00000001);
  }
  else if (x < 52) {
    light_leds(0b00000011);
  }
  else if (x < 78) {
    light_leds(0b00000111);
  }
  else if (x < 79) {
    light_leds(0b00001111);
  }
  else if (x < 105) {
    light_leds(0b00011111);
  }
  else if (x < 131) {
    light_leds(0b00111111);
  }
  else {
    light_leds(0b01111111);
  }
  delay(50);
}

Hope that helps....

sherzaad:
I went thru ur code but it is not clear to me what you are doing in it (e.g what does 'relay' do?) but you is this what you want to achieve? This code uses the serial monitor as input to receive 'u' or 'd' instead of the IR device.
(compiles, NOT tested!)

#include <IRremote.h>

#include <Servo.h>

Servo myservo;
int ledPin1 = 2;
int ledPin2 = 3;
int ledPin3 = 4;
int ledPin4 = 5;
int ledPin5 = 6;
int ledPin6 = 7;
int ledPin7 = 8;
int ledPin_arr[7] = {ledPin1, ledPin2, ledPin3, ledPin4, ledPin5, ledPin6, ledPin7};
int relay = 12;
int receiver = 11;
IRrecv irrecv(receiver);
decode_results results;
int code = 0;
int x = 0;
int cont = 0;
int volume = 0;
int y = 0;

void light_leds(uint8_t n) {
 for (uint8_t i = 0; i < 7; ++i) {
   digitalWrite(ledPin_arr[i], n);
   n >>= 1;
 }
}

void setup() {
 pinMode(12, OUTPUT);
 pinMode(ledPin1, OUTPUT);
 pinMode(ledPin2, OUTPUT);
 pinMode(ledPin3, OUTPUT);
 pinMode(ledPin4, OUTPUT);
 pinMode(ledPin5, OUTPUT);
 pinMode(ledPin6, OUTPUT);
 pinMode(ledPin7, OUTPUT);
 Serial.begin(9600);
 //irrecv.enableIRIn();
 myservo.attach(9);
 myservo.write(0);                  // initialise the servo to position 0
 Serial.println("Serial Monitor Test");

}

void loop() {
 if (Serial.available()) {
   char c = Serial.read();
   if (c == 'up') {
     Serial.print("Volume UP! ");
     x = constrain(x + 3, 0, 180);
     Serial.println(x);
   }
   else if (c == 'd') {
     Serial.print("Volume DOWN! ");
     x = constrain(x - 3, 0, 180);
     Serial.println(x);
   }

myservo.write(x);
   volume = map(x, 0, 180, 0, 100);
 }

/*
   Servo at <3 : all Leds is off
   Servo at 3 - 25°: Led1 is on
   Servo at 26 - 51°: Leds1,2 are on
   Servo at 52- 77°: Leds1,2,3 are on
   Servo at 78- 103°: Leds1, 2,3,4 are on
   Servo at 79 - 104°: Leds1,2,3,4,5 are on
   Servo at 105 - 130°: Leds1,2,3,4,5,6 are on
   Servo at 131 - 180°: Leds1,2,3,4,5,6,7 are on
 */
 if (x < 3) {
   light_leds(0b00000000);
 }
 else if (x < 26) {
   light_leds(0b00000001);
 }
 else if (x < 52) {
   light_leds(0b00000011);
 }
 else if (x < 78) {
   light_leds(0b00000111);
 }
 else if (x < 79) {
   light_leds(0b00001111);
 }
 else if (x < 105) {
   light_leds(0b00011111);
 }
 else if (x < 131) {
   light_leds(0b00111111);
 }
 else {
   light_leds(0b01111111);
 }
 delay(50);
}




Hope that helps....

Hy, I solved this already with :

[ltr][color=#222222] [color=#cc6600]if[/color] (myservo.[color=#cc6600][b]read[/b][/color]() >= 90) 
 { 
 [color=#7e7e7e]// turn on the LED[/color]
 [color=#cc6600][b]digitalWrite[/b][/color](myled, [color=#006699]HIGH[/color]);
 }
}

Thank you[/color][/ltr]