Code It

The Arduino sketch for this is straightforward. If you want to test out the effect, open the Arduino File > Examples > Basics > Fade sketch. Change the int led variable to pin 0 (since that's one of the pins controlling one of our LCD panels) like this:

 

Download: file
/*
 Fade

 This example shows how to fade an LED on pin 9
 using the analogWrite() function.

 The analogWrite() function uses PWM, so if
 you want to change the pin you're using, be
 sure to use another PWM capable pin. On most
 Arduino, the PWM pins are identified with 
 a "~" sign, like ~3, ~5, ~6, ~9, ~10 and ~11.

 This example code is in the public domain.
 */

int led = 0;           // the PWM pin the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup() {
  // declare pin 9 to be an output:
  pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // set the brightness of pin 9:
  analogWrite(led, brightness);

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(30);
}

 

Then, plug your Trinket into your computer over USB, select your Trinket (either Trinket 16Mhz or 8Mhz) from the Arduino Tools > Boards menu, pick the USB port, and upload. After the program is done uploading you'll see the LCD opacity fading up and down!

We'll extend this code to include our other two panels, as well as checking the Hall effect sensor for the presence of a magnet. Copy the code below, create a new Arduino sketch, paste the code, save the file, and then upload to your Trinket.

Download: file
//Shutterglass Chamber
//by John Park
//uses analogWrite() PWM function to fade between dark and light states 
//on LCD welders glass panels

/*
 Unlike pinMode(INPUT), there is no pull-down resistor necessary. An internal
 20K-ohm resistor is pulled to 5V. This configuration causes the input to
 read HIGH when the switch is open, and LOW when it is closed.


Wiring is left (pin 1) to Vin, middle to GND, right to analog voltage output, such as A1 on Uno
  place 10K resistor between voltage in Pin 1 and voltage out Pin 3
  _______
 |\_____/|  
 | |   | |
 | |   | |
  -------
  /  |  \
 |   |   |
 |   |   |
 |   |   |
 |   |   |
 +  GND  Output
 |   |   |
 |       |
 \-[|||]-/ 10k resistor

 
 */

int panelState = 0; //0 is transparent, 1 is dark

int paneA = 0;           //  PWM pin controlling panel on Trinket
int paneB = 1;           
int paneC = 4;    
int onSwitch = 2;   

void setup() {

  //declare switch pin as input_pullup to use internal resistor
  //pinMode(onSwitch, INPUT_PULLUP);
  //digital hall sensor (A3144 402 type) acts as on/off switch
  pinMode(onSwitch, INPUT);

  // declare pins as  output
  pinMode(paneA, OUTPUT);
  pinMode(paneB, OUTPUT);
  pinMode(paneC, OUTPUT);


  digitalWrite(paneA, HIGH); //darken the panel at start
  digitalWrite(paneB, HIGH);
  digitalWrite(paneC, HIGH); 
}

void loop() {
  //read the pushbutton value into a variable
  int sensorVal = digitalRead(onSwitch);

  // Keep in mind the pullup means the pushbutton's
  // logic is inverted. It goes HIGH when it's open,
  // and LOW when it's pressed. Turn on pin 13 when the
  // button's pressed, and off when it's not:
  
  if (sensorVal == HIGH) { //the button has been pressed/switch has been closed
    //while the switch is closed, make the led go off and the panel transparent
     if(panelState==1){
      
      fadePanel(0,20);
      panelState=0;//flip the counter
      
     }
    } 
  
  else if (sensorVal == LOW){
     if(panelState==0){
    
      //digitalWrite(paneA,HIGH);
      //digitalWrite(paneB,HIGH);
      //digitalWrite(paneC,HIGH);
      fadePanel(1,20);
      panelState=1;
     }
  } 
  
}

void fadePanel(int fadeDir, int delayTime){ 
//fades a pin 
  if (fadeDir==0){ //fade it down to transparent
    for(int i=140; i>=0; i-=5){ //140 is a nice value for i, but should it be darker?
     // analogWrite(panelPin, i);
      analogWrite(0, i);
      analogWrite(1, i);
      analogWrite(4, i);
      delay(delayTime);
    }
  }
  if (fadeDir==1){ //fade it up to dark
    for(int i=0; i<=255; i+=5){ //140?
      analogWrite(4, i);
      analogWrite(1, i);
      analogWrite(0, i);
      delay(delayTime); 
    }

  }
}

 

Now, when you pass the correct pole of the magnet past the proper face of your Hall effect sensor, the LCD panels will fade to opaque. Remove the magnet and they fade back to transparent.

This guide was first published on Apr 01, 2017. It was last updated on Apr 01, 2017. This page (Code It) was last updated on Dec 02, 2019.