# 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:** 2

<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:53pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/21 "2021-01-19T21:53:57Z")

</div>

yes just like that I have an encoder coupled to the axis

---

<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, 10:02pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/22 "2021-01-19T22:02:11Z")

</div>

Hehe. My friend, the information is coming in slowly. 😃 How could I know?  
I need to know how this is wired to proceed. The android code to command the angle over Bluetooth I already, have, but I need to know the details to write the Arduino code. (if possible). Maybe you could manually design it.

---

<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, 10:22pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/23 "2021-01-19T22:22:15Z")

</div>

I’ll try to put down the wiring diagram  
Thanks

 ![ard_meg](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/7/7272acf032040709c0c9f52f1c45bf822d3ad235.jpeg)  
 ![20210119_232442](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/ad4dd3d3d73c51c31ab08ca2a98ea6b4baac5962.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 20, 2021, 9:59am UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/24 "2021-01-20T09:59:57Z")

</div>

Let’s recapitulate this topic because I think I misunderstood the whole thing.

- 1 You: Remember the code of the circle with degrees and steps, which is moved by an encoder?  
in java mode it works well while in android mode the same code of the circle does the flickering

- 2 You: DC Motor yes i have 2 encoder

- 3 You: this my code:===

Now that I think I have a working Arduino code to control the stepper motor, I will post it here anyway. I haven’t tested it yet, because at the moment I have no stepper at my disposal. So if anyone wants to try it, will do so at his own risk. This code expects to receive the angle (0 to 360) via serial.

> **Arduino stepper control code.**
>
> ```auto
> const int stepperPin = 7;
> const int directionPin = 6;
> String state = "";
> int current_state = 0;
> int current_angle = 0;
> int last_angle = 0;
> int angle = 0;
> int rotatation = 0;
> boolean dirRotation = HIGH;
> int revolution = 200;
> 
> void setup() {
> pinMode(stepperPin, OUTPUT);
> pinMode(directionPin, OUTPUT);
> Serial.begin(38400); 
> }
> void loop() {
> delayMicroseconds(1);
> if (Serial.available() > 0) { 
> state = Serial.readString(); 
> }
> current_state = state.toInt();
> //Serial.println(state);
> current_angle = map(current_state, 0, 359, 0, revolution);
> digitalWrite(directionPin, HIGH); 
> if (current_angle != last_angle) {
> if (current_angle > last_angle) {
> rotatation = current_angle-last_angle;
> for (int x = 0; x < rotatation; x++) {
> digitalWrite(stepperPin, HIGH);
> delayMicroseconds(500);
> digitalWrite(stepperPin, LOW);
> delayMicroseconds(500);
> }
> }
> if (current_angle < last_angle) {
> rotatation = last_angle-current_angle;
> digitalWrite(directionPin, LOW); 
> for (int x = 0; x < rotatation; x++) {
> digitalWrite(stepperPin, HIGH);
> delayMicroseconds(500);
> digitalWrite(stepperPin, LOW);
> delayMicroseconds(500);
> }
> }
> }
> last_angle = current_angle;
> }
> }
> 
> ```

---

<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 20, 2021, 7:47pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/25 "2021-01-20T19:47:50Z")

</div>

Hi Noel  
yes in fact, I rotate the motor then the encoder on the same axis and consequently the needle of the circle must move on android,  
on java; yes it does and transmits via serial (maybe I will not have posted the part of the serial)  
however I want to clarify that everything works with dc motor and not step by step  
so the code you posted above i believe it is not for my engine but for step by step  
let me know 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 20, 2021, 7:58pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/26 "2021-01-20T19:58:26Z")

</div>

OK. So if you post the serial code that you use to send data to the pc processing sketch, I can write the Android receiver code. I need to know how you are sending your data.

---

<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 20, 2021, 9:53pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/27 "2021-01-20T21:53:44Z")

</div>

```auto
import processing.serial.*;
Serial myPort; 
String myString = null;
int incoming_position = 0;
float position_in_degree = 0;
PFont font18,font63;

void setup(){
  size(800, 600);
  font18 = loadFont("Tahoma-18.vlw");
  font63 = loadFont("Tahoma-63.vlw");
  
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 9600);
}

void draw(){
  background(100);
  textFont(font63, 36);
  text("RAW Position:", 30, 510);
  text(incoming_position, 270, 510);
  text("Position in Degree:", 30, 550);
  text(position_in_degree, 330, 550);
  draw_angle(400,250,200,position_in_degree); // draw circle and encoder position
  
  // Reading Serial Port when buffer not zero
while (myPort.available() > 0) {
    myString = myPort.readStringUntil(10); // Read Data from Serial until line feed (10)
    if (myString != null) {
      myString = trim(myString); // Remove Line Feed
      incoming_position = int(myString); // String to int
      position_in_degree = map(incoming_position,0, 1023, 0,359.999); // translate raw position in to 360 degree position format
      println(position_in_degree);
    }
  }
  
}

void draw_angle(int x, int y,int size, float angle_dec){
  noFill();
  stroke(255,255,255,255);
  // X,Y plane Translation
  float end_x,end_y;
  float angle_rad = radians(angle_dec);
  end_x = x + cos(angle_rad)*size; 
  end_y = y + sin(angle_rad)*size;
  // mark angle guage in circle
  line(x,y,end_x,end_y);
  
  int space = 20;
  int space2 = size - space;
  ellipse(x, y, size*2+space, size*2+space);
  
  // mark small tick on cicle every 15 degree
  for(int i = 0; i < 360; i = i +15){ 
    float rad1 = radians(i);
    // mark large scale on 0 90 180 270 degree
    if( i == 0 || i == 90 || i == 180 || i == 270){ 
      line(x + cos(rad1)*space2,y + sin(rad1)*space2,x + cos(rad1)*(size+space),y + sin(rad1)*(size+space));
    }else{
      line(x + cos(rad1)*space2,y + sin(rad1)*space2,x + cos(rad1)*size,y + sin(rad1)*size);
    }
  }
  
}

```

---

<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 21, 2021, 6:01am UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/28 "2021-01-21T06:01:49Z")

</div>

> [@noel](#):
>
> OK. So if you post the serial code that you use to send data to the pc processing sketch

. . . . .I mean the Arduino serial code that you use to send data to the pc processing sketch

Edit. Looking at this code in this line:

> position\_in\_degree = map(incoming\_position,0, 1023, 0,359.999);

Your Arduino code is sending strings from number 0 to 1023 (to give the angle), so that’s enough as well. Tonight I will post the 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 21, 2021, 2:09pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/30 "2021-01-21T14:09:23Z")

</div>

the data from 0 to 1023 are sent by the encoder through the sketch I posted to you some post above

---

<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 21, 2021, 2:19pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/31 "2021-01-21T14:19:42Z")

</div>

I understood this already, therefore I edited my post soon after posting.I have it already working nicely. After lunch ( and a nap 😉 ), I will post it.

---

<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 21, 2021, 2:27pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/32 "2021-01-21T14:27:21Z")

</div>

226 / 5000

## Risultati della traduzione

noel I would need to be able to set the position of the motor example say go to position example 300 (then the program reads the encoder steps and the mortor moves to position 300 Could you give me info?

---

<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 21, 2021, 3:42pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/33 "2021-01-21T15:42:29Z")

</div>

The Ketai lib can’t handle fast incoming data, so I wrote a Bluetooth code that does.  
I posted that code [here](https://discourse.processing.org/t/android-bluetooth/27140/14) and added also the code you requested as an example for others as well.  
I kept your design, because I think it’s nice, but re-wrote the code as a class to easily construct multiple knobs if needed. Don’t forget to give the needed permissions.

---

<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 21, 2021, 4:21pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/34 "2021-01-21T16:21:27Z")

</div>

> [@r0x15](#):
>
> noel I would need to be able to set the position of the motor example say go to position example 300 (then the program reads the encoder steps and the motor moves to position 300 Could you give me info?

But that was my first understanding of your request because it seemed more obvious to me that you want to control your motor, and not only visualize the axis position. That is where the Arduino code I wrote above is for (stepper) Of course you can do this as well with the dc motor since you have an encoder coupled to the axis. The picture shows me that you have a slider, I assume it’s for the speed control, but without seeing the whole Arduino code I can’t say much. It also depends on what the motor is connected to, because you are using a more potent motor, and together with the load they can generate high kinetic energy so that you need to decrease the velocity before the actual stopping.

---

<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 21, 2021, 5:00pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/36 "2021-01-21T17:00:06Z")

</div>

> [@r0x15](#):
>
> void moveMotorRight()

Did you have this already in your 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 21, 2021, 6:30pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/37 "2021-01-21T18:30:34Z")

</div>

no, I have a good code but it is for python and runs under windows with arduino and honestly I don’t like this os so much but how it works is good and malleable in everything only that should be modified for android

[image]

---

<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 23, 2021, 10:24am UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/38 "2021-01-23T10:24:45Z")

</div>

Noel where you ?  
i need help

---

<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 23, 2021, 1:53pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/39 "2021-01-23T13:53:50Z")

</div>

I will not answer any questions anymore because I’m leaving this forum unless my request being accepted. My ego just can’t accept an unjustifiable accusation. You were pinged at did not react. I am very disappointed.

---

<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 23, 2021, 1:58pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/40 "2021-01-23T13:58:16Z")

</div>

(You received a ping and did not react. I am very disappointed.) **what does it mean**

But what do you say I don’t know what a ping is  
also I always have to use google to translate so everything is new to me  
so explain to me what happened as I no longer receive posts regarding my requests

Anyway I wanted to tell you that I tried your code for the circle and it works great with my 200 step encoder for rotation

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [January 23, 2021, 5:05pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/41 "2021-01-23T17:05:16Z")

</div>

@noel

Please don’t take things that happen here too seriously. Life is short. Personally, I think you do a great job and are the main reason I hang around over here.

With regard to the initial reason for this thread (which was a flicker of the encoder gauge) I believe the problem was on the Android end when the serial bluetooth data was reconstructed. The data was not being perfectly parsed and sometimes gave spurious results, which gave the flicker when plotted. The demo that you posted (using Android bluetooth code) appears to have solved the problem. It needs further testing but it appears to work as far as stopping the flicker (due to better parsing code). Thanks for the post.

---

<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 23, 2021, 5:32pm UTC](https://discourse.processing.org/t/pwm-dimmer-led-android/25906/42 "2021-01-23T17:32:43Z")

</div>

Hello Svan  
?  
I don’t understand what I have done something wrong or did something wrong? let me know … then the ping issue that I don’t understand  
Thanks

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

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