I can also connect using two digital pins to control the brightness of an LED?
for a 5mm LED, one I put a 10k resitencia,
for another pin, falling to half brightness I put a 20K resistor,
this is good, as this resistance calculation?
int ledA = 7;
int ledB = 4;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(ledA, OUTPUT);
pinMode(ledB, OUTPUT);
digitalWrite(ledA, LOW); // turn the LED off by making the voltage LOW
digitalWrite(ledB, LOW); // turn the LED off by making the voltage LOW
}
// the loop routine runs over and over again forever:
void loop() {
for(int i=0;i<2;i++){
if(i==0){
digitalWrite(ledB, LOW); // turn the LED off by making the voltage LOW
digitalWrite(ledA, HIGH); // turn the LED on (HIGH is the voltage level)
}else{
digitalWrite(ledA, LOW); // turn the LED off by making the voltage LOW
digitalWrite(ledB, HIGH); // turn the LED on (HIGH is the voltage level)
}
delay(1000); // wait for a second
}
}
What you have done is make a very simple 2 bit digital to analog convertor. It should work, but instead of using digitalWrite (?, LOW); try pinMode(?, INPUT); and put pinmode(?, OUTPUT); before digitalWrite(?, HIGH);.
If you set one pin HIGH and the other LOW, some current will flow from one pin to the other instead of through the LED.
PaulRB:
What you have done is make a very simple 2 bit digital to analog convertor. It should work, but instead of using digitalWrite (?, LOW); try pinMode(?, INPUT); and put pinmode(?, OUTPUT); before digitalWrite(?, HIGH);.
If you set one pin HIGH and the other LOW, some current will flow from one pin to the other instead of through the LED.
int ledA = 7;
int ledB = 4;
// the setup routine runs once when you press reset:
void setup() {
}
// the loop routine runs over and over again forever:
void loop() {
for(int i=0;i<4;i++){
if(bitRead(i, 0) == 1){
pinMode(ledB, OUTPUT);
digitalWrite(ledB, HIGH);
}else{
pinMode(ledB, INPUT);
}
if(bitRead(i, 1) == 1){
pinMode(ledA, OUTPUT);
digitalWrite(ledA, HIGH);
}else{
pinMode(ledA, INPUT);
}
delay(1000); // wait for a second
}
}
This should give you three levels of brightness (plus off).
However, with 10K and 20K they will be very dim. Try 100R and 220R.