Interface and Application Programming
Write an application that interfaces with an input and / or output device.
RGB LED with Processing
I managed to granularly control an RGB LED using processing with an Arduino but when I tried it with my Barduino I was having a few issues. I concluded that maybe it was due to needing to use PWM pins but in the rush to finish all of my assignments I didn't investigate further. Instead I modified my application to light a single LED at a time when its respective colour bar was interacted with.
Barduino Code
// Pins of the RGB LED, use resistors for long-term application int red = 0; int ground = 1; int green = 2; int blue = 3; void setup() { Serial.begin(9600); pinMode(red, OUTPUT); pinMode(green, OUTPUT); pinMode(blue, OUTPUT); pinMode(ground, OUTPUT); digitalWrite(ground, LOW); } void loop() { switch(readSerial()) { case 'R': analogWrite(red, readSerial()); break; case 'G': analogWrite(green, readSerial()); break; case 'B': analogWrite(blue, readSerial()); break; } } int readSerial() { while (Serial.available()<=0) { } return Serial.read(); }
Processing Code
import processing.serial.*; Serial port; slideControl sC1, sC2, sC3; color cor; void setup() { size(500, 500); println("Available serial ports:"); println(Serial.list()); // this will vary on different machines, check serial port = new Serial(this, Serial.list()[4], 9600); sC1 = new slideControl(100, 100, 90, 255, #FF0000); sC2 = new slideControl(200, 100, 90, 255, #03FF00); sC3 = new slideControl(300, 100, 90, 255, #009BFF); } void draw() { background(0); sC1.render(); sC2.render(); sC3.render(); port.write('R'); port.write(sC1.p); port.write('G'); port.write(sC2.p); port.write('B'); port.write(sC3.p); } class slideControl { int x, y, w, h, p; color cor; boolean slide; slideControl (int _x, int _y, int _w, int _h, color _cor) { x = _x; y = _y; w = _w; h = _h; p = 90; cor = _cor; slide = true; } void render() { fill(cor); rect(x-1, y-4, w, h+10); noStroke(); fill(0); rect(x, h-p+y-5, w-2, 13); fill(255); text(p, x+2, h-p+y+6); if (slide==true && mousePressed==true && mouseX<x+w && mouseX>x){ if ((mouseY<=y+h+150) && (mouseY>=y-150)) { p = h-(mouseY-y); if (p<0) { p=0; } else if (p>h) { p=h; } } } } }