{"version":"1.0","provider_name":"Projects","provider_url":"https:\/\/projects.schneidr.de","author_name":"Gerald","author_url":"https:\/\/projects.schneidr.de\/author\/admin\/","title":"Dimming the wordclock","html":"I just tried if it is possible to use one of the PWM pins of the Arduino to dim the LEDs if I want to. Short answer: yes and no. Now \u00a0for the longer answer.<!--more-->\r\n\r\nI connected one of the shift registers with the Arduino. But instead of connecting the other pin of the LEDs with GND, I connected them to one of the PWM pins of the Arduino.\r\n\r\nSince it currently seems to be impossible to embed a YouTube video without it loading tracking scripts the video can only be viewed <a href=\"https:\/\/www.youtube.com\/watch?v=z8t5AHSCge4\">directly on YouTube<\/a>.\r\n\r\nSo, it works. I can control which LEDs are lit with the shift registers, and I can control how bright they are with the PWM pin. The only downside: I can only dim all of them at once, so no fading in and out of single words. The only way to do that would be to connect the relevant groups to different PWM pins. I guess I'll have to give that some thought.\r\n\r\nWhat I can do right now, is dimming them according to ambient light. When I connect a photoresistor to one of the analog ins I can detect the light level and make the clock brighter when the room is lit and dim it when the room becomes dark.\r\n\r\nHere is the source code I used:\r\n<pre lang=\"Arduino\">int ledPin = 9;    \/\/ LED connected to digital pin 9\r\n\r\n\/\/ shift register stuff\r\nconst int g_pinCommLatch = 40;\r\nconst int g_pinData = 41;\r\nconst int g_pinClock = 42;\r\nconst int g_registers = 1;\r\nbyte g_registerArray[g_registers];\r\n\r\n\/\/ Simple function to send serial data to one or more shift registers by iterating backwards through an array.\r\n\/\/ Although g_registers exists, they may not all be being used, hence the input parameter.\r\nvoid sendSerialData (\r\n  byte registerCount,  \/\/ How many shift registers?\r\n  byte *pValueArray)   \/\/ Array of bytes with LSByte in array [0]\r\n{\r\n  \/\/ Signal to the 595s to listen for data\r\n  digitalWrite (g_pinCommLatch, LOW);\r\n\r\n  for (byte reg = registerCount; reg &gt; 0; reg--) {\r\n    byte value = pValueArray [reg - 1];\r\n    for (byte bitMask = 128; bitMask &gt; 0; bitMask &gt;&gt;= 1) {\r\n      digitalWrite(g_pinClock, LOW);\r\n      digitalWrite(g_pinData, value &amp; bitMask ? HIGH : LOW);\r\n      digitalWrite(g_pinClock, HIGH);\r\n    }\r\n  }\r\n  \/\/ Signal to the 595s that I'm done sending\r\n  digitalWrite (g_pinCommLatch, HIGH);\r\n}  \/\/ sendSerialData\r\n\r\nvoid setup()\r\n{\r\n  pinMode (g_pinCommLatch, OUTPUT);\r\n  pinMode (g_pinClock, OUTPUT);\r\n  pinMode (g_pinData, OUTPUT);\r\n\r\n  g_registerArray[0] = 254;\r\n  sendSerialData (g_registers, g_registerArray);\r\n} \/\/ setup\r\n\r\nint c = 2;\r\n\r\nvoid loop()\r\n{\r\n  g_registerArray[0] = c;\r\n  sendSerialData (g_registers, g_registerArray);\r\n  c *= 2;\r\n  if (c &gt; 128) {\r\n    c = 2;\r\n  }\r\n  \/\/ fade in from min to max in increments of 5 points:\r\n  for(int fadeValue = 0 ; fadeValue = 0; fadeValue -=5) { \r\n    \/\/ sets the value (range from 0 to 255):\r\n    analogWrite(ledPin, fadeValue);         \r\n    \/\/ wait for 30 milliseconds to see the dimming effect    \r\n    delay(30);                            \r\n  } \r\n} \/\/ loop<\/pre>","type":"rich"}