Show Posts
Pages: 1 2 3 [4] 5 6 ... 48
46  Forum 2005-2010 (read only) / Syntax & Programs / Re: LDR on: November 07, 2008, 05:28:31 pm
lol I still have sleep in my eyes and the nuke is busy churning out the coffee = )
47  Forum 2005-2010 (read only) / Syntax & Programs / Re: LDR on: November 07, 2008, 06:20:18 am
yup = )
48  Forum 2005-2010 (read only) / Syntax & Programs / Re: LDR on: November 07, 2008, 03:58:59 am
Your using one of the analog pins? if so, analogRead gives values from 0 to 1024. 0 equals 0V, 1024 equals 5V.

So ...

800 is 3.9v
300 is 1.46v

= )

49  Forum 2005-2010 (read only) / Syntax & Programs / Re: How to make an I2C master wait before sending on: October 25, 2008, 02:16:24 pm
For sure. When the master polls the slave every 1ms, is that interrupting the shift?
50  Forum 2005-2010 (read only) / Syntax & Programs / Re: How to make an I2C master wait before sending on: October 24, 2008, 08:44:40 pm
Is there any way to estimate the amount of time it takes for one character to be shifted?

If the master sends 20 characters, and it takes the slave half a second to shift them out, then perhaps the master could wait half second after sending the characters before attempting to send more?
51  Forum 2005-2010 (read only) / Syntax & Programs / Re: How to make an I2C master wait before sending on: October 24, 2008, 08:23:09 pm
Thats an interesting scenario. Have you tried running both as masters?
52  Forum 2005-2010 (read only) / Syntax & Programs / Re: MAC OS X c++ question on: October 12, 2008, 06:41:24 am
I thought it also worth mentioning that its possible to run c++ applications from php using the PHP exec() function.

We can also use the passthru PHP function to execute a $command variable as an external Unix program and pass the raw output back to the web browser, like so:-

$command = "/Applications/Mamp/htdocs/CppApp " . escapeshellcmd($args);
passthru($command);

If you already have the g++ complier then you don't need to use Xcode, though Xcode works just as well I've tested both.

Using the sample c++ code above, you can copy it into a text editing program, and save it in your htdocs directory as CppApp.cpp

Open terminal, and type
which g++

This will display the path to g++, if you don't have it you can search google for a version for your platform and download it.

In terminal, change into the directory where you saved the CppApp.cpp file, using "cd", in my case, that looked like this:-

cd '/Applications/Mamp/htdocs/'

Now use the G++ compiler to compile the .cpp file into an object file containing machine code. Use the G++ command like this:

g++ -c CppApp.cpp

If you check the directory you'll see that a CppApp.o has been created which is the machine code version of the cpp file, and now we want to create an executable file from that object file like this:-

g++ CppApp.cpp –o CppApp

This creates a new executable file named "CppApp". Unix executables do not have extensions.

The executable can now be run from php using the methods described above.

This offers another method of controlling Arduino over www over USB without needing to use an ethernet shield.

I use MAMP with Apache php 5 and mysql installed on OSX 10.3.9. With an Arduino NG connected over USB.

Its possible to pass variables to the executable and read results back through the browser window using php, so it could be used with writing sensor data back to a mySql database ... I discovered thats a lot easier to do using php than it is using c++ =)

53  Forum 2005-2010 (read only) / Syntax & Programs / Re: MAC OS X c++ question on: October 11, 2008, 02:15:39 pm
This thread is old, but its "up there" if you happen to be searching for c++, and it stands out on the radar if you also happen to be looking for "OSX" c++

Unfortunately, its also "incomplete", so I've added a routine to "read" from the serial port, as well as write to it.

This has been tested on OSX 10.3.9 - Xcode 1.2

This program works with a version of my "Toggle LED" sketch.  

First, it sends a chr(10) handshake to Arduino (write(fd, "\n", 1)).
Then you have the option of sending:-
write(fd, "0", 1); // Turns the LED OFF
write(fd, "1", 1); // Turns the LED ON
write(fd, "2", 1); // Asks Arduino to Send the value state of the LED
Finally, we send a chr(13) to tell Arduino we've finished talking to it (write(fd, "\r", 1)).

The program automatically starts reading from the serial port after writing to it, if nothing comes back, then it times out after about half a second. The value of the timeout depends on how much of a delay you used in the Arduino sketch between the time the data is received and when a reply is sent back plus it gives the port a chance to receive data before deciding nothing will arrive. This can be tweaked if your running a loop, so if the value of the serial buffer is greater than 0, then you would do a read without using a time out.

To get the name of the USB serial port, you can open Terminal, then type:-

ls /dev/tty.*

Edit copy the name, and change this line of code:-

fd = open("/dev/tty.usbserial-A4001nU7", O_RDWR | O_NOCTTY | O_NDELAY);
 

Code:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>  
#include <termios.h>
#include <iostream>
using namespace std;
volatile int STOP=FALSE;
unsigned char buf[255];
int res;
int myCount=0;
int maxCount=10000;            // Number of cycles to time out serial port
void init_port(int *fd, unsigned int baud)
{
    struct termios options;
    tcgetattr(*fd,&options);
    switch(baud)
    {
            case 9600: cfsetispeed(&options,B9600);
                  cfsetospeed(&options,B9600);
                  break;
            case 19200: cfsetispeed(&options,B19200);
                  cfsetospeed(&options,B19200);
                  break;
            case 38400: cfsetispeed(&options,B38400);
                  cfsetospeed(&options,B38400);
                  break;
            default:cfsetispeed(&options,B9600);
                  cfsetospeed(&options,B9600);
                  break;
    }
    options.c_cflag |= (CLOCAL | CREAD);
    options.c_cflag &= ~PARENB;
    options.c_cflag &= ~CSTOPB;
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;
    tcsetattr(*fd,TCSANOW,&options);
}

int main()
{
      int fd;
      fd = open("/dev/tty.usbserial-A4001nU7", O_RDWR | O_NOCTTY | O_NDELAY); // List usbSerial devices using Terminal ls /dev/tty.*
      
    if(fd == -1) {                        // Check for port errors
            cout << fd;
            perror("Unable to open serial port\n");
            return (0);
      }
      
      cout << "Serial Port is open\n";
      
      // Write to Serial Port
      init_port(&fd,9600);                  //set serial port to 9600,8,n,1
      write(fd, "\n", 1);                  // chr(10) start comms
      write(fd, "2", 1);                  // 0 = off 1 = on 2 = ask LED state
      write(fd, "\r", 1);                  // chr(13) terminate comms
      cout << "Write to serial port OK\n";
      
      // Read from Serial Port
      while (STOP==FALSE) {
            myCount++;
            res = read(fd,buf,1);
            buf[res]=0;
            if (buf[0]==13) {            // Stop reading serial port if value = 13
                  STOP=TRUE;
            }
            if (buf[0]!=13) {            // If value = 13 then don't display it
                  if (buf[0]>0) {            // If received greater than 0, display it
                        std::cout << "Led Value is: " << (int)buf[0]<< std::endl;
                        myCount=0;      // Byte received so reset timer
                  }
            }
            if (myCount>maxCount) {         // Stop reading port if time out
                  STOP=TRUE;
                  myCount=0;
                  cout << "Serial Port Timed Out\n";
            }
      }
      close(fd);                              // Close the Serial Port
      return (0);                              // End program
}

Here is the Arduino Code

Code:
/*
 * Toggle LED via c++
 */

int incomingByte = -1;
int ledPin = 13;                // LED connected to digital pin 13
int  val = 0;
char code[10];
int bytesread = 0;
int led1 = 1; // LED initial state is ON

void setup()                    // run once, when the sketch starts
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
  Serial.begin(9600);
  digitalWrite(ledPin, HIGH);   // sets the LED on
}

void loop()        {             // run over and over again
  checkSerial();
}

void checkSerial() {
  if(Serial.available() > 0) {          // if data available
    if((val = Serial.read()) == 10) {   // check for header start
      bytesread = 0;
      while(bytesread<1) {              // read 1 digit code
        if( Serial.available() > 0) {
          val = Serial.read();
          if(val == 13) { // check for header end  
            break;                       // stop reading
          }
          code[bytesread] = val;         // add the digit          
          bytesread++;                   // ready to read next digit  
        }
      }
      if(bytesread == 1) {              // if 1 digit read is complete
        incomingByte = int(code[0]);
        doLEDS();
      }
      bytesread = 0;
      delay(50);                       // wait for a second
    }
  }


}

void doLEDS()
{

  if (incomingByte == 49) {
    digitalWrite(ledPin, HIGH);    // sets the LED off
    led1=1;
  }

  if (incomingByte == 48) {
    digitalWrite(ledPin, LOW);    // sets the LED off
    led1=0;
  }
  
  if (incomingByte == 50) { // php is asking for the LED state
      Serial.print(led1);
      Serial.print char(13);
  }
  
}
54  Forum 2005-2010 (read only) / Syntax & Programs / Re: Need a bit of help with I2C on: August 26, 2008, 08:17:52 am
Internal resistors activated or not, it would be cheap and especially quick to put a couple of resistors on A4 & A5. The issue ("my wii nunchuck data becomes corrupt") sounds like logic levels floating too erratically to hold a "clean and consistent data stream", and the solution if thats the problem, is a couple of pull ups.

I've read of people using 1.8, 4.7 and 10k on a wide range of different devices, so if you've got them handy you've got nothing to lose by trying.

55  Forum 2005-2010 (read only) / Syntax & Programs / Re: Need a bit of help with I2C on: August 24, 2008, 11:13:44 pm
Quote
No, I am not using any resistors... I was under the impression that the Wire library activates the internal resistors on the I2C bus and it wasnt needed. Is this incorrect?

My other devices have been functioning correctly without resistors on the sda and scl lines.

Thanks.

Personally I had no luck getting i2c to work without 2 x 1.8k (not 4.7) resistors.

Here's the pic of the setup from an old post of mine.

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1201476854/5#5
56  Forum 2005-2010 (read only) / Syntax & Programs / Re: Need a bit of help with I2C on: August 24, 2008, 09:06:25 pm
No, the sharing of the resonator was simply a result of not needing two for 2 168's on the same board.

Prior to the dualCore, I'd run successful i2c tests using two Lilypad's and had no problems sending data from one (master) to the other (slave).

Are you using 2 x 4.7k resistors?

57  Forum 2005-2010 (read only) / Syntax & Programs / Re: Serial problems on: April 08, 2008, 06:26:30 pm
Quote
There were two examples on that page.  The first one looked pretty much like my code, except the delay was at the end of the function instead of inbetween the Serial.available() and Serial.read().   I'm not sure why they want to pause the program for a whole second at the end for no apparent reason, other than they were having the same issue that I was.   The second example used softwareserial.h, which I do not want to use due to it's limitations.  Does anybody know why I have to include this delay in order for my code to work???  Is there some other way of doing this that doesn't use the delay?

Thanks,    Dan

I've tested the code and it works fine without a delay. I didn't notice a second sketch had been added, software serial will hang the program which isn't a problem if your Arduino's only purpose is to wait for an RFID reader to respond.

58  Forum 2005-2010 (read only) / Syntax & Programs / Re: Serial problems on: April 08, 2008, 12:09:57 pm
Try this code

http://www.arduino.cc/playground/Learning/PRFID

It works, but not with Software Serial (only hardware / rxtx)
59  Forum 2005-2010 (read only) / Syntax & Programs / Re: Modifying "blink without delay" on: April 06, 2008, 03:30:56 pm
It's a basic framework for pausing without hanging 'the loop', so he could cut in at 2000 and do something else. I've used it before and once the intervals were determined it was quite stable.

Millis is another good one and rather more predictable, it wouldn't take much of a hack to implement that rather than counter.

60  Forum 2005-2010 (read only) / Syntax & Programs / Re: Modifying "blink without delay" on: April 06, 2008, 02:28:14 pm
Declare int counter in the variables declarations section.

You'll need to experiment with the trigger values  (counter > trigger) to figure out the timing.

Code:
void loop() {
      counter=counter+1;
      if (counter > 3050) {
            turn-on-leds();
            counter=0; // reset counter
      }

      if (counter > 50) {
            turn-off-leds();
      }
}


void turn-on-leds {
      // turn on the LEDs that should be on
      for(int i=0;i<4;i++) {
          digitalWrite  (ledPin[i],  hitz[i]);
      }
}


void turn-off-leds() {
      // turn off all the LEDs
      for(int i=0;i<4;i++) {
      digitalWrite (ledPin[i],  LOW);
      }
}


Pages: 1 2 3 [4] 5 6 ... 48