Transmutation Code (Project 2)
The things you’ll need:
1- potentiometer 1- RGB LED 3- 330 ohm resistors 9- jumper wires
Setup:
-Red jumper wire -> 5v pin on Arduino -Black jumper wire -> GRND pin on Arduino
-The potentiometer is placed: • A7, B6, B8 • A blue jumper wire connects from E6 -> - (right side of the breadboard) • Green wire connects from E7 -> A0 on the Arduino • Yellow wire connects fromE8 -> + (right side of the breadboard) -The RGB LED is placed: • RED = i20 • Cathode = i21 • GREEN = i22 • BLUE = i23 -Resistors are placed: • G20 -> E20 • Black wire from G21 -> (right side of breadboard) • G22 -> E22 • G23 -> E23 3 White wires are placed: • C20 -> pin 11 on Arduino • C22 -> pin 10 on Arduino • C23 -> pin 9 on Arduino
---------------------------------------------------------------------------
Arduino code:
int potPin = 0; //where potentiometer is connected to on the Arduino
//////////////////////////////////// /////////////////////RGB LED START//////////////// ///////////////////////////
int redPin = 9; //controls RED of the RGB LED, connected to pin 9 on Arduino int greenPin = 10; //controls GREEN of the RGB LED, connected to pin 10 on Arduino int bluePin = 11; //controls BLUE of the RGB LED, connected to pin 11 on Arduino
long int inByte; //long int is used so the LED can detect any combination of colour code // Byte is a binary format and stores an 8-bit number, again for colour detection
int wait = 10; //10 milliseconds delay
//////////////////////////////////// /////////////////////RGB LED END//////////////// ///////////////////////////
//int LEDPin = 6; //int writeValue; //int readValue; //I couldn't get a second LED to light but I left the code for future reference
void setup() {
pinMode(redPin, OUTPUT); // all pins of the RGB LED and outputs pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT);
// pinMode (potPin, INPUT); // pinMode (LEDPin, OUTPUT); //non-working LED
Serial.begin (9600); } /////////////////////////////////////////////////////////////////////////// void outputColour(int red, int green, int blue) //the output that controls the RED, GREEN, & BLUE of the LED { analogWrite(redPin, red); //redPin controls RED colour analogWrite(bluePin, blue);// bluePin controls BLUE colour analogWrite(greenPin, green);//greenPin controls GREEN colour }
int* getColour() { //tells program to look for the colour (controlled by a mouse press -see Processing) int* colour; // int i; i = 0;
while (i < 4) //keeps 'i' at 0 { if (Serial.available() > 0) { //when a colour is detected the program will recognize it colour[i] = Serial.read(); i++; } }
return colour; //returns the colour info that it detects } /////////////////////////////////////////////////////////////////// void loop() {
// readValue = analogRead (potPin); // writeValue = (255./1023.) * readValue; // analogWrite (LEDPin, writeValue); //non-working LED
//////////////////////////////////// /////////////////////RGB LED START//////////////// ///////////////////////////
if (Serial.available() > 0) { // get info about colour and outputs it
inByte = Serial.read();
if (inByte == 'C') { int* one; one = getColour(); outputColour(one[1],one[2],one[3]); } }
//////////////////////////////////// /////////////////////RGB LED END//////////////// ///////////////////////////
//////////////////////////////////// /////////////////////POTENTIOMETER START//////////////// ///////////////////////////
int val = map (analogRead(potPin), 0, 1023, 0, 255); //for controlling colour- value between 0 & 255 Serial.println (val); //so the value of the colour shows up delay (50); //makes the change noticable (slows it down)
//////////////////////////////////// /////////////////////POTENTIOMETER END//////////////// ///////////////////////////
---------------------------------------------------------------------
Processing code:
import processing.serial.*; //imports the serial library so Processing know to 'talk' to Arduino
Serial port; //port is used for the serial data (from Arduino)
float R = random(255); //controls RED value (random each time) float G = random (255); //controls GREEN value (random each time) float blue = random (255); //controls BLUE value (random each time)
void setup () { size (500, 500); //sketch window size port = new Serial (this, "COM3", 9600); //this lets Processing know to connect to the port Arduino is using // and the the serial data it displays is for this sketch/ using this port port.bufferUntil('\n'); //tells Processing to go to next line when end is reached
}
void draw () { cursor (CROSS); //makes the cursor display as a Cross on the sketch background (R, G, blue); //displays the colour in/of the sketch window (I used "blue" because instead og 'B' becuase it was easier for me to keep track of } public void mouseMoved() //allows something to happen when the mouse is moved in sketch { loadPixels(); //allows sketch to read the pixels of the image println("Red: "+red(pixels[mouseX + mouseY * width])); //reads and prints (in println) the RED pixels values in accordance with the mouse moves println("Green: "+green(pixels[mouseX + mouseY * width])); //reads and prints (in println) the GREEN pixels values in accordance with the mouse moves println("Blue: "+blue(pixels[mouseX + mouseY * width]));//reads and prints (in println) the BLUE pixels values in accordance with the mouse moves println ("#" + hex(pixels[mouseX + mouseY * width], 6));// //reads and prints (in println) the HEX VALUE pixels values in accordance with the mouse moves
//all values are represented by there color code/ hex values//
}
void mousePressed() { port.write("CL"); port.write(int(red(pixels[mouseX+mouseY*width]))); port.write(int(green(pixels[mouseX+mouseY*width]))); port.write(int(blue(pixels[mouseX+mouseY*width]))); }
void serialEvent (Serial port) //Processing knows it is displaying whatever occurs in the port {
blue = float(port.readStringUntil ('\n')); //allows for change to occur when Arduino components are manipulated








