Go Down

Topic: using two LED's at the same time using the Bridge example  (Read 405 times) previous topic - next topic

rcvolley

so im trying to get the modified version I made of the Bridge example to be able to work with multiple LEDs currently its set up so it works with one LED and turns that LED into a PWM

this is the original code

Code: [Select]
#include <Bridge.h>
#include <BridgeServer.h>
#include <BridgeClient.h>

BridgeServer server;

void setup() {
   Bridge.begin();
   server.listenOnLocalhost();
   server.begin();
}
void loop() {
  BridgeClient client = server.accept();
  if (client) {
    process(client);
    Console.println("client connected and will now close");
    client.stop();
  }

  delay(50);
}
void process(BridgeClient client) {
  String command = client.readStringUntil('/');
   if (command == "analog") {
    analogCommand(client);
   }
}

void analogCommand(BridgeClient client) {
  int pin, value;
  pin = client.parseInt();
  if (client.read() == '/') {
    value = client.parseInt();   
    float P = 50; // period of the pulse (dont go below 50)
    float DC = {value/255.00}; // duty cycle
    while(0 < value){
      analogWrite(pin, 255);   
      delay(P*DC);                       
      analogWrite(pin, 0);   
      delay(P*(1-DC));
      //LOOKING FOR a new client
        BridgeClient client = server.accept();
        if (client) {
        process(client);
        Console.println("client connected and will now close");
    client.stop();
    }
    // Send feedback to client
    client.print(F("Pin D"));
    client.print(pin);
    client.print(F(" set to analog "));
    client.print(value);
    client.print("\t");
    client.println(DC);
    }
    String key = "D";
    key += pin;
    Bridge.put(key, String(value));
     }
       
}


and this is the code I was trying to use to make the LEDs work separately I thought using the TELE variable to dedicate a pin number would let me dedicate certain pins to certain LED's but it doesn't turn the LEDs on like is should

Code: [Select]
#include <Bridge.h>
#include <BridgeServer.h>
#include <BridgeClient.h>

BridgeServer server;

void setup() {
   Bridge.begin();
   server.listenOnLocalhost();
   server.begin();
}
void loop() {
  BridgeClient client = server.accept();
  if (client) {
    process(client);
    Console.println("client connected and will now close");
    client.stop();
  }

  delay(50);
}
void process(BridgeClient client) {
  String command = client.readStringUntil('/');
   if (command == "analog") {
    analogCommand(client);
   }
}

void analogCommand(BridgeClient client) {
  int value, tele;
  if (client.read() == '/') {
    value = client.parseInt();   
     if (client.read() == '/') {
    tele = client.parseInt();
    if (tele == 1){
      int pin = 5;
      float P = 50; // period of the pulse (dont go below 50)
    float DC = {value/255.00}; // duty cycle
    while(0 < value){
      analogWrite(pin, 255);   
      delay(P*DC);                       
      analogWrite(pin, 0);   
      delay(P*(1-DC));
      //LOOKING FOR a new client
        BridgeClient client = server.accept();
        if (client) {
        process(client);
        Console.println("client connected and will now close");
    client.stop();
    }
    // Send feedback to client
   
      }
    if (tele == 2){
      int pin = 6;   
    float P = 50; // period of the pulse (dont go below 50)
    float DC = {value/255.00}; // duty cycle
    while(0 < value){
      analogWrite(pin, 255);   
      delay(P*DC);                       
      analogWrite(pin, 0);   
      delay(P*(1-DC));
      //LOOKING FOR a new client
        BridgeClient client = server.accept();
        if (client) {
        process(client);
        Console.println("client connected and will now close");
    client.stop();
    }
     }
  }client.print(F("Pin D"));
    client.print(pin);
    client.print(F(" set to analog "));
    client.print(value);
    client.print("\t");
    client.println(DC);
    }
    String key = "D";
    //key += pin;
    Bridge.put(key, String(value));
}
}
}

ShapeShifter

There are multiple issues with this sketch.

At the beginning of the analogCommand() function, the first thing you do is read the next character from the client string (the URL) and compare it to a slash character. However, this function is called from the process() function if the first token in the URL string was "analog", and this was read in using readStringUntil('/'). The readStringUntil() function will read characters from the client, and copy them to the return value, until the specified character (a slash in this case) has been read or until the end of the input string. When the specified character ('/') has been read, it is not copied to the return value, but it is consumed from the input string. So, inside of analogCommand(), your call to read() is consuming another character from the input string, and checking whether it is a slash. Unless you put two slashes in a row in your URL (ie: yun.local/arduino/analog//255/1) then this check will always fail because the next character to be read at that point is the first character of the value field. Because this test fails, the rest of the analogCommand() function is not processed.

The next issue is that the if (tele == 2) test is inside the block controlled by the if (tele == 1) test. The only way that second block of code is going to be executed is if tele initially has the value 1 so it can perform the pin 5 operations, and then it magically changes to 2 before getting to the second block of code. The way the tele == 2 block is nested inside the tele == 1 block, it can never get executed. Take a careful look at your code, and see how the curly braces balance out - they are not doing what you assume they are.

But I think the biggest issue is what you have inside these two blocks of code: you have a loop that flashes the LED, and makes another call to look for a client connection. I think this is a VERY BAD idea, as you are recursively entering the process() and analogCommand() functions with each client request - you will quickly fill up the stack and crash the sketch. As it is now, each request you make with a value that is not zero will recuse back into these functions and add another context on the stack. The only way to unwind the stack and return from analogCommand() is to send another client request with a zero value, but that will only unwind one level - how ever many requests you made with a non-zero value, you will have to make that many requests with a zero value to get back to where you started.

Whatever you do, DO NOT make another call to accept a client connection while you are processing a client connection. I see what you are trying to do - stay in that LED flasher loop until the value is set to zero - but this is not the way to do it.

I strongly suggest you study up on the "BlinkWithoutDelay" example and understand what it is doing. Also, check out THIS THREAD for ideas about doing multiple things at once. Basically, you want your loop() function to do what it needs to do, and return as quickly as possible. As you have it now, it does not return as long as an LED is flashing. Your loop structure should basically look something like this pseudocode (not real code, it's just to give you an idea):

Code: [Select]
void loop() {

   accept a client connection;
   if (a client connected)
      process(client);

   if (it is time to update the first LED)
      update(first LED);

   if (it is time to update the second LED)
      update(second LED);

}


In this way, processing the client updates some global variables for the LED values. Then, using the concepts from BlinkWithoutDelay, you check to see if it is time to update any of the LEDs, and if so, you change the LED output and update the time when it should be updated again, and then get out.

rcvolley

So I was looking at the example for doing multiple things with the Arduino Yun and I was wondering if you know a way to combine that with the bridge example I'm having trouble with it.

Go Up