Offline
Newbie
Karma: 0
Posts: 9
|
 |
« on: January 06, 2013, 02:41:58 am » |
I'm trying to combine one code that flashes an RGB LED and one that controls a regular LED via a photocell. Not sure if it's possible, I'm very much a newbie when it comes to this, I tried a few things but none of them worked, these are the codes: int sensorPin = 0; // select the input pin for the photocell int sensorValue = 0; // variable to store the value coming from the photocell int LEDpin = 8; //LED Pin is on the Digital i/o pin number 8
void setup() { Serial.begin(9600); //Set baud rate to 9600 on the Arduino pinMode(LEDpin, OUTPUT); //set the LED pin as an output on digital i/o pin 8 }
void loop() {
sensorValue = analogRead(sensorPin); //get the value from input pin Serial.println(sensorValue); //print the value to Serial monitor delay(2000);
if (sensorValue < 300) //if there is darkness then turn led on
{ digitalwrite(LEDpin,HIGH); } else { digitalwrite(LEDpin, LOW); //else, keep the led off }
} int redpin=9; //Pin 9 int greenpin=10; //Pin 10 int bluepin=11; //Pin 11 int var=0; int var1=0;
void setup() { }
void loop() { for(var=250;var<255;var++) { analogWrite(redpin,var); //RED analogWrite(greenpin,0); delay(500); analogWrite(redpin,0); //GREEN analogWrite(greenpin,var); delay(500); analogWrite(greenpin,0); //BLUE analogWrite(bluepin,var); delay(500); analogWrite(bluepin,0); delay(500); } for(var1=250;var1<255;var1++) { analogWrite(redpin,var1); //YELLOW analogWrite(greenpin,var1); delay(500); analogWrite(redpin,0); delay(500); analogWrite(greenpin,var1); //CYAN analogWrite(bluepin,var1); delay(500); analogWrite(greenpin,0); delay(500); analogWrite(bluepin,var1); //MAGENTA analogWrite(redpin,var1); delay(500); analogWrite(bluepin,0); delay(500); analogWrite(bluepin,var1); analogWrite(redpin,var1); analogWrite(greenpin,var1); } }
|
|
|
|
|
Logged
|
|
|
|
|
Malaysia
Offline
Sr. Member
Karma: 7
Posts: 385
|
 |
« Reply #1 on: January 06, 2013, 02:51:10 am » |
what do you want the code to do? and what is happening with your code rite now so that we could help you.
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Online
Edison Member
Karma: 55
Posts: 1599
May all of your blinks be without delay
|
 |
« Reply #2 on: January 06, 2013, 02:53:17 am » |
It would be helpful to see what you have tried. Can you please post your combined code ? You will almost certainly need to stop using delay() because it holds up the program and will not let anything else happen until the delay finishes. Have a look at this http://arduino.cc/en/Tutorial/BlinkWithoutDelay for some ideas.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #3 on: January 06, 2013, 02:57:06 am » |
I want the code to go through the RGB LED colors as it does in the second code, but I only want it to do it when the photocell senses no light, as in the first code. I don't have any code right now because I tried so many things it got all messed up. I did try something like this: int LEDpin = 9; //LED Pin is on the Digital i/o pin number 8 int LEDpin2 = 10; int LEDpin3 = 11; void setup() { Serial.begin(9600); //Set baud rate to 9600 on the Arduino pinMode(LEDpin, OUTPUT); pinMode(LEDpin2, OUTPUT); //set the LED pin as an output on digital i/o pin 8 pinMode(LEDpin3, OUTPUT); }
|
|
|
|
|
Logged
|
|
|
|
|
Dee Why NSW
Offline
God Member
Karma: 12
Posts: 622
|
 |
« Reply #4 on: January 06, 2013, 03:01:29 am » |
Does this work? int sensorPin = 0; // select the input pin for the photocell int sensorValue = 0; // variable to store the value coming from the photocell int LEDpin = 8; int redpin=9; //Pin 9 int greenpin=10; //Pin 10 int bluepin=11; //Pin 11 int var=0; int var1=0; //LED Pin is on the Digital i/o pin number 8
void setup() { Serial.begin(9600); //Set baud rate to 9600 on the Arduino pinMode(LEDpin, OUTPUT); //set the LED pin as an output on digital i/o pin 8 }
void loop() {
sensorValue = analogRead(sensorPin); //get the value from input pin Serial.println(sensorValue); //print the value to Serial monitor delay(2000);
if (sensorValue < 300) //if there is darkness then turn led on
{ digitalwrite(LEDpin,HIGH); for(var=250;var<255;var++) { analogWrite(redpin,var); //RED analogWrite(greenpin,0); delay(500); analogWrite(redpin,0); //GREEN analogWrite(greenpin,var); delay(500); analogWrite(greenpin,0); //BLUE analogWrite(bluepin,var); delay(500); analogWrite(bluepin,0); delay(500); } for(var1=250;var1<255;var1++) { analogWrite(redpin,var1); //YELLOW analogWrite(greenpin,var1); delay(500); analogWrite(redpin,0); delay(500); analogWrite(greenpin,var1); //CYAN analogWrite(bluepin,var1); delay(500); analogWrite(greenpin,0); delay(500); analogWrite(bluepin,var1); //MAGENTA analogWrite(redpin,var1); delay(500); analogWrite(bluepin,0); delay(500); analogWrite(bluepin,var1); analogWrite(redpin,var1); analogWrite(greenpin,var1); } } else { digitalwrite(LEDpin, LOW); //else, keep the led off } }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #5 on: January 06, 2013, 03:24:35 am » |
It does not. There isn't anything on Digital i/o pin number 8.
The photocell is on Analog 1, and the RGB LED is on 9,10,11.
|
|
|
|
« Last Edit: January 06, 2013, 03:26:57 am by PM5K »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #6 on: January 06, 2013, 03:42:20 am » |
I was also wondering, is there some way to control the ground to the RGB LED instead of trying to do this via software?
So in other words the photocell could somehow control the ground, while the RGB setup remains the same as far as hardware and software, so all that changes is that when it's dark the photocell would trigger the ground which would activate the RGB LED circuit.
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Online
Edison Member
Karma: 55
Posts: 1599
May all of your blinks be without delay
|
 |
« Reply #7 on: January 06, 2013, 03:54:52 am » |
I was also wondering, is there some way to control the ground to the RGB LED instead of trying to do this via software?
So in other words the photocell could somehow control the ground, while the RGB setup remains the same as far as hardware and software, so all that changes is that when it's dark the photocell would trigger the ground which would activate the RGB LED circuit.
Do it all in software. Much easier to make changes. Please can you describe exactly what you are trying to do with the combined sketches and what is connected to which pin on the Arduino ? Do you want/need the LED from sketch 1 to turn on and off or just the RGB ones ?
|
|
|
|
|
Logged
|
|
|
|
|
Malaysia
Offline
Sr. Member
Karma: 7
Posts: 385
|
 |
« Reply #8 on: January 06, 2013, 03:55:54 am » |
you could make it as u want to but we could suggest rite ok heres the thing what you want is to make state
pseudo code
if photosensor reading higher/lower(depending of your sensor reading) then set point fade colour blue fade colour green fade colour re else turn off all led
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #9 on: January 06, 2013, 04:01:49 am » |
I want to take the first code which powers a LED on PIN 8 depending on input from a photocell.
I want to use the photocell, but I want to use it to run the RGB LED in the second code, and not the LED that was on PIN 8 in the first code.
The RGB LED is on digital 9,10,11. The photocell is on analog 0.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #10 on: January 06, 2013, 04:08:47 am » |
Actually Nick your code does work.
I had the ground for the photocell wrong, and had to change digitalwrite to digitalWrite. I also had to remove the second set of colors, otherwise it would cycle to white and stay on regardless of the photocell.
|
|
|
|
« Last Edit: January 06, 2013, 04:28:27 am by PM5K »
|
Logged
|
|
|
|
|
East Anglia (UK)
Online
Edison Member
Karma: 55
Posts: 1599
May all of your blinks be without delay
|
 |
« Reply #11 on: January 06, 2013, 04:24:04 am » |
I was going to suggest doing it this way. You might like to try it so that can appreciate why it works OK (1) Wire up the second example and get it working (2) Save the sketch with a new name (3) Copy the declaration of variables at the top of the second sketch into the new sketch so the Arduino will know where to find things. (4) At very the end of the new sketch, after the last }, paste in all of the code between { and } in the loop() function of the first sketch. (5) Put "void RGB()" (without the quotes) before what you just pasted in (6) Put "{" (no quotes) after void RGB() on a new line (7) Put "}" (no quotes) on a new line after what you pasted in You have just created a function that will run the RGB sequence Whenever you want to run the RGB sequence issue the command RGB(); So (  Replace if (sensorValue < 300) //if there is darkness then turn led on
{ digitalwrite(LEDpin,HIGH); } else { digitalwrite(LEDpin, LOW); //else, keep the led off }
in your new sketch with if (sensorValue < 300) //if there is darkness run the RGB function RGB();
} What do you want to happen when the lights are on ?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #12 on: January 06, 2013, 04:29:11 am » |
Interesting. I want it to turn off when the lights are on.
|
|
|
|
|
Logged
|
|
|
|
|
Malaysia
Offline
Sr. Member
Karma: 7
Posts: 385
|
 |
« Reply #13 on: January 06, 2013, 05:53:05 am » |
heres my code if you wanna try const uint8_t RedLed= 9; const uint8_t GreenLed= 10; const uint8_t BlueLed= 11; const uint8_t PhotoSensor= A0; const uint8_t SetValue=300;
void setup() { pinMode(RedLed,OUTPUT); pinMode(GreenLed,OUTPUT); pinMode(BlueLed,OUTPUT); }
void loop() { if (analogRead(PhotoSensor)>SetValue) { for(int a=0;a<=255;a++) { for(int b=0;b<=255;b++) { for(int c=0;c<=255;c++) { analogWrite(RedLed,a); analogWrite(GreenLed,b); analogWrite(BlueLed,c); } } } for(int a=255;a>=0;a--) { for(int b=255;b>=0;b--) { for(int c=255;c>=0;c--) { analogWrite(RedLed,a); analogWrite(GreenLed,b); analogWrite(BlueLed,c); } } } } else { digitalWrite(RedLed,LOW); digitalWrite(GreenLed,LOW); digitalWrite(BlueLed,LOW); } }
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Online
Edison Member
Karma: 55
Posts: 1599
May all of your blinks be without delay
|
 |
« Reply #14 on: January 06, 2013, 06:05:21 am » |
Interesting. I want it to turn off when the lights are on.
Ash has provided a solution. My preference would be to create a new function to turn the LEDs off and call it when you need to. I prefer this because you can give the functions names that mean something, can use them several times in a program if you need to and changes can be made in one place rather than searching for where to make changes. void LEDSoff() //turn off the RGB LEDs { digitalWrite(RedLed,LOW); digitalWrite(GreenLed,LOW); digitalWrite(BlueLed,LOW); }
Another thing to think about. When the light comes on should the RGB sequence stop immediately or carry on until it is finished ?
|
|
|
|
|
Logged
|
|
|
|
|
|