Hi,
I am trying to use processing to allow my computer to control the brightness of the LED's in my sketch. I just can't figure out how to do it though. I am very new to Arduino and just got started a few days ago. Can someone add to my code so that it works? Also, can someone help simplify my code, as it is rather long. My sketch will automatically turn on the LED's and have them blink at the speed that is set with the pot if the room is dark. You can also force them to stay on and blink at the speed that is set with the pot if you press the button connected to the Arduino. I have connected everything using a breadboard. I am using the same processing code that is in this Arduino tutorial, http://arduino.cc/en/Tutorial/Dimmer. Here is my sketch.
/* This program uses a potentiometer, a light sensor, and a button to control the blinking of 3 LEDS
*/// constants won't change
const int butPin = 2; // pin the button is on
const int potPin = A0; // pin the potentiometer is on
const int lightPin= A1; // pin the light sensor is on
const int redLedPin = 11; // pin the red LED is on
const int greenLedPin = 10; // pin the green LED is on
const int blueLedPin = 9; // pin the blue LED is on// variables will change
int potVal; // the potentiometers value
int butVal; // the buttons value
int state = LOW; // state to put the LEDs in
int previous = LOW; // previous state of LEDs
int lightValue = 0; // the value of the light sensor
int lightMin = 1023; // the minimum light sensor value
int lightMax = 0; // the maximum light sensor valuelong time = 0;
long debounce = 200; // the debounce timervoid setup() {
// we set the pin modes here, they should be self explanitory
pinMode(13, OUTPUT);
pinMode(butPin, INPUT);
pinMode(potPin, INPUT);
pinMode(lightPin, INPUT);
pinMode(redLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(blueLedPin, OUTPUT);
// we use an attachInterrupt here to interrupt the prgram and jump back to loop when the button is pressed
attachInterrupt(0, loop, RISING);
// serial for debugging
Serial.begin(9600);
// These lights tell uss when the calibration has begun and finished
digitalWrite(13, HIGH);
// we calibrate the light sensor here
while (millis() < 5000) {
lightValue = analogRead(lightPin);if (lightValue < lightMax) {
lightMax = lightValue;
}if (lightValue > lightMin) {
lightMin = lightValue;
}
}digitalWrite(13, LOW);
}void loop() {
// we check if the button has been pressed here
butVal = digitalRead(butPin);if (butVal == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;time = millis();
}// we jump to void blinkLight() here
blinkLight();
}void blinkLight() {
// we read the light sensor here
lightValue = analogRead(lightPin);
lightValue = map(lightValue, lightMin, lightMax, 0, 255);
lightValue = constrain(lightValue, 0, 255);
// print the serial information for debugging
Serial.println(lightValue);// this if statement runs while lightValue greater then 90 and butVal does not equal HIGH. If the light value < 90 or butVal does equal HIGH,
// we jump to void blink()
if (lightValue > 90 && butVal != HIGH) {
potVal = analogRead(potPin);
analogWrite(redLedPin, lightValue);
delay(potVal);
potVal = analogRead(potPin);
digitalWrite(redLedPin, LOW);
delay(potVal);
potVal = analogRead(potPin);
analogWrite(greenLedPin, lightValue);
delay(potVal);
potVal = analogRead(potPin);
digitalWrite(greenLedPin, LOW);
delay(potVal);
potVal = analogRead(potPin);
analogWrite(blueLedPin, lightValue);
delay(potVal);
potVal = analogRead(potPin);
digitalWrite(blueLedPin, LOW);
delay(potVal);
}
// the else statement is used if the requirements for the if loop are false
else {
blink();
}
}void blink() {
// we read the potentiometers value here
potVal = analogRead(potPin);
// we do the blinks here by first checking the state to see if it is HIGH or LOW, if it is HIGH, the LEDs will turn on and blink,
// if the state is LOW, they remain off. We also delay by the amount of the potentiometer, this is what is used to control the how fast
// the LEDs blink
digitalWrite(redLedPin, state);
delay(potVal);
digitalWrite(redLedPin, LOW);
delay(potVal);
potVal = analogRead(potPin);
digitalWrite(greenLedPin, state);
delay(potVal);
digitalWrite(greenLedPin, LOW);
delay(potVal);
potVal = analogRead(potPin);
digitalWrite(blueLedPin, state);
delay(potVal);
digitalWrite(blueLedPin, LOW);
delay(potVal);
}