【发布时间】:2018-02-27 11:26:08
【问题描述】:
所以我开始使用我最近购买的一个 Arduino 套件,并且在我开始做更复杂的事情之前,我一直在尝试(目前)进行电机移动。
我未来的小项目的重点是让 Arduino 在晚上从我的窗户附近感应光线。从那里它有望转动一个能敲响我闹钟的马达。虽然现在我只是想让电机在看到光时移动,一旦停止看到光就关闭,因为我可以在几秒钟后添加自动关闭。
这是当前代码:
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);
}
所以基本上,我正在尝试使用这两段代码来根据电机是否感应到光来移动。我的“弗兰肯斯坦怪物”没有编译,因此,我想帮助组合这两个代码,使电机在光线照射到光电传感器时移动,而不是在被覆盖时移动(我已经知道如何接线) .
【问题讨论】:
标签: python loops arduino arduino-uno