1
Arduino support / Re: Arduino Plugin
« on: June 21, 2015, 04:12:04 AM »
Hi James
Thank you for the advice on VC responding to inputs on the Arduino.
It all makes a great deal of sense now.
By triggering events with digital inputs on Arduino, one can literally do anything. I went from the idea of VC letting me know when the front door or garage door is opening, to a multimedia interface where i can play, pause and skip music from my entertainment area with buttons or voice.
Here is the code I used to trigger multiple inputs.
Thank you for the advice on VC responding to inputs on the Arduino.
It all makes a great deal of sense now.
By triggering events with digital inputs on Arduino, one can literally do anything. I went from the idea of VC letting me know when the front door or garage door is opening, to a multimedia interface where i can play, pause and skip music from my entertainment area with buttons or voice.
Here is the code I used to trigger multiple inputs.
Code: [Select]
String data=""; //used to receive serial messages
const int buttonPin1 =2;
const int buttonPin2 =3;
const int buttonPin3 =4;
const int buttonPin4 =5;
bool flag_1 = 0;
bool flag_2 = 0;
bool flag_3 = 0;
bool flag_4 = 0;
bool buttonState_1 = 0;
bool buttonState_2 = 0;
bool buttonState_3 = 0;
bool buttonState_4 = 0;
void setup() {
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
Serial.begin(9600);
}
void loop() {
delay(100);
int oneButtonState = digitalRead(buttonPin1);
if (oneButtonState != buttonState_1) {
if(oneButtonState)
{
if(flag_1 == 0)
{
Serial.println("VC.TriggerEvent&&ButtonOne.Pressed");
flag_1 = 1;
}
}
else
{
if(flag_1 == 1)
{
Serial.println("VC.TriggerEvent&&ButtonOne.State.Released");
flag_1 = 0;
}
}
buttonState_1 = oneButtonState;
}
delay(100);
int twoButtonState = digitalRead(buttonPin2);
if (twoButtonState != buttonState_2) {
if(twoButtonState)
{
if(flag_2 == 0)
{
Serial.println("VC.TriggerEvent&&ButtonTwo.Pressed");
flag_2 = 1;
}
}
else
{
if(flag_2 == 1)
{
Serial.println("VC.TriggerEvent&&ButtonTwo.Released");
flag_2 = 0;
}
}
buttonState_2 = twoButtonState;
}
delay(100);
int threeButtonState = digitalRead(buttonPin3);
if (threeButtonState != buttonState_3) {
if(threeButtonState)
{
if(flag_3 == 0)
{
Serial.println("VC.TriggerEvent&&ButtonThree.Pressed");
flag_3 = 1;
}
}
else
{
if(flag_3 == 1)
{
Serial.println("VC.TriggerEvent&&ButtonThree.Released");
flag_3 = 0;
}
}
buttonState_3 = threeButtonState;
}
delay(100);
int fourButtonState = digitalRead(buttonPin4);
if (fourButtonState != buttonState_4) {
if(fourButtonState)
{
if(flag_4 == 0)
{
Serial.println("VC.TriggerEvent&&ButtonFour.Pressed");
flag_4 = 1;
}
}
else
{
if(flag_4 == 1)
{
Serial.println("VC.TriggerEvent&&ButtonFour.Released");
flag_4 = 0;
}
}
buttonState_4 = fourButtonState;
}
}
void serialEvent() {
data="";
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the data:
if (inChar == (char)10)
{
break; //break message on newline
}
data += inChar;
}
//data should be ready now. What should we do with it?
if (data=="hello")
{
Serial.println("tts.speak&&hi there");
}
//Analog Read - Pin #
else if (data.startsWith("ar."))
{
String strPin = data.substring(3);
int whatPin = atoi(strPin.c_str());
int v = analogRead (whatPin);
Serial.println("VC.TriggerEvent&&Analog.Pin&&"+strPin+"&&"+v);
}
//DigitalWrite HIGH - Pin #
else if (data.startsWith("dw.high."))
{
String strPin = data.substring(8);
int whatPin = atoi(strPin.c_str());
pinMode(whatPin, OUTPUT);
digitalWrite (whatPin,HIGH);
}
//DigitalWrite LOW - Pin #
else if (data.startsWith("dw.low."))
{
String strPin = data.substring(7);
int whatPin = atoi(strPin.c_str());
pinMode(whatPin, OUTPUT);
digitalWrite (whatPin,LOW);
}
//unknown command
else
{
Serial.println("VC.TriggerEvent&&Arduino.error&&"+data);
}
//clear data for next time
data="";
}