grabScreen(): first two captures of a sequence are the same

Hi,
My application grabs frames form a webcam and displays them continuously using ofVideoGrabber.draw(). When I press a key the application saves a sequence of 25 screen captures using ofImage.grabscreen().

The problem is that in that sequence the first two images are the same, showing that during the first two captures the screen didn’t refreshed (the rest of the captures are ok). I noticed that when the application starts void update() is called twice before void draw() is called for the first time, but after that first loop, update() and draw() are called once per loop, as expected.

code for update(), draw() and KeyPressed():

  
void testApp::update(){  
  
	ofBackground(100,100,100);  
	  
	if (avanzar == true){  
	avanzarUnPaso();//FUNCTION THAT CONTROLS A STEPPER MOTOR  
	}  
	vidGrabber.grabFrame();  
}  

  
void testApp::draw(){  
  
	ofSetColor(0xffffff);  
	vidGrabber.draw(origenX, origenY);  
  
	if (adquirir == true){  
	capturar(); //FUNCTION THAT GRABS FROM THE SCREEN AND SAVES AS IMAGE  
	}  
  
}  

  
void testApp::keyPressed(int key){  
  
  
		if (key == 'v' || key == 'V'){  
		adquirir = true;  
		cantidadFotos = 27;  
		}  
}  

So, what happens after keyPressed is called ? does the program return to the next line of the code where it was before the keyPressed call? can keyPressed be called when the application is inside update() or draw()?

Thanks!

problem solved.

setting a boolean variable from keyPress I can ensure that the needed update of the screen takes place before I capture form the screen.

in case anyone wants to see the changes here is the code:

  
#include "testApp.h"  
  
//--------------------------------------------------------------  
void testApp::setup(){  
  
	origenX			= 20;  
	origenY			= 20;  
	camWidth 		= 320;	   
	camHeight 		= 240;  
  
	vidGrabber.setVerbose(true);  
	vidGrabber.initGrabber(camWidth,camHeight);  
  
	adquirir		= false;  
	avanzar			= false;  
	contadorFotos   = 0;  
  
	ofSetVerticalSync(true);  
	serial.enumerateDevices();  
	serial.setup("COM9",9600);  
  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
  
	ofBackground(100,100,100);  
	  
	if (avanzar == true){  
	avanzarUnPaso();  
	}  
	vidGrabber.grabFrame();  
  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
  
	ofSetColor(0xffffff);  
	vidGrabber.draw(origenX, origenY);  
  
	if (adquirir == true){  
	capturar();   
	}  
}  
  
//--------------------------------------------------------------  
  
void testApp::capturar(){  
  
		char nombreArchivo[255];  
		sprintf(nombreArchivo, "foto_%0.3i.bmp", contadorFotos);  
  
		char etiqueta[255];  
		sprintf(etiqueta, "%s", nombreArchivo);  
		ofDrawBitmapString(etiqueta, origenX+20, origenX+camHeight-20);  
  
		// capturar un rectangulo de la pantalla  
		imagen.grabScreen(origenX,origenY,camWidth,camHeight);  
  
		imagen.saveImage(nombreArchivo);  
		contadorFotos++;  
  
		if(contadorFotos == cantidadFotos){  
		avanzar = false;  
		adquirir = false;  
		contadorFotos = 0;  
		}  
  
}  
  
  
//--------------------------------------------------------------  
void testApp::avanzarUnPaso(){  
  
/*EL MOTOR DA 200 PASOS POR REVOLUCION POR LO QUE CADA PASO CORRESPONDE A UN DESPLAZAMIENTO  
DE 1,8º. EL PROGRAMA DEL MICROCONTROLADOR ESCUCHA EL PUERTO SERIAL Y ESPERA RECIBIR CARACTERES  
ASCII EN EL RANGO DE 0 A 8 Y ESOS NUMEROS CORRESPONDEN A LA CANTIDAD DE PASOS DE 1,8º QUE DA EL MOTOR   
CADA VEZ QUE SE LE MANDA UN MENSAJE SERIAL*/  
  
	//AVANZAR "UN PASO" EL MOTOR  
		serial.writeByte('8');  
		  
		//ESCUCHAR EL PUERTO SERIAL ESPERANDO UNA "R" QUE INDICA QUE EL MICROCONTROLADOR RECIBIO LA ORDEN DE DAR EL PASO  
		while(serial.available() < 0) {  
		//esperando "r"....  
		}  
		//llega "r":  
		lectura = serial.readByte();  
		serial.flush();  
		  
		adquirir = true;  
		ofSleepMillis(600); // retardo para que no gire demasiado rápido  
  
}  
  
  
  
//--------------------------------------------------------------  
void testApp::keyPressed(int key){  
  
		if (key == 's' || key == 'S'){  
		vidGrabber.videoSettings();  
	}  
  
		  
  
		if (key == 'v' || key == 'V'){  
		avanzar = true;  
		cantidadFotos = 26;  
		}  
}