Add slider to control led intensity [ESP]

View previous topic View next topic Go down

Add slider to control led intensity [ESP]

Post by Vicc on Tue Apr 18, 2017 11:48 am

Hi,
I'm trying to control the led with roboremo and I need to add slider which controls led intensity and a button. I borrowed button's code (c) from one of roboremo projects but I cannot understand how to add a slider (in code) which controls ledIntensity value from 0 to 255. Any simple example?

I'm using ESP-07 for that and uploading the code via ArduinoIDE with UART adapter.

Here's the code:
Code:
// Disclaimer: Don't use RoboRemo for life support systems
// or any other situations where system failure may affect
// user or environmental safety.

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <Servo.h>

// config:

const char *ssid = "mywifi";  
const char *pw = "qwerty123";
IPAddress ip(192, 168, 0, 1);
IPAddress netmask(255, 255, 255, 0);
const int port = 9876;


int ledOn = 0;
int ledIntensity = 50;


WiFiServer server(port);
WiFiClient client;


char cmd[100]; // stores the command chars received from RoboRemo
int cmdIndex;
unsigned long lastCmdTime = 60000;
unsigned long aliveSentTime = 0;


boolean cmdStartsWith(const char *st) { // checks if cmd starts with st
  for(int i=0; ; i++) {
    if(st[i]==0) return true;
    if(cmd[i]==0) return false;
    if(cmd[i]!=st[i]) return false;;
  }
  return false;
}



// commands:
// l = ledOn
// s = stop




void exeCmd() { // executes the command from cmd

  lastCmdTime = millis();
  
  if( cmdStartsWith("l") ) {
    analogWrite(ledOn, ledIntensity);
}

  if( cmdStartsWith("s") ) {
    analogWrite(ledOn, 0);
}

}

void setup() {


  pinMode(ledOn, OUTPUT);
  analogWrite(ledOn, 0);
  delay(1000);

  cmdIndex = 0;

  WiFi.softAPConfig(ip, ip, netmask); // configure ip address for softAP
  WiFi.softAP(ssid, pw); // configure ssid and password for softAP

  server.begin(); // start TCP server
}

void loop() {

  // if contact lost for more than half second
  if(millis() - lastCmdTime > 500) {
    // stop car:
    analogWrite(ledOn, 0);
  }
  
  if(!client.connected()) {
    client = server.available();
    return;
  }

  // here we have a connected client

  if(client.available()) {
    char c = (char)client.read(); // read char from client (RoboRemo app)

    if(c=='\n') { // if it is command ending
      cmd[cmdIndex] = 0;
      exeCmd();  // execute the command
      cmdIndex = 0; // reset the cmdIndex
    } else {      
      cmd[cmdIndex] = c; // add to the cmd buffer
      if(cmdIndex<99) cmdIndex++;
    }
  }

  if(millis() - aliveSentTime > 500) { // every 500ms
    client.write("alive 1\n");
    // send the alibe signal, so the "connected" LED in RoboRemo will stay ON
    // (the LED must have the id set to "alive")
    
    aliveSentTime = millis();
    // if the connection is lost, the RoboRemo will not receive the alive signal anymore,
    // and the LED will turn off (because it has the "on timeout" set to 700 (ms) )
  }

}

Vicc

Posts : 7
Join date : 2016-06-07

View user profile

Back to top Go down

Re: Add slider to control led intensity [ESP]

Post by Vicc on Tue Apr 18, 2017 4:45 pm

I figured out that these lines
Code:
 if( cmdStartsWith("ch") ) {
    int ch = cmd[2] - '0';
    if(ch>=0 && ch<=9 && cmd[3]==' ') {
      chVal[ch] = (int)atof(cmd+4);
      if(!servoCh[ch].attached()) {
        servoCh[ch].attach(chPin[ch], usMin, usMax);
      }  
      servoCh[ch].writeMicroseconds(chVal[ch]);
    }
  }
from this project code are responsible for receiving data values from roboremo slider id'ed "ch0", but how to integrate them below the button, I mean, how to do, that I set the slider and push the button and led is on my set intensity?

Vicc

Posts : 7
Join date : 2016-06-07

View user profile

Back to top Go down

Re: Add slider to control led intensity [ESP]

Post by Admin on Tue May 09, 2017 7:27 pm

Hi,
Here is that part of the code
Code:
int ledIntensity = 0;

void exeCmd() { // executes the command from cmd

  if( cmdStartsWith("l") ) {
    analogWrite(ledPin, ledIntensity);
   }

  if( cmdStartsWith("s") ) {
    analogWrite(ledPin, 0);
   }
   
   if( cmdStartsWith("ch0 ") ) {
     ledIntensity = (int)atof(cmd+4);
      analogWrite(ledPin, ledIntensity);
   }
}
(not tested, but I hope you get the idea)

Sorry for replying so late.
All the best,
Victor

Admin
Admin

Posts : 111
Join date : 2016-03-08

View user profile http://roboremo.makeforum.net

Back to top Go down

Re: Add slider to control led intensity [ESP]

Post by Admin on Tue May 09, 2017 7:28 pm

You may also get inspiration from this project:
http://www.roboremo.com/ac-lamp-dimmer.html

Admin
Admin

Posts : 111
Join date : 2016-03-08

View user profile http://roboremo.makeforum.net

Back to top Go down

Re: Add slider to control led intensity [ESP]

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum