Python 使用arduino和光电传感器移动电机
所以我开始使用我最近买的Arduino套件,在我开始做更复杂的事情之前,我一直在尝试让马达移动(现在) 我未来的小项目的重点是让Arduino在夜间感应我窗户附近的光线。从那里它将有希望转动一个马达,击中我的闹钟。虽然现在我只是想让马达在看到光的时候移动,当它停止看到光的时候关闭,因为我可以在几秒钟后自动关闭 以下是当前代码:Python 使用arduino和光电传感器移动电机,python,loops,arduino,arduino-uno,Python,Loops,Arduino,Arduino Uno,所以我开始使用我最近买的Arduino套件,在我开始做更复杂的事情之前,我一直在尝试让马达移动(现在) 我未来的小项目的重点是让Arduino在夜间感应我窗户附近的光线。从那里它将有希望转动一个马达,击中我的闹钟。虽然现在我只是想让马达在看到光的时候移动,当它停止看到光的时候关闭,因为我可以在几秒钟后自动关闭 以下是当前代码: const int motorPin = 9; const int sensorPin = 10; int lightLevel, high = 0, low = 102
const int motorPin = 9;
const int sensorPin = 10;
int lightLevel, high = 0, low = 1023;
void setup()
{
// Set up the motor pin to be an output:
pinMode(motorPin, OUTPUT);
// Set up the serial port:
Serial.begin(9600);
}
void loop()
{
motormoveLevel = analogRead(sensorPin);
manualTune();
analogWrite(motorPin, lightLevel);
}
void manualTune()
{
lightLevel = map(lightLevel, 0, 1023, 0, 255);
lightLevel = constrain(lightLevel, 0, 255);
}
它不编译,但是我从中得到的代码是这样的,这里有一个打开电机几秒钟,然后间歇性关闭的代码:
const int motorPin = 9;
void setup()
{
// Set up the motor pin to be an output:
pinMode(motorPin, OUTPUT);
// Set up the serial port:
Serial.begin(9600);
}
void loop()
{
motorOnThenOff();
}
// This function turns the motor on and off like the blinking LED.
// Try different values to affect the timing.
void motorOnThenOff()
{
int onTime = 3000; // milliseconds to turn the motor on
int offTime = 3000; // milliseconds to turn the motor off
digitalWrite(motorPin, HIGH); // turn the motor on (full speed)
delay(onTime); // delay for onTime milliseconds
digitalWrite(motorPin, LOW); // turn the motor off
delay(offTime); // delay for offTime milliseconds
}
该代码根据光电传感器打开和关闭led:
const int sensorPin = 0;
const int ledPin = 9;
int lightLevel, high = 0, low = 1023;
void setup()
{
pinMode(ledPin, OUTPUT);
}
void loop()
{
lightLevel = analogRead(sensorPin);
manualTune();
analogWrite(ledPin, lightLevel);
}
void manualTune()
{
lightLevel = map(lightLevel, 0, 1023, 0, 255);
lightLevel = constrain(lightLevel, 0, 255);
}
所以基本上,我试着用这两段代码让马达根据它是否感觉到光线而运动。我的“弗兰肯斯坦怪兽”没有编译,因此,我想帮助您梳理这两个代码,使电机在光线照射到光电传感器时移动,而在被覆盖时不移动(我已经知道如何连接它)。您无法读取针脚0。您必须使用A0-A5(14-19)因此在代码上下文中会是什么样子?将“sensorPin=10”更改为“sensorPin=A0”,并将该引脚用于传感器。