what is the minumum delay requied in this moddified shift-out example code?

what is the minumum delay requied in this moddified shift-out example code?
(look at highlighted delays)

//**************************************************************//
// Name : shiftOutCode, Hello World
// Author : Carlyn Maw,Tom Igoe, David A. Mellis
// Date : 25 Oct, 2006
// Modified: 23 Mar 2010
// Version : 2.0
// Notes : Code for using a 74HC595 Shift Register //
// : to count from 0 to 255
//****************************************************************

int v2 = 0;
int v2a = 0;
int v2b = 0;
int v2c = 0;
int v1 = 0;
int v1a = 0;
int v1b = 0;
int v1c = 0;

//Pin connected to ST_CP of 74HC595
int latchPin = 3;
//Pin connected to SH_CP of 74HC595
int clockPin = 4;
////Pin connected to DS of 74HC595
int dataPin = 2;

void setup() {
//set pins to output so you can control the shift register
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
Serial.begin(9600);
}

void loop() {
// count from 0 to 255 and display the number
// on the LEDs
if(Serial.available() > 0){
delay(1);
v1a = Serial.read() - 48;
delay(1);
if(Serial.available() > 0){
v1b = Serial.read() - 48;
delay(1);
if(Serial.available() > 0){
v1c = Serial.read() - 48;
delay(1);
}
else{
v1c = v1b;
v1b = v1a;
v1a = 0;
}
}
else{
v1c = v1a;
v1a = 0;
v1b = 0;
}
v1 = (v1a * 100) + (v1b * 10) + v1c;
Serial.println(v1);
if(v1 > 255){
Serial.println(“Out of Range”);
v1 = 000;
Serial.print(“v1 = 000”);
}
}
// count from 0 to 255 and display the number
// on the LEDs
if(Serial.available() > 0){
delay(1);
v2a = Serial.read() - 48;
delay(1);
if(Serial.available() > 0){
v2b = Serial.read() - 48;
delay(1);
if(Serial.available() > 0){
v2c = Serial.read() - 48;
delay(1);
}
else{
v2c = v2b;
v2b = v2a;
v2a = 0;
}
}
else{
v2c = v2a;
v2a = 0;
v2b = 0;
}
v2 = (v2a * 100) + (v2b * 10) + v2c;
Serial.println(v2);
if(v2 > 255){
Serial.println(“Out of Range”);
v2 = 000;
Serial.print(“v2 = 000”);
}
// take the latchPin low so
// the LEDs don’t change while you’re sending in bits:
digitalWrite(latchPin, LOW);
// shift out the bits:
shiftOut(dataPin, clockPin, MSBFIRST, v2);
delay(1);
shiftOut(dataPin, clockPin, MSBFIRST, v1);
delay(1);
/*shiftOut(dataPin, clockPin, MSBFIRST, v1);
//delay(10);
*/

//take the latch pin high so the LEDs will light up:
digitalWrite(latchPin, HIGH);
// pause before next value:
delay(250);
}
}

The first bunch of delays don't have nothing to do with shiftOut() and I don't understand what the last two delays() are for.


Rob

I see no reason to have any of those highlighted delays in there at all.

if i remove the delays i get stuff like this

sent via usb → 255 & 111

read as 025
also read as 005

&

read as 001
also read as 001
also read as 001

with delay(1); or delay(10); i get this

sent via usb → 255 & 111

read as 255
&

read as 111

That's why you need to use start and/or end of packet markers, rather than rely on timing to know when a full packet has been received.

Graynomad: The first bunch of delays don't have nothing to do with shiftOut() and I don't understand what the last two delays() are for.


Rob

i was unsure wat was causeing the errors so i put delays() in where ever i had moddieffied the original code

PaulS: That's why you need to use start and/or end of packet markers, rather than rely on timing to know when a full packet has been received.

what do u mean? can u explain it or give a link?

What are you using to send the data to the Arduino?

Serial.read() or laptop usb conection using serial monitor's send function

The problem with serial monitor is it doesn't send any kind of delimiter, like CR/LF that could help you find the start/end of a set of numbers. Relying on delays is really error-prone (but you've found that out)

AWOL:
delimiter, // like CR/LF // start/end of a set of numbers.

delimiter: ← i have not heard of this word before
CR/LF: ← i have not heard of these before
start/end of a set of numbers.: ← what does this this mean

A delimiter is like punctuation, allowing you to parse the dataset more accurately.
http://www.google.co.uk/search?sourceid=navclient&hl=en-GB&ie=UTF-8&rlz=1T4GGLL_en-GBGB396GB397&q=define%3Adelimiter

CR/LF = carriage-return / line-feed

start/end of a set of numbers - well, err, the beginning and the…end of a group of numbers.

AWOL: A delimiter is like punctuation, allowing you to parse the dataset more accurately.

CR/LF = carriage-return / line-feed

start/end of a set of numbers - well, err, the beginning and the...end of a group of numbers.

that helps alot, thanks. but what whould u sugest i use to communicate in this code?

the options i can think of are:

-ardunio serial monitor -arduino 2nd version ethernet shield(need help with this(will post problems if this is a good way to communicate))

that is all i can think of that i know of...

It's not a matter of what you use to communicate. It's a matter of the data being put together prior to sending it across whatever you're using. For example12091285 ... what does that tell you? Anything at all? Now, what if it's formatted like this 120,91,285Now I can tell that there are three different values. Not only that, it tells me specifically how the original string is broken up. That's what a delimiter does for you. In this case, the delimiter is a comma, it "breaks up" the string into the respective parts.

Regardless of what you're using to communicate, it's the actual data that you need to interpret and figure out where or what the delimiters are and how to write your code to catch those. Now, if you're original data doesn't have delimiters in it, you have a bigger problem. Fix that first, and other things will be easier to deal with.

Check out this thread here: http://arduino.cc/forum/index.php/topic,55427.0.html

It discusses how to read in properly delimited and terminated serial data, and then how to parse out the values into the appropriate datatypes. Even provides usable sample code. That thread is specific to the Sparkfun Razor IMU, but the information provided should be useable for general Serial data parsing.

i did some tests... some delay is needed somwhere.

i added a delimiter checker with no delay at all... same probelem without the delimiter and no delays.

next i re-added the highlighted delays from post#1 as well as the delimiter... worked fine.

obviusly a delay is needed, also the datasheet for the 595 i'm using, if i recall correctly, said the maximum speed it can recive data was kinda near half of what my Arduino Mega is sending it at...

actual it's not the chip's delay but the serial.read() 's delay that if removed causeses problems

Read the thread jraskell linked - it'll help - really.

Your issue is that serial communications are very slow compared to the Arduino. Your nested ifs, although they are at least checking for serial.available, represent an assumption that additional characters will be there. They won't be. Once your arduino picks up one character, it won't find any more until some time later on another iteration of loop and will get only a single digit number to work with. The others you typed in to serial monitor, thinking that they would be grabbed together will also be picked up as single numbers. You need to gather them all first. Your delays achieve that, kind of, but it's not a good way to do it. You can gather the characters into a buffer and work on them when you have enough. You could require that all numbers have three digits & keep reading until you have them three (bad idea too). You could put a space after the number to indicate that you've sent all the digits you're going to - this is a delimeter.

Bottom line is, you have to do a bit more work in the code to achieve what you're looking for.

said the maximum speed it can recive data was kinda near half of what my Arduino Mega is sending it at...

Unlikely but possible.

Either way adding a delay between shifOuts() won't help as the damage is done inside shiftOut().

AFAIK the entire Arduino community uses shiftOut directly to a 595 without problems. If for some reason your situation is too fast then you can dick with the SPI hardware directly to slow the clock rate or write your own version of shiftOut (5 minutes work max).


Rob