Week 8 - Untoolkit : electronic outputs

Zine

My zine design :

Assignment 1: RGB LED & digital switch

For this assignment, I used my Grove Arduino board. My regular board is broken so I had to improvise. Loes helped me out with this problem and showed me a way to connect the grove board with my breadboard.

To begin with, I connected the RGB LED to my groveboard on the D0, D1 and D2 pinholes. I connected those to my breadboard. Then I placed three 22 ohm resistors on the board, paralel from the LED pins and the cables. At last, I connected the LED to an GND port, and connected this to the GND of my arduino.

In Arduino, I used the following code that Loes provided to me:

int RED_PIN = D2;
int GREEN_PIN = D1;
int BLUE_PIN = D0;

// This variable controls how fast we loop through the colors.
// (Try changing this to make the fading faster or slower.)

int DISPLAY_TIME = 100;  // In milliseconds

void setup()
{
  // Here we'll configure the Arduino pins we're using to
  // drive the LED to be outputs:

  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
}

void loop(){

// Off (all LEDs off):

  digitalWrite(RED_PIN, LOW);
  digitalWrite(GREEN_PIN, LOW);
  digitalWrite(BLUE_PIN, LOW);

  delay(1000);

  // Red (turn just the red LED on):

  digitalWrite(RED_PIN, HIGH);
  digitalWrite(GREEN_PIN, LOW);
  digitalWrite(BLUE_PIN, LOW);

  delay(1000);

  // Green (turn just the green LED on):

  digitalWrite(RED_PIN, LOW);
  digitalWrite(GREEN_PIN, HIGH);
  digitalWrite(BLUE_PIN, LOW);

  delay(1000);

  // Blue (turn just the blue LED on):

  digitalWrite(RED_PIN, LOW);
  digitalWrite(GREEN_PIN, LOW);
  digitalWrite(BLUE_PIN, HIGH);

  delay(1000);

  // Yellow (turn red and green on):

  digitalWrite(RED_PIN, HIGH);
  digitalWrite(GREEN_PIN, HIGH);
  digitalWrite(BLUE_PIN, LOW);

  delay(1000);

  // Cyan (turn green and blue on):

  digitalWrite(RED_PIN, LOW);
  digitalWrite(GREEN_PIN, HIGH);
  digitalWrite(BLUE_PIN, HIGH);

  delay(1000);

  // Purple (turn red and blue on):

  digitalWrite(RED_PIN, HIGH);
  digitalWrite(GREEN_PIN, LOW);
  digitalWrite(BLUE_PIN, HIGH);

  delay(1000);

  // White (turn all the LEDs on):

  digitalWrite(RED_PIN, HIGH);
  digitalWrite(GREEN_PIN, HIGH);
  digitalWrite(BLUE_PIN, HIGH);

  delay(1000);
}

After I uploaded the code, the LED on my breadboard began to shine different colors!

Now my LED is working, I am going to connect a switch to toggle between the different colors of the LED.

Because I don't have a 10K resistor at home, Loes has thaught me to use a Pullup. In this way, you can enhance the signal a digital pin is giving. I had to swap the High and Lows in the original code from Micky, because the connections on the breadboard are different. The button is normally attached to a ground, but now it has to be attached to a 3v port. My code looked like this:

//       RGB LED Color Control
//      circuits4you.com
//========================================================================
//Changes made by Micky van Zeijl 
 
// constants won't change. They're used here to
// set pin numbers:
 
// Make sure to change the pinnumbers when you’re using the Arduino MKR.
// For the nodeMcu you can use all pins except 0. Don’t forget to add a D
// so it’s redPin = D3; for example

const int buttonPin = D8; // for Arduino MKR 1010 I used 6
const int redPin = D1; // for Arduino MKR 1010 - use pin 3
const int bluePin = D3; // for Arduino MKR 1010 - use pin 4
const int greenPin = D2; // for ARduino MKR 1010 - use pin 5

int counter = 0;
bool previousbuttonstateHigh = false;
void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  pinMode(D8,INPUT_PULLUP);
  Serial.begin(115200);
}

void loop() {

  int buttonState;
  buttonState = digitalRead(buttonPin);

  Serial.println(counter);

  if (buttonState == HIGH && !previousbuttonstateHigh) {
    counter++;

    if (counter > 3) counter = 0;
    previousbuttonstateHigh = true;

  }

  if (buttonState == HIGH) {
    previousbuttonstateHigh = false;
  }

  Serial.print("buttonstate is:" );
  Serial.println( buttonState);
  Serial.print("previous buttonstate is:");
  Serial.println(previousbuttonstateHigh);

  if (counter == 0) {
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, LOW);
    digitalWrite(bluePin, LOW);
  }

  else if (counter == 1) {
    digitalWrite(redPin, HIGH);
    digitalWrite(greenPin, LOW);
    digitalWrite(bluePin, LOW);
  }

  else if (counter == 2) {
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, HIGH);
    digitalWrite(bluePin, LOW);
  }

  else if (counter == 3) {
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, LOW);
    digitalWrite(bluePin, HIGH);
  }


}

At first, I received unexpected outputs from the counter in the code. I only saw a red light from the LED.

To check if my code was correct, I added a serial print line to each else if statement. The code was correct so it had to be my hardware.

//       RGB LED Color Control
//      circuits4you.com
//========================================================================
//Changes made by Micky van Zeijl 
 
// constants won't change. They're used here to
// set pin numbers:
 
// Make sure to change the pinnumbers when you’re using the Arduino MKR.
// For the nodeMcu you can use all pins except 0. Don’t forget to add a D
// so it’s redPin = D3; for example

const int buttonPin = D8; // for Arduino MKR 1010 I used 6
const int redPin = D1; // for Arduino MKR 1010 - use pin 3
const int bluePin = D3; // for Arduino MKR 1010 - use pin 4
const int greenPin = D2; // for ARduino MKR 1010 - use pin 5

int counter = 0;
bool previousbuttonstateHigh = true;

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  pinMode(D8,INPUT_PULLUP);
  Serial.begin(115200);
}

void loop() {

  int buttonState;
  buttonState = digitalRead(buttonPin);

  Serial.println(counter);

  if (buttonState == LOW && !previousbuttonstateHigh) {
    counter++;

    if (counter > 3) counter = 0;
    previousbuttonstateHigh = true;

  }

  if (buttonState == HIGH) {
    previousbuttonstateHigh = false;
  }


  if (counter == 0) {
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, LOW);
    digitalWrite(bluePin, LOW);
    Serial.println("uit");
  }

  else if (counter == 1) {
    digitalWrite(redPin, HIGH);
    digitalWrite(greenPin, LOW);
    digitalWrite(bluePin, LOW);
    Serial.println("Rood");
  }

  else if (counter == 2) {
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, HIGH);
    digitalWrite(bluePin, LOW);
    Serial.println("Groen");
  }

  else if (counter == 3) {
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, LOW);
    digitalWrite(bluePin, HIGH);
    Serial.println("blauw");
  }


}

I finally noticed that my resistors didn't connect properly, so the green and blue colors didn't want to light up. When I fixed this, I noticed that the output was very unstable. It had to do with my counter, because I saw in the monitor that the counter value fluctuated from 0 to 3 very quickly.

When I switched

bool previousbuttonstateHigh = false;

to

bool previousbuttonstateHigh = true;

I fixed the problem. Because when the previous button state is high and the counter reads false, it forms a loop and pendles between the counter values. When the previous button state is high and true, the counter can make a distinction between the different inputs.

Assignment 2: RGB-LED & analog sensor

The next assigment was to connect a RGB-LED to an analog sensor. Originaly, we had to use a LDR sensor. I didn't have one, so I used a grove light sensor.

At first, I tried the code on DLO. Because I use a different sensor, the code didn't work properly. I searched on the internet and found a nice code I could use to manipulate the RGB-LED with the sensor:

int sensorPin = A0; //Light sensor
int redPin= D1; 
int greenPin = D2;
int bluePin = D3;
int sensorValue = 0; // variable to store the value coming from the sensor

void setup() {
Serial.begin(115200); //sets serial port for communication
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}
void loop() {
sensorValue = analogRead(sensorPin); // read the value from the sensor
Serial.println(sensorValue); //prints the values coming from the sensor on the screen


delay(100);

if (sensorValue < 400) {
  setColor(170, 0, 255); //purple
  }

  else {
    setColor(255, 255, 0); //green
    }

}

void setColor(int redValue, int greenValue, int blueValue) {
  analogWrite(redPin, redValue);
  analogWrite(greenPin, greenValue);
  analogWrite(bluePin, blueValue);
}

The code worked perfectly, the color of the LED changed when placed in a dark environment.

Processing & Digital Swatch

The next step is to work with Processing. Processing is a sketchbook software that generates visuals by the input of code. It is possible to connect the input of your sensor with Arduino to manipulate the output of Processing.

Because I was working with the grove light sensor on my previous assignments, I decided to use it for this assignment as well because I am familiar with it.

The output created by Processing has to resemble the series we created, so I am aiming for a black background and copper details.

I searched on the processing website for some nice variable examples in shapes. They all use the mouse position, I have to adjust to code so the output reacts to my sensor input.

To do so, I wrote some code for the Arduino, so the output on my COM3 will convert it's values for processing languague.

int sensorValue = 0;

void setup() {
Serial.begin(115200); //needs to match processing baudrate
}

void loop() {
//Serial.println(analogRead(A0)); // for debugging only
sensorValue = analogRead(A0); // store value in sensorValue
Serial.write(map(sensorValue, 0,1023,0,255)); // map to processing range
delay(50);
}

Next, I looked at the examples on the processing website. I came across a cool design, a bezier.

The code for this example was originaly like this:

void setup() {
  size(640, 360); 
  stroke(255);
  noFill();
}

void draw() {
  background(0);
  for (int i = 0; i < 200; i += 20) {
    bezier(mouseX-(i/2.0), 40+i, 410, 20, 440, 300, 240-(i/16.0), 300+(i/8.0));
  }
}

The sketch now only uses the input of your mouse. The styling is also not in line with my idea. So I had to connect my sensor input with Processing, and change the values of the sketch to make it appear more like the series we've made.

The code I wrote looked like this:

import processing.serial.*;
int x;
int y;
float outsideRadius = 150;
float insideRadius = 100;
Serial myPort;
int dataInput;
PImage bg;


void setup() {
  size(500, 500);
  stroke(91, 53, 17);strokeWeight(4);  // Thicker
line(20, 40, 80, 40);
  noFill();
  String portName = "COM3";  //change to your own port address!
  myPort = new Serial(this, portName, 115200); //needs to match arduino baudrate
}

  
 void draw() {
  background(0);
  for (int i = 0; i < 200; i += 20) {
    bezier(dataInput-(i/2.0), 40+i, 410, 20, 440, 300, 240-(i/16.0), 300+(i/8.0));
  }
}

void serialEvent(Serial myPort) {
dataInput = myPort.read();
 }

I adjusted the thickness and colors of the lines. I also adjusted the canvas size to match the 10cm * 10cm requirement. I changed the bezier input to dataInput instead of mouseX.

I uploaded the code, and the bezier moved when I changed the light on my light sensor!

Reflection

During this week, I learned a lot of new insights concerning programming on Arduino. I had some prior experience, but that was very basic compared to the things I've done this week.

Especialy the combination with the breadboard and additional electronical circuit props, like resistors (and the lack of them) has thaught me to debug, program and build cool installations.

Loes helped me out a lot this week, because I didn't have all the required parts. I learned how to pull up a signal for my Arduino board, so I can bypass the required 10k ohm resistor for my button.

Last updated