<< return to Pixycam.com

Pixy Arduino not responding to if then

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;

}}

I just found that the pixy.init statement in setup interferes with the operation of the digital pins. If this statement is commented out, the pins respond to the code and work normally. If pixy.init is not commented out, the digital pins are non responsive and seem to be stuck in either off or on states. Is there any way to use these pins for output while pixy.init is running?

Hi Charles,

I see that you are using pin 13 for an LED. Pixy also uses pin 13 for the SPI clock. You can look at the “Input and Output” section of the “Arduino”:http://arduino.cc/en/Main/ArduinoBoardUno board pages and see which pins are used for communication. I don’t think you can change the pins used for SPI comm., unless it has multiple SPI controllers. Is it not possible for you to use a different pin?

Scott