# PWM dimmer LED Android

**URL:** <https://discourse.processing.org/t/pwm-dimmer-led-android/25906>\
**Category:** Processing for Android\
**Tags:** homework\
**Created:** [December 2, 2020, 10:51pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906 "2020-12-02T22:51:12Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![r0x15](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@r0x15](https://discourse.processing.org/u/r0x15)\
**Post date:** [December 2, 2020, 10:51pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/1 "2020-12-02T22:51:12Z")

</div>

Who know one code on android mode to dimmer led to arduino bluetooth ?

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [January 13, 2021, 3:47pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/2 "2021-01-13T15:47:22Z")

</div>

See this code:

> <https://github.com/EmmanuelPil/Android-java-code-utilities-widgets-for-Processing-for-Android/blob/master/Android%20Arduino%20Bluetooth%20example>

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [January 13, 2021, 6:02pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/3 "2021-01-13T18:02:36Z")

</div>

@glv Please instead of flagging or retagging to homework explain your objection to releasing this code.

---

<div class="post-metadata">

**Author:** ![r0x15](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@r0x15](https://discourse.processing.org/u/r0x15)\
**Post date:** [January 13, 2021, 7:17pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/4 "2021-01-13T19:17:01Z")

</div>

Thanks Noel  
for the moment my problem is:  
Remember the code of the circle with degrees and steps, which is moved by an encoder?  
here the problem is that the code when it runs on pc  
in java mode it works well while in android mode the same code of the circle does the flickering  
would you know how i could fix it?  
you see the code in old places  
thanks

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [January 17, 2021, 3:43pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/5 "2021-01-17T15:43:55Z")

</div>

> [@r0x15](#):
>
> while in android mode the same code of the circle does the flickering

Sorry for the late reply.  
Do you mean [this code](https://discourse.processing.org/t/best-way-to-emulate-a-rotary-encoder-wheel/21982/23) by @jeremydouglass and @raron ?  
I tested just with one knob and didn’t see any flickering.

```auto
// Rotary Encoder demo
// Code by @raron and @kerremydouglass

HwRotaryEncoder enc;
float scrollWheel = 0.0;

void setup() {
  fullScreen();
  ellipseMode(RADIUS);
  enc = new HwRotaryEncoder(width/2, height/2, width/3, 8);
}

void draw() {
  background(0, 0, 80);
  fill(0);
  enc.getDirection();
  enc.update();
  enc.display();
}

class HwRotaryEncoder {
  PVector center;
  PVector startDragPos;
  PVector currentPos;
  float encSize, slice;
  float rotation, oldRotation;
  int quadsteps, quadIndex, oldQuadIndex;
  int direction;
  boolean encA, encB, oldEncA, oldEncB;
  boolean active, mouseButtonWasPressed;
  color knobColor;

  HwRotaryEncoder(float w, float h, float encSize, int stepsPerRev) {
    this.center = new PVector(w, h);
    this.startDragPos = new PVector(0, w);
    this.currentPos = new PVector(0, w);
    this.encSize = encSize;
    this.quadsteps = stepsPerRev*4; // internal counts per step (quadrature encoder)
    this.slice = TWO_PI/this.quadsteps;
    this.direction = 0;
    this.rotation = 0;
    this.oldRotation = 0;
    this.oldEncA = false;
    this.oldEncB = false;
    this.active = false;
  }

  boolean mouseInside() {
    if (currentPos.mag() < encSize) return true;
    return false;
  }

  void update() {
    currentPos = new PVector(mouseX, mouseY).sub(center);
    if (mousePressed == false) {
      // a mouse button is not pressed
      if (active) {
        active = false;
        oldRotation = rotation;
      }
      mouseButtonWasPressed = false;
    } else {
      // a mouse button is pressed
      if (mouseButtonWasPressed == false) {
        mouseButtonWasPressed = true;
        if (mouseInside()) {
          active = true;
          startDragPos = new PVector(mouseX, mouseY).sub(center);
          oldEncA = false;
          oldEncB = false;
          direction = 0;
        }
      }
    }

    // Mouse drag rotation
    // If moving mouse fast, will probably skip steps or count backwards
    if (active) {
      rotation = currentPos.heading() - startDragPos.heading() + oldRotation;
      if (rotation<0) rotation += TWO_PI;
      if (rotation>TWO_PI) rotation -= TWO_PI;

      int quadIndex = (int)((float)quadsteps * (rotation/TWO_PI));

      if (oldQuadIndex != quadIndex) {
        // Make rotary encoder quadrature pattern
        int pa = (quadIndex/2)%2;
        int pb = ((quadIndex+1)/2)%2;
        if (pa == 1) encA = true; 
        else encA = false;
        if (pb == 1) encB = true; 
        else encB = false;

        // Count when transitioning pattern edges
        if (encB & (encA ^ oldEncA)) {
          if (encA) direction = 1; 
          else direction = -1;
        }
        if (!encB & (encA ^ oldEncA)) {
          if (encA) direction = -1; 
          else direction = 1;
        }
        if (encA & (encB ^ oldEncB)) {
          if (encB) direction = -1; 
          else direction = 1;
        }
        if (!encA & (encB ^ oldEncB)) {
          if (encB) direction = 1; 
          else direction = -1;
        }
        oldEncA = encA;
        oldEncB = encB;
        oldQuadIndex = quadIndex;
      }
    }
  }

  int getDirection() {
    int temp = direction;
    direction = 0;
    return temp;
  }

  void display() {
    if (mouseInside() || active) knobColor = color(210); 
    else knobColor = color(192);
    pushMatrix();
    pushStyle();
    translate(center.x, center.y);
    stroke(255);
    for (int i=0; i < quadsteps; i++) {
      if (i == 0) strokeWeight(6); 
      else strokeWeight(3);
      line( encSize*0.9, 0, encSize, 0 );
      rotate(slice);
    }
    fill(knobColor);
    ellipse(0, 0, encSize*6/8, encSize*6/8);
    rotate(rotation);
    stroke(0);
    strokeWeight(6);
    line( 0, 0, encSize*6/8, 0 );
    popMatrix();
    popStyle();
  }
}

```

---

<div class="post-metadata">

**Author:** ![r0x15](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@r0x15](https://discourse.processing.org/u/r0x15)\
**Post date:** [January 18, 2021, 11:29am UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/6 "2021-01-18T11:29:13Z")

</div>

Noel  
Tankyou for the code i evening try ti run  
It’s have one sketch for arduino ?  
Hello

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [January 18, 2021, 12:27pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/7 "2021-01-18T12:27:55Z")

</div>

> [@r0x15](#):
>
> It’s have one sketch for arduino ?

You didn’t say what it is for. Rotation of a stepper motor? What resolution?  
Please post what you have.

---

<div class="post-metadata">

**Author:** ![r0x15](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@r0x15](https://discourse.processing.org/u/r0x15)\
**Post date:** [January 18, 2021, 4:35pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/8 "2021-01-18T16:35:37Z")

</div>

DC Motor  
yes i have 2 encoder  
1: is 200 step for revolution  
or  
1: 1600 step for revolution  
Conncect wire:  
+A B -  
Tankyou

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [January 18, 2021, 4:45pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/9 "2021-01-18T16:45:26Z")

</div>

OK, but where is your Arduino code?

---

<div class="post-metadata">

**Author:** ![r0x15](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@r0x15](https://discourse.processing.org/u/r0x15)\
**Post date:** [January 18, 2021, 8:35pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/10 "2021-01-18T20:35:05Z")

</div>

this my code:

```auto
volatile long temp, counter = 0; //This variable will increase or decrease depending on the rotation of encoder
    
void setup() {
  Serial.begin (9600);

  pinMode(2, INPUT_PULLUP); // internal pullup input pin 2 
  
  pinMode(3, INPUT_PULLUP); // internalเป็น pullup input pin 3
   //Setting up interrupt
  //A rising pulse from encodenren activated ai0(). AttachInterrupt 0 is DigitalPin nr 2 on moust Arduino.
  attachInterrupt(0, ai0, RISING);
   
  //B rising pulse from encodenren activated ai1(). AttachInterrupt 1 is DigitalPin nr 3 on moust Arduino.
  attachInterrupt(1, ai1, RISING);
  }
   
  void loop() {
  // Send the value of counter
  if( counter != temp ){
  Serial.println (counter);
  temp = counter;
  }
  }
   
  void ai0() {
  // ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH
  // Check pin 3 to determine the direction
  if(digitalRead(3)==LOW) {
  counter++;
  }else{
  counter--;
  }
  }
   
  void ai1() {
  // ai0 is activated if DigitalPin nr 3 is going from LOW to HIGH
  // Check with pin 2 to determine the direction
  if(digitalRead(2)==LOW) {
  counter--;
  }else{
  counter++;
  }
  }

```

---

<div class="post-metadata">

**Author:** ![r0x15](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@r0x15](https://discourse.processing.org/u/r0x15)\
**Post date:** [January 18, 2021, 8:48pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/11 "2021-01-18T20:48:11Z")

</div>

when i move encoder the circle this code by @jeremydouglass and @raron not move

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [January 19, 2021, 3:56pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/12 "2021-01-19T15:56:06Z")

</div>

What is your setup?  
Is it something like this?  
[![](https://simple-circuit.com/wp-content/uploads/2018/02/arduino-rotary-encoder-motor-control-circuit.png) ](https://simple-circuit.com/wp-content/uploads/2018/02/arduino-rotary-encoder-motor-control-circuit.png)

---

<div class="post-metadata">

**Author:** ![r0x15](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@r0x15](https://discourse.processing.org/u/r0x15)\
**Post date:** [January 19, 2021, 5:40pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/13 "2021-01-19T17:40:37Z")

</div>

Hello Noel  
No my circuit is some i use mega 2560  
Encoder os digitale encoder : share channel A is Pin and channel B is Pin 3  
And driver motor is some you schematic

---

<div class="post-metadata">

**Author:** ![r0x15](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@r0x15](https://discourse.processing.org/u/r0x15)\
**Post date:** [January 19, 2021, 5:42pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/14 "2021-01-19T17:42:28Z")

</div>

Hello Noel  
No my circuit is some i use mega 2560  
Encoder os digitale encoder : where channel A is Pin 2 and channel B is Pin 3  
And driver motor is some you schematic

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [January 19, 2021, 5:57pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/15 "2021-01-19T17:57:12Z")

</div>

> [@r0x15](#):
>
> And driver motor is some you schematic

Can you give me the name of the driver board?

---

<div class="post-metadata">

**Author:** ![r0x15](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@r0x15](https://discourse.processing.org/u/r0x15)\
**Post date:** [January 19, 2021, 8:22pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/16 "2021-01-19T20:22:30Z")

</div>

driver motor BTS7960 43A

---

<div class="post-metadata">

**Author:** ![r0x15](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@r0x15](https://discourse.processing.org/u/r0x15)\
**Post date:** [January 19, 2021, 8:28pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/17 "2021-01-19T20:28:01Z")

</div>

its some but i have arduino 2560 and i use pin forward 13,bakward 11, encoder to 2,and 3

![kit_driver_bts7960_1](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/a5c85f133ecee82b3df816d539a5897b98c9a5a0.jpeg)

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [January 19, 2021, 9:36pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/18 "2021-01-19T21:36:30Z")

</div>

> [@r0x15](#):
>
> BTS7960 43A

I’m lost here. As far as I can see, the BTS7960 43A is pulse-width modulated where rotation can be reversed and speed controlled. Anyway I have some code for a stepper motor where you can set the exact angle position, but you need a stepper pin and a direction pin.

---

<div class="post-metadata">

**Author:** ![r0x15](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@r0x15](https://discourse.processing.org/u/r0x15)\
**Post date:** [January 19, 2021, 9:39pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/19 "2021-01-19T21:39:18Z")

</div>

I have a DC motor and not step by step, the position of the angle is given by the encoder

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [January 19, 2021, 9:51pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/20 "2021-01-19T21:51:11Z")

</div>

I don’t see how this can be done with a dc motor, because you don’t have reference points to ensure the right angle unless you have a dc motor with an encoder coupled to the axis.

[Next page](https://discourse.processing.org/t/pwm-dimmer-led-android/25906.md?page=2)
