IR serial link working, but with delay

I am making a project using a simple IR link connected to the serial ports of the 328 chip.

I have made a remote control that works well over a great distance ( I dont have to point it at the receiver, it bounces round the walls and is picked up )

The code I am using ( the poster didn’t put his name on it ) is :-

char buf [5];
char key;
int group = 0 ;
const byte numChars = 90;
char receivedChars[numChars];

boolean newData = false;

void setup() {
	Serial.begin(2400);
	Serial.println("<Arduino is ready -->");
}

void loop() {
	recvWithStartEndMarkers();
	showNewData();
	doOtherStuff(); // <<=== NEW  --- This just simulates time spent elsewhere in a big program
}

void recvWithStartEndMarkers() {
	static boolean recvInProgress = false;
	static byte ndx = 0;
	char startMarker = '<';
	char endMarker = '>';
	char rc;
	
	// if (Serial.available() > 0) {
	while (Serial.available() > 0 && newData == false) { // <<== NEW - get all bytes from buffer
		rc = Serial.read();

		if (recvInProgress == true) {
			if (rc != endMarker) {
				receivedChars[ndx] = rc;
				ndx++; //Serial.print(" ndx = ");Serial.println(int ( ndx ));
buf [ndx] =  rc;
				if (ndx >= numChars) {
					ndx = numChars - 1;
				}
			}
			else {
				receivedChars[ndx] = '\0'; // terminate the string
				recvInProgress = false;
				ndx = 0;
				newData = true;
			}
		}

		else if (rc == startMarker) {
			recvInProgress = true;
		}
	}
}

void showNewData() {
	if (newData == true) {
		Serial.print("This just in ... ");
		Serial.println(receivedChars);
		newData = false;
grabkey ();


	}
}

void doOtherStuff() {
  
 
  
	//   delay(20); // <<===NEW - This just simulates time spent elsewhere in a big program
}

I removed the delay in DoOtherstuff to get it fastest.

I have 5 buttons on the remote, and if I press a different button each time, it receives perfectly, but I cannot send the same button within a second or so.

I am using keypad.h in the remote, and the signal is coming out of the receiver ok, but I think it is the if (newData == true) part that makes it wait - perhaps letting another 90 chars come in ? ( I dont understand the code too well )

I want to be able to hold down a button and increment a counter at the receiver.

I have done this with VirtualWire in the past OK, but I cannot use that as I am using the timer to generate the 38 Khz carrier in the remote.

Any suggestions ?

( I am using v22 as I wanted to try Serialflush to deal with a hang up after it had been running a while, but I have disabled it for now, and willl use a watchdog later to flush after a few seconds of inactivity )

Boffin1: I am making a project using a simple IR link connected to the serial ports of the 328 chip.

I have made a remote control that works well over a great distance ( I dont have to point it at the receiver, it bounces round the walls and is picked up )

The code I am using ( the poster didn't put his name on it ) is :-

void showNewData() {
 if (newData == true) {
 Serial.print("This just in ... ");
 Serial.println(receivedChars);
 newData = false;
grabkey ();

} }




I removed the delay in DoOtherstuff to get it fastest.

Any suggestions ?

what is the grabkey()?

Chuck.

Hi Chuck

Its a routine that is going to check out the " password" which is SCORE, and the one character - either U, D, L, R, S for up, down, left, right, and store.

So far it is just :-

void grabkey() {
  
  if( (buf[1] =='S') &&  (buf[2] =='C') &&(buf[3] =='O') &&(buf[4] =='R') &&(buf[5] =='E') )
  { key = buf [6]; Serial.print("key in grabkey =  ");Serial.println(char ( key ));
  
  
for ( int J = 1; J <=6;J++ ) { buf [J] = 0 ;}  //  saved key , so clear buffer and serial buffer
//Serial.flush() ;
 
  Serial.println("test"); 
 
  }// END OF IF SCORE
 
} // end grabkey

I ditched the 38Khz timer generation, dead-bugged an ubiquitous 555 timer on the back of the minipro arduino, and am now using good old VirtualWire. :-)

It took 5 minutes, after hours of trying to get the original working !