Hello,
I just received my Pixy a week ago and am pleased with it. I am having some trouble with a program that I want to do one thing; make the led pin 13 go high if two conditions are met. Condition 1: A signature (object) is present. 2. The object is not moving up or down the y axis. Below is some code that I have attempted. It lights ledpin13 on the Arduino when the object is present, regardless of if it is moving or not. Any help is appreciated. Thanks!
/*
ReadAnalogVoltage
Reads Pixy blocks and lights led pin 13 if the object is detected and is moving below a specified rate.
This example code is in the public domain.
*/
#include <SPI.h>
#include <Pixy.h>
const int ledPin = 13;
float Y = 0;
float lastY = 0;
float rate = 0;
unsigned long lastTime = 0;
unsigned long dt = 100; // dt in milliseconds
Pixy pixy;
void setup()
{
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
pixy.init();
}
void loop()
{
if (millis() - lastTime >= dt) // wait for dt milliseconds and does the following each 100 millis
{pixy.getBlocks();
//Imports the Pixy blocks
lastTime = millis();
int Y = pixy.blocks[1].y; // defines Y as the y-location of the object from Pixy
rate = (Y - lastY)/lastTime; //defines rate of movement of object
Serial.print("Rate: ");
Serial.println(rate);
if (rate <= 5)
{
digitalWrite(ledPin, HIGH);
}
else {digitalWrite(ledPin, LOW);
}
lastY = Y;
}}