I have taken Vilros's tutorial #6 and #10 and merged them because I ultimately want to have a sensor control lighting brightness and motor speed. The closer to sensor, the brighter and faster they would go.
I'm not there yet.
Right now I just merged in one sketch those two tutorial sketches with a few adjustments.
The motor serial read works fine.
The lighting doesn't.
Whenever I //comment out the serialSpeed command, the lights work. But when on, it doesn't.
Can someone please take a look at my code and let me know where there is a mistake?
My connections are as described in tutorial except the motor is now on Pin 10. (see attached pic).
// THIS IS MISSING ANY LINKS OR OTHER INFORMATION AS TO SOURCE.
// As usual, we'll create constants to name the pins we're using.
// This will make it easier to follow the code below.
const int sensorPin = 0;
const int ledPin = 9;
const int motorPin = 10;
// We'll also set up some global variables for the light level:
int lightLevel, high = 0, low = 1023;
void setup()
{
// We'll set up the LED pin to be an output.
// (We don't need to do anything special to use the analog input.)
pinMode(ledPin, OUTPUT);
pinMode(motorPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
lightLevel = analogRead(sensorPin);
manualTune(); // manually change the range from light to dark
analogWrite(ledPin, lightLevel);
serialSpeed();
}
void manualTune()
{
lightLevel = map(lightLevel, 500, 1023, 0, 255);
lightLevel = constrain(lightLevel, 0, 255);
}
void serialSpeed()
{
int speed;
Serial.println("Type a speed (0-255) into the box above,");
Serial.println("then click [send] or press [return]");
Serial.println(); // Print a blank line
// In order to type out the above message only once,
// we'll run the rest of this function in an infinite loop:
while(true) // "true" is always true, so this will loop forever.
{
// First we check to see if incoming data is available:
while (Serial.available() > 0)
{
// If it is, we'll use parseInt() to pull out any numbers:
speed = Serial.parseInt();
// Because analogWrite() only works with numbers from
// 0 to 255, we'll be sure the input is in that range:
speed = constrain(speed, 0, 255);
// We'll print out a message to let you know that the
// number was received:
Serial.print("Setting speed to ");
Serial.println(speed);
// And finally, we'll set the speed of the motor!
analogWrite(motorPin, speed);
}
}
}
Just taking a wild guess here.....
while(true) // "true" is always true, so this will loop forever.
Think this might be of any clue ?
What would you get if you put the two lines of getting the light value and writing the LED into serialSpeed() ?