首页 > 其他 > 详细

用Tinkercad学arduino之 光线控制彩灯

时间:2020-12-03 12:57:59      阅读:33      评论:0      收藏:0      [点我收藏+]

技术分享图片

const byte O = OUTPUT;
const byte I = INPUT;
const byte H = HIGH;
const byte L = LOW;

const int serialPort = 9600;

const int redLedPin = 11;
const int greenLedPin = 9;
const int blueLedPin = 10;

const int redSensorPin = A0;
const int greenSensorPin = A1;
const int blueSensorPin = A2;

const int mapSensorValueMin = 54;
const int mapSensorValueMax = 974;
const int mapColorValueMin = 0;
const int mapColorValueMax = 255;

int redValue = 0;
int greenValue = 0;
int blueValue = 0;

int redSensorValue = 0;
int greenSensorValue = 0;
int blueSensorValue = 0;

void setup()
{
  Serial.begin(serialPort);
  
  pinMode(redLedPin, O);
  pinMode(greenLedPin, O);
  pinMode(blueLedPin, O);
}

void loop()
{
  redSensorValue = analogRead(redSensorPin);
  delay(5);
  
  greenSensorValue = analogRead(greenSensorPin);
  delay(5);
  
  blueSensorValue = analogRead(blueSensorPin);
  delay(5);
  
  Serial.print("\nRaw Sensor Values \t Red :");
  Serial.print(redSensorValue);
  Serial.print("\t Green:");
  Serial.print(greenSensorValue);
  Serial.print("\t Blue:");
  Serial.print(blueSensorValue);
  
  redValue = map(redSensorValue, mapSensorValueMin, mapSensorValueMax, mapColorValueMin, mapColorValueMax);
  greenValue = map(greenSensorValue, mapSensorValueMin, mapSensorValueMax, mapColorValueMin, mapColorValueMax);
  blueValue = map(blueSensorValue, mapSensorValueMin, mapSensorValueMax, mapColorValueMin, mapColorValueMax);
  
  analogWrite(redLedPin, redValue);
  analogWrite(greenLedPin, greenValue);
  analogWrite(blueLedPin, blueValue);
  
  delay(300);
}

 

用Tinkercad学arduino之 光线控制彩灯

原文:https://www.cnblogs.com/meetrice/p/14078788.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!