Show Posts
Pages: [1]
1  Using Arduino / Programming Questions / Re: why isn't it incrementing? on: April 09, 2013, 11:09:37 pm
ookid - I changed the baud rate to 9600 and it doesn't change anything, the reason I have it so high was that I was playing around with it at one point and haven't changed it back. Also shouldn't it mean sending data at a higher rate taking less time?

PaulS - That's the thing, why doesn't it work without that there? it works when the delay(0) is there, or any other delay is there, but why? it's not really an issue now that I've found it but it seems to be a bit of an oddity.

PeterH - Really? timePassed is simply a variable that is (roughly) equivalent to currentMillis - previousMillis used in the blink without delay example, it times it correctly. And I do realise that the function name is no longer relevant, at one point it was, I just haven't been bothered to update the function name of it yet. However it doesn't just work "by coincidence", it took me a few tries to get it right, (mostly because of variable types, there's always infinite solutions to every problem with some better than others, I just picked one I came up with).

This is just a rough sketch, later on I might go through and fix names and such.

I tried moving the call to writeData() in the if, so that it is only called when the LED is changed, but again removing the delay(0) causes it to be erratic, but it does change. To show this here is some of the output. where the first number is the value of counter and the second value is the time passed since it was last checked (should roughly be the same as delayTime in this case is 250) in milliseconds.
Code:
0  250.05
2  250.01
1  250.02
0  250.08
0  250.09
2  250.07
1  250.04
0  250.06
2  250.03
2  250.12
0  250.02
and when the code is working as it should the output is as follows
Code:
0  250.13
2  250.06
1  250.07
0  250.13
2  250.09
1  250.11
0  250.16
2  250.11
1  250.11
0  250.12
2  250.13

The only difference between the two bit of code used to produce each output is when it is to get it working correctly is a delay(0) before counter is incremented and to get the erratic (first) the delay is commented out and writeData() is moved into the if.
working:
Code:
void loop() {
  delay(0);
  ++counter;
  if( counter > 2 ){
    counter = 0;
  }
  // delay without stopping everything
  // allows other code to run when this code isn't
  if( timePassed > delayTime ){
  //  Serial.print( "\t" );
  //  Serial.print( "IN?!?!" );
  //  Serial.print( "\t" );
   
    // If counter = 0 turn on red
    //            = 1         green
    //            = 2         blue
    for( int i = 0; i < NUM_RGB_LEDS; i++ ){
      if( counter == 0 ){
        setLED( i, 1, 0, 0);
      }
      else if( counter == 1 ){
        setLED( i, 0, 1, 0 );
      }
      else if( counter == 2 ){
        setLED( i, 0, 0, 1 );
      }
    }
    Serial.print( counter );
    Serial.print( "  " );
    Serial.println( timePassed );
    timePassed = 0;
//     writeData();
  }// end delay if
 
  writeData();
 
  printFreq();
   
}
erratic:
Code:
void loop() {

//  delay(0);
  ++counter;
  if( counter > 2 ){
    counter = 0;
  }
  // delay without stopping everything
  // allows other code to run when this code isn't
  if( timePassed > delayTime ){
  //  Serial.print( "\t" );
  //  Serial.print( "IN?!?!" );
  //  Serial.print( "\t" );
   
    // If counter = 0 turn on red
    //            = 1         green
    //            = 2         blue
    for( int i = 0; i < NUM_RGB_LEDS; i++ ){
      if( counter == 0 ){
        setLED( i, 1, 0, 0);
      }
      else if( counter == 1 ){
        setLED( i, 0, 1, 0 );
      }
      else if( counter == 2 ){
        setLED( i, 0, 0, 1 );
      }
    }
    Serial.print( counter );
    Serial.print( "  " );
    Serial.println( timePassed );
    timePassed = 0;
     writeData();
  }// end delay if
 
  printFreq(); //updates timePassed
   
}

I guess it's not really an issue now that I've found it and can work around it, but it's something that would be nice to know why it happens.
It should be reproducable without making the circuit as it doesn't rely on anything.
2  Using Arduino / Programming Questions / why isn't it incrementing? on: April 09, 2013, 12:06:35 am
So I'm driving 6 rgb LEDs using three 74HC595, and have that working. And in my main loop I have a counter that simply increments so that it changes colour every time a delay time is reached, but my problem is the line of code that increments the counter doesn't work unless there is either a delay(x); before or after the increment, where x is any number (including 0). or a Serial.print("") before and after it.

example:
Code:
delay(0);
++counter;
  if( counter > 2 ){
    counter = 0;
  }
or
Code:
Serial.print( " before " );
++counter;
Serial.print( " after " );
  if( counter > 2 ){
    counter = 0;
  }
work, but
Code:
++counter;
if( counter > 2 ){
    counter = 0;
  }
doesn't
What is happening, because otherwise the counter stays at 2, and the rest of the loop seems to execute fine...

any help would be appreciated, even if it's some stupid error on my part.

Here's all of the code (minus the bounce method).
The code in question is about halfway through.
There are a few variable lying around (specifically the once about brightness and the TIME_DELAY_TYPE) which are unused.
Code:
#define NUM_RGB_LEDS 6
#define NUM_LEDS NUM_RGB_LEDS*3

const int latchPin = 3; // RCLK  (12)
const int clockPin = 4; // SRCLK (11)
const int dataPin  = 2; // SER   (14)
int data[NUM_LEDS];

typedef struct {
  int red;
  int green;
  int blue;
  byte redBrightness;
  byte greenBrightness;
  byte blueBrightness;
} LED_TYPE;

typedef struct {
  unsigned long dt, t;
  float timePassed;
  unsigned long delayTime;
  unsigned long f;
} TIME_DELAY_TYPE;

LED_TYPE leds[NUM_RGB_LEDS];

TIME_DELAY_TYPE fadeDelay;

int counter = 0;
unsigned long t;
float dt;
float timePassed;
unsigned long delayTime;
float f;

void setup() {
 
  pinMode( clockPin, OUTPUT );
  pinMode( latchPin, OUTPUT );
  pinMode( dataPin, OUTPUT );
 
  // Turn on all blue LEDs
  /* Use data array to set leds
  for( int i = 17; i >= 0 ; i-- ){
    if( (i+1)%3  == 0 )
      data[i] = 1;
    else
      data[i] = 0;
  }
  */
 
  // Use struct array to control LEDs
  leds[0].red   = 1;
  leds[1].green = 1;
  leds[2].blue  = 1;
  leds[3].red   = 1;
  leds[4].green = 1;
  leds[5].blue  = 1; 
 
  for( int i = 0; i < NUM_RGB_LEDS; i++ ){
    leds[i].redBrightness = 255;
    leds[i].greenBrightness = 255;
    leds[i].blueBrightness = 255;
  }
 
  // delayTime in millis
  delayTime = 250;
 
  Serial.begin( 115200 );
  Serial.println( "Ready" );
 
//  delay( 2000 );
 
  bounce( 1, 0, 25 );
  bounce( 1, 1, 25 );
  bounce( 1, 2, 25 );

 
}

void loop() {
 // Serial.print( delayTime );
 // Serial.print( "\t" );
  //Serial.print( timePassed );
 // Serial.print( "\t" );
  // On board !OE is LOW, !SRCLR is HIGH
 
  // By pulsing the clock pin ( SRCLK / Shift Register CLK ),
  // The low to high transition shifts the data from the data pin.
  // (To take input from the pin, and move data across)
 
  // By pulsing the latch pin ( RCLK / Register CLK ),
  // The low to high transition shifts the data to the outputs
  // (Turns on the LEDs based on data entered using SRCLK and SER)
  // Use once every 18 clocks for 6 rgb LEDs
  delay(0);
  ++counter;
  if( counter > 2 ){
    counter = 0;
  }
  // delay without stopping everything
  // allows other code to run when this code isn't
  if( timePassed > delayTime ){
  //  Serial.print( "\t" );
  //  Serial.print( "IN?!?!" );
  //  Serial.print( "\t" );
   
    // If counter = 0 turn on red
    //            = 1         green
    //            = 2         blue
    for( int i = 0; i < NUM_RGB_LEDS; i++ ){
      if( counter == 0 ){
        setLED( i, 1, 0, 0);
      }
      else if( counter == 1 ){
        setLED( i, 0, 1, 0 );
      }
      else if( counter == 2 ){
        setLED( i, 0, 0, 1 );
      }
    }
    Serial.print( counter );
    Serial.print( "  " );
    Serial.println( timePassed );
    timePassed = 0;
  }// end delay if
 
  writeData();
 
  printFreq();
   
}

/*
 * The data array is sent in reverse order, as the data
 * is pushed along as data is entered
 *  R  G  B  R  G  B  R  G  B  R  G  B  R  G  B  R  G  B
 *  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
 */

// Pass an array NUM_LEDS bits long, then display based on the array
// The array is a global variable, along with leds, defined at the start
void writeData(){
  // Get values from LED_TYPE and enter into array to send
  for(int i = 0; i < NUM_RGB_LEDS; i++ ){
    data[ i*3 ]     = leds[i].red;
    data[ i*3 + 1 ] = leds[i].green;
    data[ i*3 + 2 ] = leds[i].blue;
  }
 
  // set latch pin low
  digitalWrite( latchPin, LOW );

  for( int i = NUM_LEDS - 1; i >= 0 ; i-- ){  // start loop
  // check value in data
  // if 1 then set data high
    if( data[i] == 1 )
      digitalWrite( dataPin, HIGH );
    else  // if 0 then set data low
      digitalWrite( dataPin, LOW );

  // clock
    digitalWrite( clockPin, HIGH );
    digitalWrite( clockPin, LOW );
    digitalWrite( dataPin, LOW );
  } // end loop
  // latch data (set latch pin high)
  digitalWrite( latchPin, HIGH );
}


void setLED( int ledNum, int r, int g, int b ){
  leds[ ledNum ].red   = r;
  leds[ ledNum ].green = g;
  leds[ ledNum ].blue  = b;
}

/*
  NOTE: It seems that the Serial.print() functions takes a while to execute
        causing the whole code to take longer, also leave Serial to last in code
        as (especially when reading the time, micros() ) a discrepency made by waiting
        until after Serial to check can cause timing errors.
        This was found as "t = micros();" in the following code was previously after
        the Serial calls, causing timing errors.
 */
void printFreq(){
  dt = micros() - t;
  t = micros();
  f = 1/(dt/1000000);
//  Serial.print( "freq: " );
//  Serial.print( f );
//  Serial.print( "Hz\tdt: " );
//  Serial.print( dt );
//  Serial.println( "us" );
 
  timePassed += (dt/1000);
}
3  Using Arduino / Programming Questions / Re: delay() causing array data to increment !?! on: March 19, 2013, 07:18:54 am
 smiley-eek oops, well that's a bit of an embarrassing mistake.
thanks, works fine now. wish the IDE would pick up on those things sometimes.
4  Using Arduino / Programming Questions / delay() causing array data to increment !?! on: March 19, 2013, 07:11:28 am
Hi, I've started playing around with a few 74HC595's to light a few LEDs but when I use the delay function it begins incrementing the 17th element by the amount of ms I'm delaying by ( give or take a little bit ).

Code:
int latchPin = 3; // RCLK  (12)
int clockPin = 4; // SRCLK (11)
int dataPin  = 2; // SER   (14)
int data[16];

void setup() {
 
  pinMode( clockPin, OUTPUT );
  pinMode( latchPin, OUTPUT );
  pinMode( dataPin, OUTPUT );
 
  // Turn on all blue LEDs
  for( int i = 0; i < 18 ; i++ ){
    if( (i%3) - 2 == 0 )
      data[i] = 1;
    else
      data[i] = 0;
  }
  Serial.begin( 115200 );
}

void loop() {
  // On board !OE is LOW, !SRCLR is HIGH
 
  // By pulsing the clock pin ( SRCLK / Shift Register CLK ),
  // The low to high transition shifts the data from the data pin.
  // (To take input from the pin, and move data across)
 
  // By pulsing the latch pin ( RCLK / Register CLK ),
  // The low to high transition shifts the data to the outputs
  // (Turns on the LEDs based on data entered using SRCLK and SER)
  // Use once every 18 clocks for 6 rgb LEDs
 
  writeData();
  for( int i = 17; i >= 0; i-- ){
    Serial.print( data[i] );
    Serial.print( " " );
  }
  Serial.println();
 
  //delay(1000);
}

// Pass an array 16 bits long, then display based on the array
void writeData(){
  // set latch pin low
  digitalWrite( latchPin, LOW );

  for( int i = 17; i >= 0 ; i-- ){  // start loop
  // check value in data
  // if 1 then set data high
    if( data[i] == 1 )
      digitalWrite( dataPin, HIGH );
    else  // if 0 then set data low
      digitalWrite( dataPin, LOW );

  // clock
    digitalWrite( clockPin, HIGH );
    digitalWrite( clockPin, LOW );
    digitalWrite( dataPin, LOW );
  } // end loop
  // latch data (set latch pin high)
  digitalWrite( latchPin, HIGH );
}

note: Because i'm not using shiftOut(), when the array is printed and sent to the shift registers it is sent backwards (last element sent first and the first element last)

Does anybody know why this is happening????
5  Using Arduino / Interfacing w/ Software on the Computer / Re: Reading serial data coming from arduino in c++? on: January 20, 2013, 12:24:39 am
You are allocating memory, but not freeing it.

Why not use a static array?
Code:
char buf[256];
I am freeing it, just later on in the code (same as when I call the destructor for the Serial class), I haven't pasted my entire code as I felt that that shows the (previously) problematic part.

Is using the static array going to be any different from using malloc? (Apart from having to allocate and free the memory)

For the sake of it, her's the entire main I'm using with the class, guess it would've been easier to just do this from the start (lesson learned).
This code just sends a '1', which turns the LED on pin 13 on and then a '0' which turns the LED off with the arduino replying with LED ON or LED OFF, I think I'm going to move the read and write functions to another class to make it easier to extend and to read and write more than one charater.
Code:
#include <cstdlib>
#include <iostream>
#include "SerialClass.h"

using namespace std;

void readData();

Serial ard = Serial("COM4");

int main()
{
if( ard.IsConnected() == true )
{
cout << "Done" << endl;
} else
{
cout << "Connection failed, exitting";
return EXIT_FAILURE;
}


//test arduino connection
if( ard.WriteData( "1", 1 ) == true )
{
cout << "Data sent successfully\n";
} else
{
cout << "Data not sent";
}

        readData();

ard.WriteData( "0", 1 );
readData();

        ard.~Serial();

        // Keep the console open to check output.
        // Saves from using a breakpoint
Sleep(20000);
return EXIT_SUCCESS;
}

void readData()
{
        // Check for data coming from arduino
        // If no data is found check 4 more times
char *buf;
int length = 256;
buf = ( char* ) malloc( length );

int num, i = 0;
while( i < 5 ){
num = ard.ReadData( buf, length );
if( num == -1 )
{
cout << "\nNothing to read";
} else
{
cout << "Read: ";
for( int i = 0; i < num; i++ )
{
cout << *(buf + i);
}
cout << "Num: " << num << endl;
break;
}
i++;
Sleep(25);
}
free( buf );
}
6  Using Arduino / Interfacing w/ Software on the Computer / Re: Reading serial data coming from arduino in c++? on: January 19, 2013, 12:23:55 pm
Quote
where ard is the Serial class.
And we're supposed to guess how (and even if) you've opened the serial port, and to guess what you are currently seeing as output, and to then tell you what is wrong. I'll pass until I know more.
Sorry if you missed it, the Serial class I am referring to is in the link ( I guess i could've explained that better ). (I believe) that the Serial port is being opened in the constructor of the SerialClass object (viewable in the .cpp file through the link, I'll copy it here if you insist). In mine the only thing I changed was the baud rate (to 115200) where it sets the stopbits and parity, it was doing the same thing before, and after the change.
But due to my lack of knowledge of how c++ opens serial ports, I can't tell how to get data from the arduino. (Though as I said before, sending data to the arduino seems fine).

Quote
ard.ReadData( buf, 256 );
You've told it that it can read up to 256 characters into buf, BUT
Quote
char *buf = "0";
buf points to a string containing only one character and the null on the end.

Pete


Ah, seems I need to get used to C++ a bit more (coming from java). I replaced it with:
Code:
        Serial ard = Serial("COM4");

char *buf;
int length = 256;
buf = ( char* ) malloc( length );

int num, i = 0;
while( i < 5 ){
num = ard.ReadData( buf, 3 );
if( num == -1 )
{
cout << "\nNothing to read";
} else
{
cout << "Read: ";
for( int i = 0; i < num; i++ )
{
cout << *(buf + i);
}
cout << "Num chars: " << num;
break;
}
i++;
Sleep(500);
}
Which seems to work now, Thanks.
7  Using Arduino / Interfacing w/ Software on the Computer / Reading serial data coming from arduino in c++? on: January 19, 2013, 06:34:08 am
I found an example of how to interface between c++ and the arduino (http://playground.arduino.cc//Interfacing/CPPWindows). But I am having trouble reading data that is coming from the arduino.

Could someone please explain to me how to read in datausing this function?

Code:
        //Read data in a buffer, if nbChar is greater than the
        //maximum number of bytes available, it will return only the
        //bytes available. The function return -1 when nothing could
        //be read, the number of bytes actually read.
        int ReadData(char *buffer, unsigned int nbChar);

What role does the nbChar play in reading the data?

At the moment my code for reading from the arduino is:
Code:
char *buf = "0";
int num, i = 0;
while( i < 5 ){
num = ard.ReadData( buf, 256 );
        if( num == -1 )
        {
    cout << "Nothing to read\n";
        } else
        {
    cout << "Read: " << *buf << endl;
    cout << "Num: " << num;
    break;
        }
    i++;
    Sleep(500);
}
where ard is the Serial class.
and the arduino just spitting out "1" using Serial.println() to see if I can actually read it.
Pages: [1]