For this lab, I chose to take a modern approach to Morse Code and where the input signals come from. For my project, the dots come from keyboard inputs and the dashes come from FSR presses and together, you get a visual of the dots and spaces for the dashes. The blue LED also flashes in accordance with the FSR presses.
Components:
- Arduino Uno
- Breadboard
- 1 force sensor
- 1 220Ω resistors
- 1 10Ω resistor
- wires
Arduino code #1:
int photocellPin = 0; // select the input pin for the potentiometer int fsrPin =11; int bPin = 11; // select the pin for the LED int fsrVal; int photocellVal; // variable to store the value coming from the sensor int counter; int THRESHOLD = 500; void setup() { photocellVal = 0; fsrVal = 0; counter = 0; Serial.begin(9600); } void loop() { fsrVal = analogRead(fsrPin); // read the value from the sensor, between 0 - 1024 analogWrite(bPin, fsrVal/4); photocellVal = analogRead(photocellPin); if(photocellVal>THRESHOLD) { analogWrite(bPin, photocellVal/4); if(counter%300==0) { // Serial.print(photocellVal); Serial.print("1"); } } else { analogWrite(bPin, 0); if(counter%300==0) { Serial.print("0"); } } counter++; }
Processing code:
adapted from :http://courses.ischool.berkeley.edu/i262/s13/content/stlim/lab-submission-4-photocell-and-fsr?destination=node%2F909
import processing.serial.*; // Change this to the portname your Arduino board String portname = "/dev/tty.usbmodem1411"; Serial port; String buf=""; int cr = 13; // ASCII return == 13 int lf = 10; // ASCII linefeed == 10 int radius = 50; int gapX = 1; int gapY = 3; int windowX = 1000; int windowY = 720; int marginX = 40; int marginY = 40; int x = marginX; int y = marginY; boolean gap = false; boolean init = true; void setup() { size(1200, 720); frameRate(10); smooth(); background(255, 200, 200); noStroke(); port = new Serial(this, portname, 9600); } void draw() { } void keyPressed() { if(key == ' ') { background(40,40,40); // erase screen } else { int x = int(random(0,width)); int y = int(random(0,height)); drawball(); } } // draw balls void drawball() { ellipse(x,y,radius,radius); x+=3; // x += 2*radius + gapX; if(x>windowX-marginX) { x = marginX; y += 2*radius + gapY; } } // called whenever serial data arrives void serialEvent(Serial p) { int serialVal = 0; int c = port.read(); // called whenever serial data arrives if (c != lf && c != cr) { buf += char(c); } if (c == lf) { serialVal = int(buf); println("val="+serialVal); buf = ""; } if(serialVal==0 && init) { } else if(serialVal == 0) { if(!gap) { x += 2*radius + gapX; } gap = true; } else { init = false; gap = false; drawball(); x+=radius/5; } }