The board fired up and started the blink sketch as soon as I plugged it into my USB connector. Using both the Arduino IDE and VS2010 with the Arduino VisualMicro plugin, the board was easy to program. I would even say trivial. I don't have experience with other Arduino boards but this one easily re-loads a sketch without physically rebooting the board before download. It's auto-magic.
For my first project I connected (from left to right) a red led, slide switch, vibrator, passive infrared (PIR) and white LED modules into the Arduino Grove base shield. The white LED indicates the PIR sensor status and the vibrator module, if enabled, buzzes once when movement is sensed. I added the switch to enable or disable the vibrator module because my cats were circling the vibrating prey ready to slap the snot out of it if it buzzed just one more time. The red LED indicates if the vibrator module (cat warning) is activated.
I used the code samples from Seeed Studio (many thanks) for each of these modules to create the application, only changing the port assignments as needed.
const uint8_t pirPin = 2; // PIR sensor
const uint8_t ledPin = 4; // White LED
const uint8_t vibPin = 6; // vibration module
const uint8_t onOffPin = 8; // slide switch
const uint8_t redLedPin = 10; // red LED
void setup()
{
pinMode(pirPin, INPUT ); // receive signal from PIR module
pinMode(onOffPin, INPUT ); // switch enables vibrate module
pinMode(ledPin, OUTPUT); // light when motion detected
pinMode(vibPin, OUTPUT); // vibrate when motion detected
pinMode(redLedPin, OUTPUT);// light when vibrator enabled
Flash(5);
}
void loop()
{
int pirValue = digitalRead(pirPin);
int onOffValue = digitalRead(onOffPin);
digitalWrite(redLedPin,onOffValue);
bool vibrate = false;
if( pirValue == 1 )
digitalWrite(ledPin,HIGH);
else
digitalWrite(ledPin,LOW);
if(IsEdgeGoingTrue(pirValue) && onOffValue > 0)
{
digitalWrite(vibPin,HIGH);
delay(200);
digitalWrite(vibPin,LOW);
}
delay(250);
}
void Flash( int times )
{
for(int i=0; i<times; i++)
{
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
}
digitalWrite(vibPin,HIGH);
delay(500);
digitalWrite(vibPin,LOW);
}
// return true if state of signal goes from low to high
bool IsEdgeGoingTrue(bool signal)
{
static bool lastState = false; // init once
bool retval = false;
if(!lastState && signal)
retval = true;
lastState = signal;
return retval;
}
const uint8_t vibPin = 6; // vibration module
const uint8_t onOffPin = 8; // slide switch
const uint8_t redLedPin = 10; // red LED
void setup()
{
pinMode(pirPin, INPUT ); // receive signal from PIR module
pinMode(onOffPin, INPUT ); // switch enables vibrate module
pinMode(ledPin, OUTPUT); // light when motion detected
pinMode(vibPin, OUTPUT); // vibrate when motion detected
pinMode(redLedPin, OUTPUT);// light when vibrator enabled
Flash(5);
}
void loop()
{
int pirValue = digitalRead(pirPin);
int onOffValue = digitalRead(onOffPin);
digitalWrite(redLedPin,onOffValue);
bool vibrate = false;
if( pirValue == 1 )
digitalWrite(ledPin,HIGH);
else
digitalWrite(ledPin,LOW);
if(IsEdgeGoingTrue(pirValue) && onOffValue > 0)
{
digitalWrite(vibPin,HIGH);
delay(200);
digitalWrite(vibPin,LOW);
}
delay(250);
}
void Flash( int times )
{
for(int i=0; i<times; i++)
{
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
}
digitalWrite(vibPin,HIGH);
delay(500);
digitalWrite(vibPin,LOW);
}
// return true if state of signal goes from low to high
bool IsEdgeGoingTrue(bool signal)
{
static bool lastState = false; // init once
bool retval = false;
if(!lastState && signal)
retval = true;
lastState = signal;
return retval;
}
What is cool is that I ran this application, oops sketch, in Visual Studio with pin debug view on.
It is my first priority to eliminate the loop() as I believe polling is not the best solution for any application, even trivial toys. My plan is to move my applications to an event driven state machine. I currently have a self made minimized version of Arduino Quantum Leaps QP in evaluation and hope that it is a viable event driven model that I can use rather than a high current "loop forever mostly doing nothing at all" solution. More on that soon.
... and Mr. Spanky has his eye on the offending vibrator module.
... and Mr. Spanky has his eye on the offending vibrator module.
No comments:
Post a Comment