My relay is not functioning at all, it is a code error because it works with a simple code.
My relay is on pin 13 and my device is connected to the NO ( Normally Open), in the while loop I want the relay to close so I put digitalWrite(13, LOW); But it is not working at all.
Below is my code please try to assist
#include <SPI.h>
#include <Pixy2.h>
Pixy2 pixy;
////////////////////////////////////////////////////////
// ENA IN1 IN2 IN3 IN4 ENB
int myPins[6] = {5, 6, 7, 8, 9, 10}; // all Digital pins from the motor driver
int enable1 = 5; // Motor Driver Enable 1
int enable2 = 10; // Motor Driver Enable 2
static int i=0;
int signature, x, y, width, height;
unsigned int newarea = 0;
void setup() {
Serial.begin(115200);
Serial.print("Starting...\n");
// pixy.init();
pinMode(13, OUTPUT); // for the relay of the collection system
for (int i = 0; i < 6; i++) {
pinMode(myPins[i], OUTPUT);
}
pixy.init();
pixy.setLamp(0,0); // switching off the Pixy built in Lights
// digitalWrite(13,HIGH) ;
}
void loop() {
// Detection system
uint16_t blocks; // Maximum number of blocks that getBlocks() will return.
blocks = pixy.ccc.getBlocks(); //receive data from pixy
signature = pixy.ccc.blocks[i].m_signature; //get object's signature
x = pixy.ccc.blocks[i].m_x; //get x position
y = pixy.ccc.blocks[i].m_y; //get y position
width = pixy.ccc.blocks[i].m_width; //get width
height = pixy.ccc.blocks[i].m_height; //get hight
/////////////
// digitalWrite(13,HIGH) ;
if(signature == 1) {
// digitalWrite(13,LOW) ;
delay(350);
digitalWrite(enable1, LOW);
digitalWrite(enable2, LOW);
delay(30);
moveRobot(-110,0);
delay(500); // time to centre the robot to the ball
digitalWrite(enable1, LOW);
digitalWrite(enable2, LOW);
// code of area
///////////////////
newarea = width * height; //calculate the object area
Serial.print(newarea); // print area of the ball
Serial.print('\n');
while (newarea < 3000 && signature == 1)//go forward if object too small
{
// delay(10);
digitalWrite(13,LOW) ; // Start the vacume
moveRobot(100,100); //go forward
delay (10);
blocks = pixy.ccc.getBlocks(); //receive data from pixy
signature = pixy.ccc.blocks[i].m_signature; //get object's signature
x = pixy.ccc.blocks[i].m_x; //get x position
y = pixy.ccc.blocks[i].m_y; //get y position
width = pixy.ccc.blocks[i].m_width; //get width
height = pixy.ccc.blocks[i].m_height; //get Height
newarea = width * height; //calculate the object area
Serial.print(newarea); // print signature area
// Serial.print("NEXT --->");
Serial.print('\n');
}
delay (50);
digitalWrite(enable1, LOW);
digitalWrite(enable2, LOW);
//digitalWrite( 13,LOW) ;
// delay (10);
delay (5000) ; // time for collection system to work
digitalWrite(13,HIGH) ;
}
else {
digitalWrite(13,HIGH) ; // Start the vacume
moveRobot(140,0); // keep searching in circle for the ball
delay (10);
}
}
void moveRobot(int leftSpeed, int rightSpeed)
{
if (leftSpeed >= 0) {
digitalWrite(myPins[1], 0);
digitalWrite(myPins[2], 1);
}
else {
digitalWrite(myPins[1], 1);
digitalWrite(myPins[2], 0);
}
if (rightSpeed >= 0) {
digitalWrite(myPins[3], 0);
digitalWrite(myPins[4], 1);
}
else {
digitalWrite(myPins[3], 1);
digitalWrite(myPins[4], 0);
}
analogWrite(myPins[0], abs(leftSpeed));
analogWrite(myPins[5], abs(rightSpeed));
}