Post topics, source code that relate to the Arduino Platform

User avatar
By Thomas_Austria
#86211 Hello!

For a Lightsaber-Project i want to put Sliders on the Web-Page to control the Color.
One for red, one for green and one for blue.
What's the code to create and read them?

Thanks!


Here's the rest of the code:
Code: Select all#include <Adafruit_NeoPixel.h>
#define PIN        5

#define NUMPIXELS 8

Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

#define DELAYVAL 50// Time (in milliseconds) to pause between pixels

int run = 0;
bool SaberOnOff = false;

#include <ESP8266WiFi.h>
const char* ssid     = "JFI";
const char* password = "WLANFischer";
WiFiServer server(80);
String header;

const int Resetbtn = 14;
const int ChangeClr = 4;

int ClrIndex = -1;
int Rot = 0;
int Gruen = 0;
int Blau = 255;

unsigned long currentTime = millis();
unsigned long previousTime = 0;
const long timeoutTime = 5000;

void setup() {
    Serial.begin(115200);
   pinMode(Resetbtn, INPUT);
   pinMode(ChangeClr, INPUT);
  pixels.begin();
   pixels.clear();
   
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
  delay(500);
  Serial.print(" waiting ");
  }
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  server.begin();
   
}

void loop() {
 
 WiFiClient client = server.available();
  pixels.clear(); // Set all pixel colors to 'off'

   client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();
             client.println("<!DOCTYPE html><html>");
            client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
            client.println("<link rel=\"icon\" href=\"data:,\">");
            // CSS to style the on/off buttons
            // Feel free to change the background-color and font-size attributes to fit your preferences
            client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
            client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");
            client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
            client.println(".button2 {background-color: #77878A;}</style></head>");
           
            // Web Page Heading
            client.println("<body><h1>Laserschwert</h1>");
           
  if(run==0){
    for(int i=0; i<NUMPIXELS; i++) {
 
      pixels.setPixelColor(i, pixels.Color(Rot, Gruen, Blau));
      pixels.show();
      delay(40);
    }
    SaberOnOff = true;
    }
   
 
  if(run==1){
    for(int i=NUMPIXELS; i>=0; i--) {
      pixels.fill(pixels.Color(0, 0, 0), i-1, 7);
      pixels.show();
      delay(40);
     
    }
    SaberOnOff = false;
  }
  if (digitalRead(Resetbtn)==HIGH){
    if(SaberOnOff ==false){
      run = 0;
    }
     if(SaberOnOff ==true){
      run = 1;
    }
  }
  if (digitalRead(Resetbtn)==LOW){
    run = 2;
  }
  if (digitalRead(ChangeClr)==HIGH&&SaberOnOff==true){
    ClrIndex++;
    if (ClrIndex>=4){
      ClrIndex=0;
    }
    if (ClrIndex==0){
      Rot = 255;
      Gruen = 0;
      Blau = 0;
      pixels.fill(pixels.Color(Rot, Gruen, Blau), 0, 8);
      pixels.show();
      Serial.println("Color set to red");
    }
    if (ClrIndex==1){
      Rot = 0;
      Gruen = 255;
      Blau = 0;
      pixels.fill(pixels.Color(Rot, Gruen, Blau), 0, 8);
      pixels.show();
      Serial.println("Color set to green");
    }
    if (ClrIndex==2){
      Rot = 0;
      Gruen = 0;
      Blau = 255;
      pixels.fill(pixels.Color(Rot, Gruen, Blau), 0, 8);
      pixels.show();
      Serial.println("Color set to blue");
    }
    if (ClrIndex==3){
      Rot = 255;
      Gruen = 255;
      Blau = 255;
      pixels.fill(pixels.Color(Rot, Gruen, Blau), 0, 8);
      pixels.show();
      Serial.println("Color set to white");
    }
    delay(500);
    SaberOnOff =true;
  }
}
User avatar
By RichardS
#86212 I searched this site and found viewtopic.php?f=8&t=11887#p55566

Similar to how I have done it myself...

RichardS