Show Posts
Pages: 1 ... 2247 2248 [2249] 2250 2251 ... 3290
33721  Using Arduino / Programming Questions / Re: I2C Help on: November 08, 2011, 09:28:55 am
  int data[8];
Why is this array sized to hold 8 ints?

  *sensor0 = data[0] << 8 + data[1];
  *sensor1 = data[2] << 8 + data[3];
  *sensor2 = data[4] << 8 + data[5];
  *sensor3 = data[6] << 8 + data[7];
Some parentheses might help.
  *sensor0 = (data[0] << 8) + data[1];
  *sensor1 = (data[2] << 8) + data[3];
  *sensor2 = (data[4] << 8) + data[5];
  *sensor3 = (data[6] << 8) + data[7];

Addition has a higher precedence than bit shifting, so data[0] is being shifted data[1] + 8 places in your code. Probably not what you expected.
33722  Using Arduino / Project Guidance / Re: making a weather station on: November 08, 2011, 09:17:16 am
The graphs there say that "(V - 0.8) / 0.03" should be the relative humidity, where V is the input voltage. (3.1 - 0.8) / 0.03 = 76.67% humidity. I have my window open and it rained an hour ago, so that seems right. But I don't know where it's getting 927 from.
The analogRead() function returns a value from 0 to 1023 that is a ratio of the input voltage to the reference voltage (input voltage = 0 gives 0 output; input voltage = reference voltage gives 1023 output).

To determine the voltage that the analogRead() value corresponds to, you need to know the reference voltage. Typically, this is 5V, but that can be lower when running on weak batteries or external power that is below spec, or it can be 3.3V on some boards, or it can be something different, if a voltage is applied to the VRef pin.

Assuming that the board is a 5V board, and that nothing is applied to the VRef pin, simply multiply the analogRead value by 5.0 and divide by 1023.0, and store the result in a float.
33723  Using Arduino / Interfacing w/ Software on the Computer / Re: problem with commnunicating arduino with processing (both sending and receiving) on: November 08, 2011, 08:44:54 am
i did modified the code in arduino, yet it still stuck in the middle.
I have no idea what "it still stuck in the middle" means. I have no idea what "it" is.

However, the Processing code is going to buffer serial data until it receives a carriage return, because you told it to:

So, it might be useful in the Arduino code to actually send a carriage return, now and then. These are all of your serial statements:
  if (Serial.available()>0) {
    check =;
None of these sends a carriage return.

Change the Serial.print() calls to Serial.println() calls to send the data followed by a carriage return/line feed.
33724  Using Arduino / Programming Questions / Re: Speedometer simple code addition? on: November 08, 2011, 08:34:18 am
We might be a bunch of grumpy old men, but we aren't deaf. You can stop shouting.

You can start posting the modifications that you made to this code. You already said this code works. It's the modifications that you made that are causing you problems, so hiding them behind your back is not how you get help.
33725  Using Arduino / Programming Questions / Re: I2C Help on: November 08, 2011, 08:28:41 am
A peek at the Wire.cpp file might prove useful.
void TwoWire::send(int data)
What happens when the int output by analogRead() is cast to a byte? Depends on the value, but for 3/4 of the range of values that analogRead can return, the results are not good.

Why does this matter to you? Because of this:
This invokes the int version of TwoWire::send().

Since you are (usually) sending garbage, the failure to retrieve it properly is not surprising.

On the master, the function requestSensor could use an array of values, rather than 4 individual values. It could use reference arguments instead of pointers.

Some Serial.print() statement on the slave and on the master would confirm how much data (and what data) was sent (mangled or not) and how much data and what data was received (mangled or not).

On the sender, you could put all the data in an int array, then send the data like so:
int dataToSend[4];
dataToSend[0] = analogRead(photocellPin0);
dataToSend[1] = analogRead(photocellPin1);
dataToSend[2] = analogRead(photocellPin2);
dataToSend[3] = analogRead(photocellPin3);
Wire.send((byte *)dataToSend, 4*sizeof(int));

Of course, this will send 8 bytes. Why you are expecting 16 bytes is a mystery.
33726  Using Arduino / Storage / Re: SD card breakout board on: November 08, 2011, 07:48:40 am
ALL interaction with the SD card, data in and data out, is at 3.3V. Just "powering" the holder at 3.3 volts doesn't make sense.

You need to incorporate the entire schematic of the breakout board into your schematic.
33727  Using Arduino / Programming Questions / Re: Porting a program from processing to arduino on: November 08, 2011, 07:20:56 am
Processing sketches are written in Java. Arduino sketches are written in C/C++. You can not compile Java source code using a C++ compiler.
33728  Using Arduino / Programming Questions / Re: Minimising coding on: November 08, 2011, 07:11:15 am
Do you see a difference between these two "function calls"?

The first one is NOT a function call. It is missing the parentheses.

By the way, there is no need to have a return statement at the end of a void function. An int function, yes, because that is how you define the value to return. But, a void function has nothing to return, and a return is automatic at the end of a function.
33729  Using Arduino / Interfacing w/ Software on the Computer / Re: Help! USB I/O camera UART on: November 08, 2011, 07:05:42 am
In your prior 32 posts, what's the first thing that people have asked?

Got links to the device(s) you are asking about?

Is there any reason we need to ask again?
33730  Using Arduino / Interfacing w/ Software on the Computer / Re: send a byte array to arduino over serial from processing? on: November 08, 2011, 07:03:09 am
Since you are sending one byte from the Processing application, why are you expecting 512 bytes?

What is the Processing application eventually going to be doing?

Crossroads is right. You need to figure out a way to transfer the data in 128 byte or smaller segments.

Even of the Arduino did have a 512 byte buffer, and Processing was sending 512 bytes, it could conceivably take more than a second to send all the data. Giving up after a second, a flushing every that has arrived but not been read is a bad idea. (Giving up isn't. Flushing is.)
33731  Using Arduino / Programming Questions / Re: Minimising coding on: November 08, 2011, 06:44:12 am
how can I include them in a function when I need to be switch specific i.e. UP/DOWN/LEFT/RIGHT/PRESS?
The only difference between the two calls is the string to display.
void showStuff(char *whatToShow)
   oled.drawstringblock(5, 5, 0, oled.RGB(255, 255, 0), 4, 4, whatToShow);

Then call it
33732  Using Arduino / Programming Questions / Re: New to C++, need help. Sending Multiple Values in String on: November 08, 2011, 06:11:08 am
  int length = meetAndroid.stringLength();
  char data[length];
This is not the proper way to dynamically allocate an array. First, the array is not large enough to hold the trailing NULL that must be present.

Second, you should be using malloc() to allocate the space, and free() to free it when you are done.
33733  Using Arduino / Programming Questions / Re: NewSoftSerial on Mega's pins 2 and 3 on: November 08, 2011, 05:57:17 am
The only pins in common between the UNO and the Mega that NewSoftSerial 11 is compatible with are 10 to 13, which are the SPI pins. I really wouldn't recommend reserving any of them for your shield.
33734  Using Arduino / Programming Questions / Re: NewSoftSerial - how to get 512 bytes buffer without SRAM overflow? on: November 08, 2011, 05:48:18 am
I'm currently using a fixed global char[200] to read from NewSoftSerial, because it "feels like" using String doesn't "seem to" help with memory stability.
The best thing that you can do is get rid of all String objects. Look at the String class. Specifically, look at the constructor and the += operator functions. A String is allocated EXACTLY large enough to hold the initial string. When a character is added to the String, more memory is allocated, to hold the initial string plus the new string (usually one character). The data in the old space is copied to the new space. The data to be appended is copied to the new space. Then the old space is freed.

When the next character to be added, the space used by the String object before is a little too small for the new String, resulting in small blocks of space all over the place not being large enough to reuse, but not being contiguous, either.

C/C++ has no mechanism to relocate allocated memory, so eventually, (pretty quickly, sometimes), you no longer have enough memory for malloc to succeed. Things get ugly when this happens.

On a UNO, 512 bytes for a buffer is 1/4 of SRAM. If you have much else going on, reserving that much for NewSoftSerial to use as a buffer is not a great idea.

Better would be to make sure that you are pulling data out of the buffer faster than the other device is making NewSoftSerial fill it.

Of course, without seeing your code, best wishes is all we can offer. So, good luck.
33735  Using Arduino / Programming Questions / Re: hold button on: November 08, 2011, 05:33:18 am
Why don't you try something. You need to keep track of the previous switch state. Read the current switch state (currState) at the start of loop, and copy the value to the previous state (prevState) at the end of loop.

Something like this:
int currState;
int prevState = LOW;

int somePin = 4; // Put your value here

void loop()
   currState = digitalRead(somePin);

   // Do some stuff

  prevState = currState;

Then you need to detect transitions, from released to pressed and from pressed to released. How do you know when a transition has occured? That's easy. The values in currState and prevState will be different.
if(currState != prevState)
  // A transition occurred

When the transition is from released to pressed, record the pressed time. When the transition is from pressed to released, record the released time.

unsigned long pressedTime;
unsigned long releasedTime;

This goes in the if block:
if(currState == ????) // Replace ???? with HIGH or LOW, whichever means pressed for you
  pressedTime = millis();
  releasedTime = millis();
  // Compute the time the switch was pressed

The time that the switch was pressed is releasedTime - pressedTime. Once you know how long the switch was pressed, you can make decisions based on that time.
Pages: 1 ... 2247 2248 [2249] 2250 2251 ... 3290