Vision Sensor Cube Counter Help

We are trying to use the vision sensor to count the cubes that we have collected and also to sort them by colours. We have tried configuring the sensor multiple times and it seems to be able to identify the different colours. However, when we try and count the ones that it has seen and print them on the brain screen, only the purple seems to count. It is programmed to take a snap shot every second. Why aren’t the other colours being counted? Our code is attached.
Thanks,
56J

// ---- START VEXCODE CONFIGURED DEVICES ----
// Robot Configuration:
// [Name] [Type] [Port(s)]
// CubeCounter vision 10
// ---- END VEXCODE CONFIGURED DEVICES ----

#include “vex.h”

using namespace vex;

int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
//task TowerCount;
float purpleCubeCount = 0;
float greenCubeCount = 0;
float orangeCubeCount = 0;
while(true){
CubeCounter.takeSnapshot(1);
if (CubeCounter.largestObject.exists == true && CubeCounter.largestObject.width>=5 && CubeCounter.takeSnapshot(CubeCounter__PURPLE_CUBE) == true){
purpleCubeCount= purpleCubeCount + 1;
} else if (CubeCounter.largestObject.exists == true && CubeCounter.largestObject.width>=5 && CubeCounter.takeSnapshot(CubeCounter__GREEN_CUBE) == true){
greenCubeCount= greenCubeCount + 1;
} else if (CubeCounter.largestObject.exists == true && CubeCounter.largestObject.width>=5 && CubeCounter.takeSnapshot(CubeCounter__ORANGE_CUBE) == true){
orangeCubeCount= orangeCubeCount + 1;
}else{
}
Brain.Screen.printAt(1, 40,“%f”, purpleCubeCount);
Brain.Screen.printAt(1, 120,“%f”, orangeCubeCount);
Brain.Screen.printAt(1, 80,“%f”, greenCubeCount);
task::sleep(1000);
}
}
// /----------------------------------------------------------------------------/
// /* /
// /
Module: main.cpp /
// /
Author: C:\Users\Robotics /
// /
Created: Thu Sep 26 2019 /
// /
Description: V5 project /
// /
/
// /
----------------------------------------------------------------------------*/
// #include “vex.h”
//
// // ---- START VEXCODE CONFIGURED DEVICES ----
// // Robot Configuration:
// // [Name] [Type] [Port(s)]
//

First up, you have the wrong forum category. This code is from VEXcode, not Robot Mesh Studio. Second up, you have your counts being computed in different sections of an if…else if…else block, so only one will ever run. Third, testing for takeSnapshot(sig) == true is barking up the wrong tree, as takeSnapshot returns an int, not a bool. Fourth, the initial takeSnapshot(1) is probably not what you want, as it will only look for one color of cube that got registered as signature 1. If you want takeSnapshot to match any signature, you have to use 0 as the argument.

1 Like