Having trouble with two onColor blocks

I have some trouble with executing 2 onColor blocks. Following is my equivalent java script and I have also attached the image of the block program. Basically I have 2 onColor event blocks. One for yellow and one for read. On each event I am turning left. When I try to execute the program it only turns left on the first colour and doesn’t react on the second color.

  • If there is yellow and then red, then it turns left on yellow but not on red.
  • If there is red and then yellow, then it turns left on red but not on yellow.
  • I tried to execute the program with both Mac and Chromebook but the result is same.
var _colors = [{"r":236,"g":37,"b":46},{"r":255,"g":231,"b":69}];

async function startProgram() {
	listenForColorSensor(_colors);
	await delay(0.05);
	setMainLed(getColor());
	await roll(0, 20, 12);
	stopRoll();
	exitProgram();
}

async function onColor(color) {
	if (color.r !== 236 || color.g !== 37 || color.b !== 46) return;

	await roll(90, 20, 6);
	exitProgram();
}
registerEvent(EventType.onColor, onColor);

async function onColor_2(color) {
	if (color.r !== 255 || color.g !== 231 || color.b !== 69) return;

	await roll(90, 20, 6);
	exitProgram();
}
registerEvent(EventType.onColor, onColor_2);

After further debugging I have found some details.

When an event is triggered, it fully completes before the next event is fired. So the exitProgram() in my above program don’t help. I wanted it to stop if no further events are triggered but it will actually stop my program after the first event. So the following actually works

var _colors = [{"r":236,"g":37,"b":46},{"r":236,"g":37,"b":46}];

async function startProgram() {
	listenForColorSensor(_colors);
	await delay(0.05);
	setMainLed(getColor());
	await roll(0, 20, 12);
	exitProgram();
}

async function onColor(color) {
	
	if (color.r !== 236 || color.g !== 37 || color.b !== 46) return;
	await roll(90, 20, 5);
		
}
registerEvent(EventType.onColor, onColor);

async function onColor_2(color) {	
	
	if (color.r !== 236 || color.g !== 37 || color.b !== 46) return;
	await roll(180, 20, 5);
}

registerEvent(EventType.onColor, onColor_2);

While this is not how the event driven programming is supposed to work, it may be a limitation now which can be fixed through further upgrades. I will leave this open for others to add their comments.

My example above is wrong. And following is the correct one for anyone interested.

var _colors = [{"r":252,"g":62,"b":69},{"r":255,"g":234,"b":76}];

async function startProgram() {
listenForColorSensor(_colors);
await delay(0.05);
setMainLed(getColor());
await roll(0, 20, 12);
exitProgram();
}

async function onColor(color) {
if (color.r !== 252 || color.g !== 62 || color.b !== 69) return;

await speak('RED', true);
setHeading(270);

}
registerEvent(EventType.onColor, onColor);

async function onColor_2(color) {
if (color.r !== 255 || color.g !== 234 || color.b !== 76) return;

await speak('YELLOW', true);
setHeading(90);

}
registerEvent(EventType.onColor, onColor_2);

SPHERO Email Marketing -