//adapted from Example Code(Tom Igoe) http://www.arduino.cc/en/Tutorial/Button
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 1; // the number of the pushbutton pin
const int ledPin = 0; // the number of the LED pin not responsive to button
const int ledPin2= 2; // the number of the LED pin tied to button
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin2, OUTPUT);
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT); //sets button to input
digitalWrite(ledPin, HIGH); //turns on LED 1(n
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin2, HIGH); //turn on one led
} else {
// turn LED off:
digitalWrite(ledPin2, LOW);
}
}
The board worked out okay. One mistake. I tied the Analog input to pin 0(PWM) not Analog. Since that pin is tied to MOSI on the 6-pin header. I soldered a pin onto pin 3(A2). This allowed me to send a signal to that pin.
*/
// Code sample frome Analog Out example in Arduino IDE (Tom Igoe)
/*Modifications Dconner June 2016
* Turned Serial to Software serial
*/
#include
// These constants won't change. They're used to give names
// to the pins used:
const int analogInPin = A2; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 2; // Analog output pin that the LED is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
const int rx=3;
const int tx=2;
SoftwareSerial mySerial(rx,tx);
void setup() {
// initialize serial communications at 9600 bps:
mySerial.begin(9600);
}
void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
mySerial.print("out = ");
mySerial.println(sensorValue);
// wait 2 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(2);
}
//Code based on SimpleRead Example in Processing
//Edit by D Conner 2016
import processing.serial.*;
Serial myPort; // Create object from Serial class
int val; // Data received from the serial port
void setup()
{
size(1020, 400);
String portName = Serial.list()[2];// Port 2 is Com 3 on my computer
myPort = new Serial(this, portName, 9600);
val =510;//The sensor has an average initial value of 510.(Max is 1024, so this is almost half.)
}
void draw()
{
if ( myPort.available() > 0) { // If data is available,
String r = myPort.readString();//Read in a string
String[] outputValues = split(r, " = ");//The expected input is "Out = 'value'"
if(outputValues.length>1){ //Ensure the data has the Out value
if(outputValues[0].equals("out")){
try {
int n = Integer.parseInt(outputValues[1].trim());//Check to see if input is a integer.
println(n);
val = n;
} catch(NumberFormatException e) {}
}}
}//If statement(port available)
background(255); // Set background to white
if (val > 510) { // If the value is greater than 510, color bar green
fill(0,255,0);
rect(510, 50, val-510, 100);// make a bar that is the magnitude of the strength
}
else { // If the value is less than 510, color bar red. Draw bar equal in magnitude.
fill(255,0,0);
rect(val, 50, 510-val, 100);
}
}