电子说
步骤1:材料
Arduino Uno
加速度计模块
LED的
连接线
面包板
USB数据线
步骤2:连接加速度计
加速度计主要有5个端子。
VCC(5V)
接地
X轴
Y轴
Z轴
电源电压和接地由Arduino Uno通过连接线提供。通电后,板上的红色小LED发光,表明模块正在读取X,Y和Z轴的运动。
步骤3:互连LED的
为了控制LED,必须将它们互连到Arduino Uno。负极端子与地面(Arduino的Gnd)共用,正极端子与Arduino的数字引脚相连。
第4步:编写代码
这部分需要一些思考。为了使X,Y和Z轴控制LED的亮度,必须将它们连接到Arduino的Analog引脚。
代码可能类似于此代码-
const int RedPin = 3;
const int GreenPin = 5;
const int BluePin = 6;
const int sensorPinRed = A0;
const int sensorPinGreen = A2;
const int sensorPinBlue = A4;
int var = 1 ;
int minReadingRed = 750;
int maxReadingRed = 0;
int minReadingGreen = 750;
int maxReadingGreen = 0;
int minReadingBlue = 750;
int maxReadingBlue = 0;
int模拟值= 0;
int时间;
int模拟输出;
int闪烁;
无效setup(){
Serial.begin(9600);
Serial.println();
}
void loop(){
while(time 《1000){
时间= millis();
AnalogWrite(RedPin,255);
AnalogWrite(GreenPin,255);
AnalogWrite(BluePin,255);
延迟(1000);
AnalogWrite(RedPin,0);
AnalogWrite(GreenPin,0);
AnalogWrite(BluePin,0);
延迟(10);
}
而(time》 1000 && time 《16000){
time = millis() ;
analogValue = AnalogRead(sensorPinRed);
maxReadingRed = max(analogValue,maxReadingRed);
minReadingRed = min(analogValue,minReadingRed);
Serial.print(“ Analog Red”);
Serial.print(analogValue,DEC);
延迟(20);
analogValue = AnalogRead(sensorPinGreen);
maxReadingGreen = max(analogValue,maxReadingGreen);
minReadingGreen = min(analogValue,minReadingGreen);
Serial.print(“ Analog Green”);
Serial.print(analogValue,DEC);
延迟(20);
analogValue = AnalogRead(sensorPinBlue);
maxReadingBlue = max(analogValue,maxReadingBlue);
minReadingBlue = min(analogValue,minReadingBlue);
Serial.print(“ Analog Blue”);
Serial.println(analogValue,DEC);
延迟(20);
}
while(时间》 16000 &&时间《17000){
时间= millis() ;
analogWrite(RedPin,255);
AnalogWrite(GreenPin,255);
AnalogWrite(BluePin,255);
延迟(1000);
AnalogWrite(RedPin,0);
AnalogWrite(GreenPin,0);
AnalogWrite(BluePin,0);
}
而(var == 1){
minReadingRed = minReadingRed + 0.5 * (maxReadingRed-minReadingRed);
minReadingGreen = minReadingGreen + 0.5 *(maxReadingGreen-minReadingGreen);
minReadingBlue = minReadingBlue + 0.5 *(maxReadingBlue-minReadingBlue);
Serial.println();
Serial.print(“ minReadingRed =”);
Serial.print(minReadingRed,DEC);
Serial.print(“ maxReadingRed =”);
Serial.println(maxReadingRed,DEC);
Serial.print(“ minReadingGreen =”);
Serial.print(minReadingGreen,DEC);
Serial.print(“ maxReadingGreen =”);
Serial.println(maxReadingGreen,DEC);
Serial.print(“ minReadingBlue =”);
Serial.print(minReadingBlue,DEC);
Serial.print(“ maxReadingBlue =”);
Serial.println(maxReadingBlue,DEC);
var = 2;
}
analogValue = AnalogRead(sensorPinRed);
AnalogValue = constrain(analogValue,minReadingRed,maxReadingRed);
Serial.print(“ analogValue Red =”);
Serial.print(analogValue);
AnalogOut = map(analogValue,minReadingRed,maxReadingRed,0,255);
AnalogOut = constrain(analogOut,0,255);
Serial.print(“缩放为”);
Serial.print(analogOut,DEC);
AnalogWrite(RedPin,AnalogOut);
analogValue = AnalogRead(sensorPinGreen);
AnalogValue = constrain(analogValue,minReadingGreen,maxReadingGreen);
Serial.print(“ Green =”);
Serial.print(analogValue);
模拟输出= map(analogValue,minReadingGreen,maxReadingGreen,0,255);
AnalogOut = constrain(analogOut,0,255); Serial.print(“缩放为”);
Serial.print(analogOut,DEC);
analogWrite(GreenPin,AnalogOut);
analogValue = AnalogRead(sensorPinBlue);
AnalogValue = constrain(analogValue,minReadingBlue,maxReadingBlue);
Serial.print(“ Blue =”);
Serial.print(analogValue);
模拟输出= map(analogValue,minReadingBlue,maxReadingBlue,0,255);
AnalogOut = constrain(analogOut,0,255);
Serial.print(“缩放为”);
Serial.println(analogOut,DEC);
AnalogWrite(BluePin,AnalogOut);
延迟(25);
analogValue = AnalogRead(sensorPinRed);
AnalogOut = map(analogValue,minReadingRed,maxReadingRed,0,255);
Serial.print(“ Red =”);
Serial.print(analogValue,DEC);
analogWrite(RedPin,AnalogOut);
analogValue = AnalogRead(sensorPinGreen); AnalogOut = map(analogValue,minReadingGreen,maxReadingGreen,0,255);
Serial.print(“ Green =”);
Serial.print(analogValue,DEC);
analogWrite(GreenPin,AnalogOut);
analogValue = AnalogRead(sensorPinBlue);
模拟输出= map(analogValue,minReadingBlue,maxReadingBlue,0,255);
Serial.print(“ Blue =”);
Serial.println(analogValue,DEC);
AnalogWrite(BluePin,AnalogOut);
delay(25);
步骤5:上传代码!
再次检查所有连接,然后将代码上载到板。现在,您可以通过运动来控制LED的亮度。当倾斜到相应的轴时,不同颜色的LED点亮。
全部0条评论
快来发表一下你的评论吧 !