コンテンツにスキップ

203 Color

203をI2Cの端子につなぎます。

ライブラリの取り込み

サンプルコードを実行し、実際に色が取れている事を確認しましょう。

ソースコード

Arduino

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include "FaBoColor_S11059.h"

const char ssid[] = "ESP32AP-AKIRA";
const char pass[] = "11111111";
const IPAddress ip(192,168,0,1);
const IPAddress subnet(255,255,255,0);
WebServer server(80);

FaBoColor faboColor;
uint16_t r,g,b,i;
int RED_MIN = 300;
int RED_MAX = 700;
int GREEN_MIN = 230;
int GREEN_MAX = 700;
int BLUE_MIN = 100;
int BLUE_MAX = 630;

const char header[] PROGMEM = R"=====(
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
</head>
)=====";

const char footer[] PROGMEM = R"=====(
</html>
)=====";

void handleRoot() {
  faboColor.readRGBI(&r, &g, &b, &i);

  if(r<RED_MIN)r=RED_MIN;
  if(g<GREEN_MIN)g=GREEN_MIN;
  if(b<BLUE_MIN)b=BLUE_MIN;

  if(r>RED_MAX)r=RED_MAX;
  if(g>GREEN_MAX)g=GREEN_MAX;
  if(b>BLUE_MAX)b=BLUE_MAX;

  r = map(r,RED_MIN,RED_MAX,0,255);
  g = map(g,GREEN_MIN,GREEN_MAX,0,255);
  b = map(b,BLUE_MIN,BLUE_MAX,0,255);
  String red = "";
  if(r <= 0x0f) {
    red = "0" + String(r,HEX);
  } else {
    red = String(r,HEX);
  }
  String green = "";
  if(g <= 0x0f) {
    green = "0" + String(g,HEX);
  } else {
    green = String(g,HEX);
  }
  String blue = "";
  if(b <= 0x0f) {
    blue = "0" + String(b,HEX);
  } else {
    blue = String(b,HEX);
  }
  String color = "#" + red + green + blue;
  Serial.println(color);

  String result = "";
  result.concat(header);
  result.concat("<body bgcolor=" + color + ">");
  result.concat(color);
  result.concat("</body>");
  result.concat(footer);
  Serial.println(result);
  server.send(200, "text/html", result);
}

void setup()
{
  Serial.begin(115200);
  faboColor.begin();

  WiFi.softAP(ssid,pass);
  delay(100);
  WiFi.softAPConfig(ip,ip,subnet);
  IPAddress serverIP = WiFi.softAPIP();
  server.on("/", handleRoot);
  server.begin();

  Serial.println();
  Serial.print("AccessPoint:");
  Serial.println(ssid);
  Serial.print("IP:");
  Serial.println(serverIP);
}

void loop() {
  server.handleClient();
}

の値を調整して校正します。