// Activity 6 - Arduino Sketch for communicating with Processing program char val; // data received from Processing app int btn1 = 6; // pins for buttons connected to Arduino int btn2 = 5; int btn3 = 4; int btn4 = 3; int bt1Val, bt2Val, bt3Val, bt4Val; // value is TRUE if button pressed, FALSE // if no press long btThresh = 100; // time between button presses is 1/10 of a second long lastBt1 = millis();// current system time long lastBt2 = millis(); long lastBt3 = millis(); long lastBt4 = millis(); int led1 = 11; // first LED int led2 = 10;// second LED void setup() { pinMode(btn1, INPUT); // set the button pins as INPUTS pinMode(btn2, INPUT); pinMode(btn3, INPUT); pinMode(btn4, INPUT); pinMode(led1, OUTPUT);// set the LED pins as OUTPUTS pinMode(led2, OUTPUT); digitalWrite(led1, LOW);// turn OFF the LEDs digitalWrite(led2, LOW); Serial.begin(9600); // open a serial communications channel between Arduino // and the computer } void loop() { if (Serial.available()) { // If data is available to read, val = Serial.read(); // read it and store it in val if (val == 'L') { // if 'L' sent from the Processing application for (int i = 1; i <= 5; i++) { // flash the LEDs 5 times digitalWrite(led1, HIGH); // Turn on LED 1 delay(100); digitalWrite(led2, HIGH); // Turn on LED 2 delay(100); digitalWrite(led1, LOW); // Turn off LED 1 delay(50); digitalWrite(led2, LOW); // Turn off LED 2 } } } // scan the buttons for presses bt1Val = digitalRead(btn1); if (bt1Val) { if (millis() - lastBt1 > btThresh) { // if the minimum of time has passed // since the last button 1 press Serial.write('1'); // send a '1' to Processing app lastBt1 = millis(); //reset for next button press } } bt2Val = digitalRead(btn2); if (bt2Val) { if (millis() - lastBt2 > btThresh) { Serial.write('2'); lastBt2 = millis(); } } bt3Val = digitalRead(btn3); if (bt3Val) { if (millis() - lastBt3 > btThresh) { Serial.write('3'); lastBt3 = millis(); } } bt4Val = digitalRead(btn4); if (bt4Val) { if (millis() - lastBt4 > btThresh) { for (int i = 1; i <= 3; i++) { // send 3 repetitions of codes to // Processing Serial.write('1'); delay(50); Serial.write('2'); delay(50); Serial.write('3'); delay(50); } lastBt4 = millis(); } } delay(50); // slow down sketch a bit }