Yeah, they should open just fine.
Here's the code. I've split them up in tabs to keep it a bit organized (thus the zip).
So, I figure it might be best to give you context so you're not confused. I have two puppets, Obama and Romney, with this motor as a Pinocchio nose that moves forward or backwards based on the truthfulness of a selected statement. The statements are selected via TouchOSC on an iPad.
I've attached my processing code below this one.
//=====INITIALIZE PINS========
int switchPin = 7; // switch input
int motor1APin = 6; // H-bridge leg 1 (pin 2, 1A) PWM
int motor2APin = 5; // H-bridge leg 2 (pin 7, 2A) PWM
int enablePin = 9; // H-bridge enable pin
//For proper lineTrack readings, insert 2 to A5, 3 to 5V, 1 to GND
int lineTrack = 5; //line track for position reading
int potVal;
int prevState;
int incomingByte; // for incoming serial data
//=============================
void setup(){
Serial.begin(57600);
// set the switch pin and line track as inputs:
pinMode(switchPin, INPUT);
pinMode(lineTrack, INPUT);
//set the motor logic pins and enable pin as outputs
pinMode(motor1APin, OUTPUT);
pinMode(motor2APin, OUTPUT);
pinMode(enablePin, OUTPUT);
}
void loop() {
// set enablePin high so that motor can turn on:
digitalWrite(enablePin, HIGH);
readSerial();
//Cases are named by previous state, followed by new state
lieTruth();
truthLie();
truthHalf();
lieHalf();
halfTruth();
halfLie();
}
//If previous state was lie and new state is a truth, move backwards all the way
void lieTruth (){
if (prevState == 2 && incomingByte == 65){
// //Obtain position reading
// int potVal = analogRead(lineTrack); //read the line positioning
// int Speed = map (potVal,potVal,0,255,0); //Map from 1023-0, HIGH-LOW
do
{
// analogWrite (enablePin, Speed);
motorBackward();
potVal = analogRead(lineTrack);
}
while (analogRead(lineTrack) >= 0);
if (potVal == 0){
motorStop();
}
}
}
//If previous state was truth and new state is a lie, move forward all the way
void truthLie(){
if (prevState == 1 && incomingByte == 66){
//Obtain position reading
// int potVal = analogRead(lineTrack); //read the line positioning
// int Speed = map (potVal,potVal,1023,255,0); //Map from 0-1023, HIGH-LOW
do
{
// analogWrite (enablePin, Speed);
motorForward();
potVal = analogRead(lineTrack);
}
while (analogRead(lineTrack) <= 1023);
if (potVal == 1023){
motorStop();
}
}
}
//If prevState was truth and new state half truth. Go forwards halfway
void truthHalf(){
if (prevState == 1 && incomingByte == 67){
//Obtain position reading
// int potVal = analogRead(lineTrack); //read the line positioning
// int Speed = map (potVal,potVal,512,255,0); //Map from 0-512, HIGH-LOW
do
{
// analogWrite (enablePin, Speed);
motorForward();
analogRead(lineTrack);
}
while (analogRead(lineTrack) >= 513);
if (potVal == 512){
motorStop();
}
}
}
//If prevState was lie and new state half truth. Go backwards halfway
void lieHalf(){
if (prevState == 2 && incomingByte == 67){
//Obtain position reading
// int potVal = analogRead(lineTrack); //read the line positioning
// int Speed = map (potVal,potVal,512,255,0); //Map from 1023-512 to HIGH-LOW
do
{
// analogWrite (enablePin, Speed);
motorBackward();
analogRead(lineTrack);
}
while (analogRead(lineTrack) >= 511);
if (potVal == 512){
motorStop();
}
}
}
//If prevState was half-truth and new state truth. Go backwards halfway
void halfTruth(){
if (prevState == 3 && incomingByte == 65){
//Obtain position reading
// int potVal = analogRead(lineTrack); //read the line positioning
// int Speed = map (potVal,potVal,0,255,0); //Map from 512-0, HIGH-LOW
do
{
// analogWrite (enablePin, Speed);
motorBackward();
analogRead(lineTrack);
}
while (analogRead(lineTrack) <= 513);
if (potVal == 0){
motorStop();
}
}
}
//If prevState was half-truth and new state lie. Go forwards halfway
void halfLie(){
if (prevState == 3 && incomingByte == 66){
//Obtain position reading
// int potVal = analogRead(lineTrack); //read the line positioning
// int Speed = map (potVal,potVal,1023,255,0); //Map from 512-1023, HIGH-LOW
do
{
// analogWrite (enablePin, Speed);
motorForward();
analogRead(lineTrack);
}
while (analogRead(lineTrack) >= 511);
if (potVal == 1023){
motorStop();
}
}
}
void motorForward(){
analogWrite(motor1APin, 30);
analogWrite(motor2APin, 160);
}
void motorBackward(){
analogWrite(motor1APin, 160);
analogWrite(motor2APin, 30);
}
void motorStop(){
analogWrite(motor1APin, 255);
analogWrite(motor2APin, 255);
}
void readSerial(){
int prevState;
//Allow previous states to be stored here in prevState
// 1 = truth
// 2 = lie
// 3 = half-truth
// read the incoming byte data from Processing:
if (Serial.available() > 0) {
incomingByte = Serial.read();
}
//Store data as previous states
if (incomingByte == 65) { //Truth
prevState = 1;
}
else if(incomingByte == 66) { //Lie
prevState = 2;
}
else if(incomingByte == 67) { //Half-truth
prevState = 3;
}
}
Processing code:
//libraries needed for arduino communication
import processing.serial.*;
//libraries needed for osc
import oscP5.*;
import netP5.*;
Serial arduinoPort; // Set arduinoPort as serial connection
OscP5 oscP5;
NetAddress myRemoteLocation;
//set/change port numbers here
int incomingPort = 8000;
int outgoingPort = 9000;
//set/change the IP address that the OSC data is being sent to
//127.0.0.1 is the local address (for sending osc to an application on the same computer)
String ipAddress = "127.0.0.1";
int truthVal; //truthValue
void setup()
{
size(100, 100);
background(0);
frameRate(25);
/* start oscP5, listening for incoming messages at port 8000 */
//for INCOMING osc messages (e.g. from Max/MSP)
oscP5 = new OscP5(this, incomingPort); //port number set above
arduinoPort = new Serial(this, Serial.list()[4], 57600); //port 4 is Arduino to USB
}
// Incoming osc message are forwarded to the following oscEvent method. Write to the arduino here--------
// This method is called for each OSC message recieved
void oscEvent(OscMessage theOscMessage)
{
println("### received an osc message.");
println("addrpattern: "+theOscMessage.addrPattern());
//Convert the OSC address pattern to a string
String addr = theOscMessage.addrPattern();
if (addr.equals("/o/truth") == true) //if the osc message reads obama truth
{
truthVal = 0; //Set truthVal to 0
}
else if (addr.equals("/o/lie") == true) //if the osc message reads obama lie
{
truthVal = 1; //Set truthVal to 1
}
else if (addr.equals("/o/halfTruth") == true)
{
truthVal = 2;
}
}
void draw()
{
//write serial data to for the Arduino to read
if (truthVal == 0) //if the osc message reads obama truth
{
arduinoPort.write(65);
println("Obama Truth");
}
else if (truthVal == 1) //if the osc message reads obama truth
{
arduinoPort.write(66);
println("Obama Lie");
}
else if (truthVal == 2) //if the osc message reads obama truth
{
arduinoPort.write(67);
println("Obama Half-Truth");
}
}