Go Down

Topic: 2pcs 74HC595 with 16ch led + potentiometer. (Read 1 time) previous topic - next topic

Wawa

How can I add photoresitor to control brightness?
Connect the two OE (output enable) pins of the 595s to a PWM pin.
AnalogWrite 255 to that pin turns the LEDs off, and a lower value makes them brighter (0 = full brightness).

Read an LDR with an analogue input pin, and map it's values to the brightness levels you want.
Some experimenting required.
Leo..

gavron04

Thanks for reply.

HW isn't problem. But I have problem with software. I need to change code to PWM? Not as I have now like shiftOut/digitalWrite?

Grumpy_Mike

Quote
I need to change code to PWM?
No you have your code exactly how you have it now. You add a PWM capable output pin connected to the OE pins of the two shift registers and control the overall brightness of all the LEDs by doing an analogWrite of some value to that pin.

gavron04

Something likt this > https://learn.adafruit.com/adafruit-arduino-lesson-4-eight-leds/brightness-control

About ADC with LDR > https://forbot.pl/blog/wp-content/uploads/2015/05/ard_3_4-650x419.png

Grumpy_Mike

Quote
Something likt this > https://......
Yes.

Quote
About ADC with LDR >
Yes, but just the LDR part as the LED is not connected to a PWM capable pin so you can't dim it. The PWM capable pins have a ~ symbol next to them.

gavron04

I mean only LDR with resistor part as ADC.

About PWM it won't be problem when I'm using 2x 74hc595?


I will check all about monday.

gavron04

Only I have question about resistance? Which I need for resistor and photoresistor?

Grumpy_Mike

Quote
Which I need for resistor and photoresistor?
To get best results measure the resistance of the LDR in the medium light levels you want to use and make the resistor the closest standard value to that. This gives you the best range.

Wawa

Making use of the internal pull up resistor might do the job.

Connect the LDR between A0 and ground, and run this sketch.
Write down the numbers you see on the serial monitor when the room is light/dark.
You can use (map) these numbers to PWM values in the final sketch.
Leo..
Code: [Select]
const byte LDRpin = A0;

void setup() {
  Serial.begin(9600);
  pinMode(LDRpin, INPUT_PULLUP);
}

void loop() {
  Serial.println(analogRead(LDRpin));
  delay(250);
}

gavron04

Thanks for reply.

30-40 when it's sunny

120-130 when it's dark


I don't know the resistance of LDR - I have bought it with kit for arduino. Finally I will buy about 10pcs new LDR but I don't know which model - I have about 5-7 others LDR with different resistances.

Wawa

Look at the "map" examples in help>reference>map of the IDE.

There is explained how you can map/convert the LDR value of 30-130 to PWM dim values of e.g. 0-250.
Leo..

gavron04

Code: [Select]
const byte LDRpin = A0;

void setup() {
  Serial.begin(9600);
  pinMode(LDRpin, INPUT_PULLUP);
}

void loop() {
  Serial.println(analogRead(LDRpin));
  delay(250);

  int val = analogRead(LDRpin);
  val = map(val, 0, 40, 0, 64);
  val = map(val, 40,80, 65, 128);
  val = map(val, 80,120, 128, 192);
  val = map(val, 120, 200,192,255);
  analogWrite(10, val);
}

Wawa

#27
Sep 11, 2017, 09:32 am Last Edit: Sep 11, 2017, 09:33 am by Wawa
Untested, and you still need to add the main code (see comments).
Leo..
Code: [Select]
const byte LDRpin = A1; // LDR connected to pin A1 and ground (A0 is used for the pot)
const byte PWMpin = 10; // connected to the two OE pins
int lightValue = 30; // change/experiment with this value
int darkValue = 130; // change/experiment with this value
int LDRvalue;
byte PWMvalue;
// other declarations go here

void setup() {
  Serial.begin(9600); // only needed for debugging
  pinMode(LDRpin, INPUT_PULLUP); // enables the internal pull up resistor
  pinMode(PWMpin, OUTPUT); // make the pin an output
  // other void setup() code goes here
}

void loop() {
  LDRvalue = analogRead(LDRpin);
  Serial.print("LDR value is: ");
  Serial.println(LDRvalue);
  LDRvalue = constrain(LDRvalue, lightValue, darkValue);
  PWMvalue = map(LDRvalue, lightValue, darkValue, 0, 250);
  Serial.print("PWM value is: ");
  Serial.println(PWMvalue);
  analogWrite(PWMpin, PWMvalue); // write to the OE pins
  // other void loop() code goes here

  delay(1000); // remove this line after testing
}

gavron04

Code: [Select]
const byte LDRpin = A2; // LDR connected to pin A1 and ground (A0 is used for the pot)
const byte PWMpin = 10; // connected to the two OE pins
int lightValue = 30; // change/experiment with this value
int darkValue = 130; // change/experiment with this value
int LDRvalue;
byte PWMvalue;
int latchPin = 4;
int clockPin = 5;
int dataPin = 3;
int potPin = 0;
int potValue = 0;
int shiftValue = 0;

#define Out1Pin 1  //out1 230vac
#define Out2Pin 2 //out2 230vac
#define Out3Pin 0 //out3 5vdc
#define MutePin A2 //mute
#define VolUpPin 10 //up
#define VolDownPin A1 //down
#define CH1Pin A3 //ch1
#define CH2Pin A4 //ch2
#define CH3Pin A5 //ch3
#include <IRremote.h>
#define irPin 8  // pin dla TSOP
IRrecv irrecv(irPin);
decode_results results;





void setup() {
  //Serial.begin(9600); // only needed for debugging

 
     irrecv.enableIRIn();
     
   pinMode(Out1Pin, OUTPUT);
   pinMode(Out2Pin, OUTPUT);
   pinMode(Out3Pin, OUTPUT);
   pinMode(MutePin, OUTPUT);
   pinMode(VolUpPin, OUTPUT);
   pinMode(VolDownPin, OUTPUT);
   pinMode(CH1Pin, OUTPUT);
   pinMode(CH2Pin, OUTPUT);
   pinMode(CH3Pin, OUTPUT);
   pinMode(LDRpin, INPUT_PULLUP); // enables the internal pull up resistor
   pinMode(PWMpin, OUTPUT); // make the pin an output




   digitalWrite(0, LOW);
   digitalWrite(1, LOW);
   digitalWrite(2, LOW);
   digitalWrite(A2, HIGH);
   digitalWrite(10, LOW);
   digitalWrite(A1, LOW);
   digitalWrite(A3, HIGH);
   digitalWrite(A4, LOW);
   digitalWrite(A5, LOW);

   pinMode(latchPin, OUTPUT);
   pinMode(dataPin, OUTPUT); 
   pinMode(clockPin, OUTPUT);

}

void loop() {
  LDRvalue = analogRead(LDRpin);
  Serial.print("LDR value is: ");
  Serial.println(LDRvalue);
  LDRvalue = constrain(LDRvalue, lightValue , darkValue);
  PWMvalue = map(LDRvalue, lightValue, darkValue, 0, 250);
  Serial.print("PWM value is: ");
  Serial.println(PWMvalue);
  analogWrite(PWMpin, PWMvalue); // write to the OE pins

if (irrecv.decode(&results))
   {
       switch (results.value)
       {
         case 0x801:  // kod klawisza 1
            digitalWrite(Out1Pin, LOW);
            break;
           
         case 0x1:  // kod klawisza 1
            digitalWrite(Out1Pin, HIGH);
            break;
           
         case 0x802:  // kod klawisza 2
            digitalWrite(Out2Pin, LOW);
            break;
           
         case 0x2:  // kod klawisza 2
            digitalWrite(Out2Pin, HIGH);
            break;
           
         case 0x803:  // kod klawisza 3
            digitalWrite(Out3Pin, LOW);
            break;
           
         case 0x3:  // kod klawisza 3
            digitalWrite(Out3Pin, HIGH);
            break;
           
         case 0x80D:  // kod klawisza 4
            digitalWrite(MutePin, LOW);
            break;
           
         case 0xD:  // kod klawisza 4
            digitalWrite(MutePin, HIGH);
            break;

         case 0x807:  // kod klawisza 7 CH1
            digitalWrite(CH2Pin, LOW);
            digitalWrite(CH3Pin, LOW);
            digitalWrite(CH1Pin, HIGH);
            break;
           
         case 0x7:  // kod klawisza 7 CH1
            digitalWrite(CH2Pin, LOW);
            digitalWrite(CH3Pin, LOW);
            digitalWrite(CH1Pin, HIGH);
            break;     
                 
          case 0x808:  // kod klawisza 8 CH2
            digitalWrite(CH1Pin, LOW);
            digitalWrite(CH3Pin, LOW);
            digitalWrite(CH2Pin, HIGH);
            break;
           
         case 0x8:  // kod klawisza 8 CH2
            digitalWrite(CH1Pin, LOW);
            digitalWrite(CH3Pin, LOW);
            digitalWrite(CH2Pin, HIGH);
            break; 
                     
          case 0x809:  // kod klawisza 9 CH3
            digitalWrite(CH1Pin, LOW);
            digitalWrite(CH2Pin, LOW);
            digitalWrite(CH3Pin, HIGH);
            break;
           
         case 0x9:  // kod klawisza 9 CH3
            digitalWrite(CH1Pin, LOW);
            digitalWrite(CH2Pin, LOW);
            digitalWrite(CH3Pin, HIGH);
            break; 

           
         case 0x11:  // kod klawisza 5
            digitalWrite(VolUpPin, LOW);
            delay(250);
            digitalWrite(VolDownPin, LOW);
            break;
           
         case 0x811:  // kod klawisza 5
            digitalWrite(VolUpPin, HIGH);
            delay(250);
            digitalWrite(VolUpPin, LOW);
            break;
           
         case 0x10:  // kod klawisza 6
            digitalWrite(VolDownPin, LOW);
            delay(250);
            digitalWrite(VolUpPin, LOW);
            break;
           
         case 0x810:  // kod klawisza 6
            digitalWrite(VolDownPin, HIGH);
            delay(250);
            digitalWrite(VolDownPin, LOW);
            break;
           
         case 0x80C:  // kod klawisza OFF
            digitalWrite(Out1Pin, HIGH);
            digitalWrite(Out2Pin, HIGH);
            digitalWrite(Out3Pin, HIGH);
            //digitalWrite(MutePin, HIGH);
            break;
           
         case 0xC:  // kod klawisza OFF
            digitalWrite(Out1Pin, LOW);
            digitalWrite(Out2Pin, LOW);
            digitalWrite(Out3Pin, LOW);
            //digitalWrite(MutePin, LOW);
            break;


         }
   irrecv.resume();
   }

// read pot &  update outputs to 2 daisy chained shift registers
potValue = analogRead(A0);

if ( potValue >=0 && potValue <=20)
{
shiftValue = 0b1111111111111111;
}

if ( potValue >=20 && potValue <=63)
{
shiftValue = 0b0111111111111111;
}

if ( potValue >=63 && potValue <=126)
{
shiftValue = 0b0011111111111111;
}

if ( potValue >=126 && potValue <=189)
{
shiftValue = 0b0001111111111111;
}

if ( potValue >=189 && potValue <=252)
{
shiftValue = 0b0000111111111111;
}

if ( potValue >=252 && potValue <=315)
{
shiftValue = 0b0000011111111111;
}

if ( potValue >=315 && potValue <=378)
{
shiftValue = 0b0000001111111111;
}

if ( potValue >=378 && potValue <=441)
{
shiftValue = 0b0000000111111111;
}

if ( potValue >=441 && potValue <=504)
{
shiftValue = 0b0000000111111111;
}

if ( potValue >=504 && potValue <=567)
{
shiftValue = 0b0000000011111111;
}

if ( potValue >=567 && potValue <=630)
{
shiftValue = 0b0000000001111111;
}

if ( potValue >=630 && potValue <=693)
{
shiftValue = 0b0000000000111111;
}

if ( potValue >=693 && potValue <=756)
{
shiftValue = 0b0000000000011111;
}

if ( potValue >=756 && potValue <=819)
{
shiftValue = 0b0000000000001111;
}

if ( potValue >=819 && potValue <=882)
{
shiftValue = 0b0000000000000111;
}

if ( potValue >=882 && potValue <=945)
{
shiftValue = 0b0000000000000011;
}

if ( potValue >=945 && potValue <=1010)
{
shiftValue = 0b0000000000000001;
}

if ( potValue >=1010 && potValue <=1023)
{
shiftValue = 0b0000000000000000;
}



digitalWrite (latchPin, LOW);
shiftOut (dataPin, clockPin, MSBFIRST, highByte (shiftValue)); // upper 8 bits
shiftOut (dataPin, clockPin, MSBFIRST, lowByte (shiftValue)); // lower 8  bits
digitalWrite (latchPin, HIGH);
  delay(1000); // remove this line after testing
}

gavron04

#29
Sep 11, 2017, 10:25 am Last Edit: Sep 11, 2017, 10:47 am by gavron04
This code works, I delete delay at last line. Only what's wrong -leds are blinking xxxx per seconds. It is possible to fix it?

Go Up