Show Posts
Pages: [1] 2 3
1  Using Arduino / Programming Questions / Re: Parsing csv file from sd card to an array on: January 24, 2013, 12:20:06 am
There are a few way to do this, and it really depends on how 'funny' your data can be.

One way is to use something like strtok and strtok_rto split the C string when it comes across a certain character in a set, although this can have trouble with strings that contain the same delimiters.

Another way is to write your own parser - which isn't too hard. The basic idea is to read each character as it comes (whether from streaming, or loading the file into a C string array and manually walking it) and split out the values as you go along. This way also allows you to have strings that share the delimiter, or adding special tokens that you can parse later.

Of course that's just the idea of writing one, and getting it to function on a micro controller will take a bit more restraint in terms of memory etc. There are a few examples, but most look to use the STL libraries, which I'm not too sure how they will run on a micro controller.

Actually - with a bit of work you might be able to use them smiley
2  Using Arduino / Programming Questions / Re: Odd sprintf behaviour on: January 14, 2013, 01:41:12 am
You haven't allocated space for a null terminator in the currDate array, and as luck would have it the next block of memory is the currTime array.

You want to allocate 1 more than the longest string to allow for a null terminator (\0) otherwise when you print it will keep reading memory as a string until it hits a null.

You really want to allocate your arrays as
char currDate[25];
char currTime[23];

3  Using Arduino / Programming Questions / Re: While loops not stopping when condition is false? on: November 26, 2012, 11:41:40 pm
You just need to call break

if(val > 0)
4  Using Arduino / Programming Questions / Re: Programming Help on: November 06, 2012, 12:02:31 am
Hi new2ard,

First - you will want to format your code and place them into code tags (the # button in the editor on the forums).

As for the code - I can see a few problems already, but will need some time to make the code a bit more readable smiley


Looking at the code - you have two for loops running in the main loop. This would probably explain the behaviour you are getting. Just to be sure, do you realise that loop is run constantly?

The way the code is set up now - you are stalling the main loop from further processing with each call to onesecond. what you might want to do is look at the Blink without delay sketch and work that into what you are doing. For example - removing the for loops and calculating the values on each pass of the main loop.

Added some braces to your code so it's a bit easier to read
#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68 //set rtc
#include <LiquidCrystal.h>

//******************** Define LCD setting ********************
// Connections:
// rs (LCD pin 4) to Arduino pin 22
// rw (LCD pin 5) to Arduino pin Gnd
// enable (LCD pin 6) to Arduino pin 23
// LCD pins d4, d5, d6, d7 to Arduino pins 24, 25 26, 27
LiquidCrystal lcd(22,23,24,25,26,27);

//*************** Define line2 LED setting ***************

int line2 = 2; // line2 LED connected to PWM digital pin 2

int line2rampup = 5 ; // time for line2 LEDs to dim on and off in minutes
int line2rampdown = 5 ; // time for line2 LEDs to dim on and off in minutes

int line2min = 0 ; // minimmum dimming value of line2 LEDs, range of 0-255
int line2max = 255 ; // maximum dimming value of line2 LEDs, range of 0-255

int photoperiod2 = 2 ; // amount of time array is on at full power in minutes
int ontime2 = 5 ; // time of day (hour, 24h clock) to begin photoperiod2 fade in

//*************** Define line3 LED setting ***************

int line3 = 3; // line3 LED connected to PWM digital pin 3

int line3rampup = 7 ; // time for line3 LEDs to dim on and off in minutes
int line3rampdown = 7 ; // time for line3 LEDs to dim on and off in minutes

int line3min = 0 ; // minimmum dimming value of line3 LEDs, range of 0-255
int line3max = 255 ; // maximum dimming value of line3 LEDs, range of 0-255

int photoperiod3 = 1 ; // amount of time array is on at full power in minutes
int ontime3 = 5 ; // time of day (hour, 24h clock) to begin photoperiod3 fade in

//***************************** RTC DS1307 ********************************

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
    return ( (val/10*16) + (val%10) );

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
    return ( (val/16*10) + (val%16) );

// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers
void setDateDs1307(byte second, // 0-59
    byte minute, // 0-59
    byte hour, // 1-23
    byte dayOfWeek, // 1-7
    byte dayOfMonth, // 1-28/29/30/31
    byte month, // 1-12
    byte year) // 0-99
    Wire.send(decToBcd(second)); // 0 to bit 7 starts the clock
    Wire.send(decToBcd(hour)); // If you want 12 hour am/pm you need to set
    // bit 6 (also need to change readDateDs1307)

// Gets the date and time from the ds1307
void getDateDs1307(byte *second,
    byte *minute,
    byte *hour,
    byte *dayOfWeek,
    byte *dayOfMonth,
    byte *month,
    byte *year)
    // Reset the register pointer

    Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

    // A few of these need masks because certain bits are control bits
    *second = bcdToDec(Wire.receive() & 0x7f);
    *minute = bcdToDec(Wire.receive());
    *hour = bcdToDec(Wire.receive() & 0x3f); // Need to change this if 12 hour am/pm
    *dayOfWeek = bcdToDec(Wire.receive());
    *dayOfMonth = bcdToDec(Wire.receive());
    *month = bcdToDec(Wire.receive());
    *year = bcdToDec(Wire.receive());

/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  D E F I N E  :  O N E S E C O N D |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/

void onesecond() //function that runs once per second while program is running
    byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
    getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
    lcd.setCursor(0, 0);
            lcd.print(hour, DEC);
            lcd.print(hour-12, DEC);


    if (minute < 10)

    lcd.print(minute, DEC);
    //if (second < 10) {
    //  lcd.print("0");
    //lcd.print(second, DEC);

    lcd.print(" ");


/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  S E T U P  |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/

void setup()
    //***************************** RTC DS1307 ********************************

    byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;

    // Change these values to what you want to set your clock to.
    // You probably only want to set your clock once and then remove
    // the setDateDs1307 call.
    second = 50;
    minute = 59;
    hour = 4;
    dayOfWeek = 6;
    dayOfMonth = 3;
    month = 11;
    year = 12;
    setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
    //***************************** LED DISPLAY LINES INFO ********************************
    lcd.begin(20, 4); // set up the LCD's number of rows and columns:
    analogWrite(line2, line2min);
    lcd.setCursor(0, 1);
    lcd.setCursor(2, 1);
    lcd.print(" ");
    analogWrite(line3, line3min);
    lcd.setCursor(5, 1);
    lcd.setCursor(7, 1);


/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  L O O P |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/

void loop() {

    //***************************** LOOP RTC DS1307 ********************************
    byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
    getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);

    //||||||||||||||||||||||||| LINES LED LOOP |||||||||||||||||||||||||
    //********** Fade line2 LED from min. to max. **********
    int daybyminute2 = ((hour * 60) + minute);
    if (daybyminute2 >= (ontime2*60))
        if (daybyminute2 <= ((ontime2*60) + line2rampup) - minute) //if time is in range of fade in, start fading in
            for(int line2fadeval = line2min ; line2fadeval <= line2max; line2fadeval ++) // sets the minimum and maximum values
                analogWrite(line2, line2fadeval);
                lcd.setCursor(2, 1);
                lcd.print(" ");

                int countdown = ((line2rampup*60)/(line2max-line2min));
                while (countdown>0)

    //********** Fade line3 LED from min. to max. **********

    int daybyminute3 = ((hour * 60) + minute);
    if (daybyminute3 >= (ontime3*60))
        if (daybyminute3 <= ((ontime3*60) + line3rampup) - minute)
            for(int line3fadeval = line3min ; line3fadeval <= line3max; line3fadeval ++)
                analogWrite(line3, line3fadeval);
                lcd.setCursor(7, 1);
                lcd.print(" ");
                int countdown = ((line3rampup*60)/(line3max-line3min));
                while (countdown>0)

5  Using Arduino / General Electronics / Re: Two different solenoids and protection on: November 04, 2012, 09:34:59 pm
Thanks for all the help guys,


In regards to the resistor before the capacitor, I do require the solenoids to be able to fire rather rapidly (My old thread is here with some break down numbers), but I found I'd only really need to be able to fire it every 20ms at most - unless I can prove I can fire the marker faster than that :p

I am a bit confused with the 6V solenoid though - do you mean to drive it from the 9V line with a 33Ohm resistor instead of using the 5V line? I'm confused how this would step down the voltage for the solenoid to use

6  Using Arduino / General Electronics / Re: Two different solenoids and protection on: October 31, 2012, 06:51:51 pm
Thanks for the quick reply Mark

The Mosfet I am currently using is an IRF540N (datasheet).

What is the resisitor for? Is it there to help limit anything flowing back into the pin?

It seems to drive the solenoid OK as shown (I've tested each section, just not together yet), but I'm not 100% sure that I've got it set up right.
7  Using Arduino / General Electronics / Two different solenoids and protection on: October 31, 2012, 06:32:16 pm
Hi all,

Just wanting to double check something, and hopefully learn something new as well.

I'm working on a circuit to control my paintball marker, and I'm almost at the point of testing. What I wanted to know is do I need to have any extra protection from the 9V solenoid on my circuit, or is that the whole point of the diode?

The circuit itself consists of 2 solenoids - one for the sear (9V and around 4 amps) and the other is for redirecting the flow of air to the cocking pneumatics (rated 6V but will drive at 5V and 0.5W - I have found the data sheet for this part) and a phototransistor eye (datasheet)

The sear solenoid would be on for a max. 4 ms, while the cocking solenoid could be on for as long as the trigger is held down (this is to replicate the current functionality). Is there anything I should be worried about? Should I drive all the solenoids at the same voltage? Am I just over thinking all this?

Any helpful pages or topics that I can look into and expand my knowledge would be awesome, and thanks in advance for the help
8  Using Arduino / Programming Questions / Re: Using a 1x4 Keypad to switch between loops on: June 21, 2012, 10:57:44 pm
Quick reply off the top of my head on lunch:

The loop() function will always be called, and without writing your own code from scratch cannot be stopped - short of stopping the whole program.

What you want is a basic state machine - there's a few examples around on how to implement one, but the basic idea would be something like this (along with your setup stuff):

enum Tasks
} currentTask;

void doTask1()
    Serial.println("Doing Task 1");

void doTask2()
    Serial.println("Doing Task 1");

void doTask3()
    Serial.println("Doing Task 1");
//take care of some special events
void keypadEvent(KeypadEvent key)

Serial.print("Pressed: ");

switch (keypad.getState())
switch (key)
case '1': currentTask = Task1; break;
case '2': currentTask = Task2; break;
case '3': currentTask = Task3; break;

void loop()
  if(currentTask == Task1)
  else if(currentTask == Task2)
  else if(currentTask == Task3)

That's the basic idea - the code might not work as a drop in but I hope it helps.

Also you might want to place your code into code tags (the # button) smiley-wink
9  Using Arduino / Programming Questions / Re: Programming an ATMEGA328P for breadboarding with Mac OS X on: April 30, 2012, 05:16:57 pm
It sounds like you've got the board/ship definition in your sketch folder instead of the install directory. I'm not sure if this is intended or not, but I came across it earlier when messing around with one of my projects.,99075.msg742844.html#msg742844

(This was using 1.0)

For OSX you have to open the package and edit the boards.txt in there - or you can copy all the core files into your sketch folder
10  Using Arduino / Project Guidance / Re: Adding a button on: April 29, 2012, 05:27:39 pm
Best to use the code tags when posting code (the # button). Makes it easier to read smiley

As for your question, if I'm assuming correctly you are wanting to stop the loop function from running?

The loop function will always run, and to stop it is to stop the micro controller from executing any more code (in essence, it's probably a bit more than that). But if you only want to run the code you currently have in loop() when, say a button is pressed, you could wrap the code in an if statement.

Going by that assumption you would be looking at doing something like this. All you would need to do is hook a switch between pin 2 and GND.

And a side note, as I've just gone through trying to use a button on an external interrupt, it's not really a good idea as the switch itself will change state numerous times quickly. I'm sure it can be done, but IMO its easier to just check in loop

#include "PCD8544.h"

// pin 7 - Serial clock out (SCLK)
// pin 6 - Serial data out (DIN)
// pin 5 - Data/Command select (D/C)
// pin 4 - LCD chip select (CS)
// pin 3 - LCD reset (RST)
PCD8544 nokia = PCD8544(7, 6, 5, 4, 3);

int sensorPin = 0;

int buttonPin = 2;

void setup() {
  // Serial.begin(9600);
  pinMode (11 , OUTPUT);
  pinMode (10 , OUTPUT);


  // back to normal


  // Set the button pin to input (not generally needed but make it's intention clear)
  pinMode (buttonPin, INPUT);
  // Enable internal pull up resistor
  digitalWrite(buttonPin, HIGH);


void loop() {

  if(digitalRead(buttonPin) == LOW)

    nokia.setCursor(0, 0);
    nokia.print("Kevin's CNC ");
    nokia.setCursor(0, 15);
    nokia.print(" Controoler");

    int reading = analogRead(sensorPin);
    float voltage = reading * 5.0;
    voltage /= 1024.0;
    float temperatureC = (voltage - 0.5) * 100 ;
    float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
    // Serial.print(temperatureF); Serial.println(" degrees F");
    nokia.setCursor(10, 30);
      if (temperatureF >= 90 ){
        digitalWrite (11,HIGH);
        digitalWrite (10,HIGH); // led yellow  to show warm
        digitalWrite (10,LOW); // led yellow  to show warm
        nokia.setCursor(10, 40);
        nokia.print("TEMP RISING");

      else if (temperatureF <= 85) {
        digitalWrite (10,LOW );
        digitalWrite (11,LOW );

        digitalWrite (11,LOW );
        nokia.setCursor(10, 40);
        nokia.print("           ");

Hope this helps smiley
11  Using Arduino / Programming Questions / Re: What is wrong with my program?? on: April 04, 2012, 10:29:37 pm
Ok - here's some code I've quickly knocked together to try an illustrate a way to track time without using the delay function. It's in no way the best approach, and I haven't tested it other that to check it compiles - but the idea is about right. I like the approach of 'learning by examples' so I've tried to comment as much as I can - but it would be good for you to read over the Reference and Learning pages as well for additional help.

One example is that an integer(int) can only hold a maximum value of 65,535 when it's unsigned and +/-32,768 when it's signed, so trying to hold 2 minutes in milliseconds would exceed this.

As to why the LED isn't turning off - without seeing how you've connected the hardware together there could be a few things, one could be the switch itself is always returning high. How have you got everything hooked together?

Hope this helps

(PS - I'm at work so this is all off the top of my head smiley-wink)

int lightPin = 11;//my led
int lightBright = A1;//my pot to controll the brightness
int lightTime = A2;//my pot to controll the time it is on
int lightReset = 2;//my tact switch to turn on the light
int timeA = 0;//the time befor it is mapped for time length
int bright = 0;//the brightness

// This needs to be of type long to be able to hold a value of 2 minutes in seconds
// the unsigned bit tells us we want the value to be for 0 -> LONG_MAX instead of
// -LONG_MAX/2 -> LONG_MAX/2. For more on the read about the data types on the reference page
unsigned long timeB = 0;//the time after it is mapped for time length

// We will use this one so we can keep track of how long the LED has been on for
unsigned long timeAtReset = 0;

void setup()
pinMode(lightPin, OUTPUT);
pinMode(lightBright, INPUT);
pinMode(lightTime, INPUT);
pinMode(lightReset, INPUT);

void loop()
bright = analogRead(lightBright);
timeA = analogRead(lightTime);

// Here we grab the current time
unsigned long currentTime = millis();

// so here we have mapped our pot value to a time between 10s and 2m(120s)
// and the idea is we want to be able to change it at any time, So we need to keep updating it
timeB = map(timeA, 0, 1023, 10000, 120000L);

// If the button is pressed (this will include being held down btw)
if (digitalRead(lightReset) == HIGH)
// Turn the LED on
analogWrite(lightPin, bright);

// Set the time we started
timeAtReset = millis();
// Otherwise if the button is not pressed/held AND the current time is greater
// than the time the LED was switched on + the mapped time
else if(currentTime > timeAtReset + timeB)
// Turn LED off
analogWrite(lightPin, 0);
12  Using Arduino / Programming Questions / Re: What is wrong with my program?? on: April 04, 2012, 09:16:33 pm
Have a look at the blink without delay example.

Conveniently linked here smiley-grin
13  Using Arduino / Programming Questions / Re: Programming on breadboard help on: March 30, 2012, 01:23:35 am
I've been messing around with that same tutorial and hit the same wall. if you were going the same as me I'm assumung you added the folder to your user directory and not the install directory?

Put this into the boards.txt file in the install dir (Program files in my case)
############################################################## on a breadboard (8 MHz internal clock)



With this I managed to get it building the Blink example. It seems to look for the additional files it needs in the same folder as the boards.txt file - you could keep it out in another dir, but you would need to copy the cores and possibly bootloaders directories.

Hope it works for you smiley
14  Using Arduino / General Electronics / Re: Strange PCB Problem on: March 29, 2012, 04:57:06 pm
Thanks for the quick reply

I have a 10uF on either side of the voltage reg on the final board - just didn't have any pictures of it at the moment as I'm at work. Been wondering if I need more as I saw a breadboard tutorial with one between VCC and GND.

EDIT: On the Uno as Serial front, seems I needed to have the pins hooked up backwards as well (link)
15  Using Arduino / General Electronics / Strange PCB Problem on: March 29, 2012, 04:37:20 pm
Hi all,

I'm getting an odd problem that I'm having a hard time tracking down. I've been building a bomb prop for a while, and since getting it working on my Uno have started making the move to it's own board. The problem is it seems to lock up and act strangely, but only when the chip is on the board and running my sketch - if I put the chip in my Uno and connect the pins up it almost works perfectly (which I'll get to shortly).

The set up for the board contains jumpers for a matrix keypad, a LCD screen (with the backlight on 13), 2 switches, a LED and a Piezo buzzer. In the code I'm using a basic FSM to control the states

At first I thought it was a problem with the board - and wouldn't be shocked if it is - as I haven't printed it, but etched it by hand with a Dremel (I have young kinds that get into everything, even when it's presumably away safe :S). I've also checked the connections and there doesn't seem to be any shorts anywhere.

Going with the assumption that it's my quick hack board that's the problem I started debugging with the following:

* Loaded the blink sketch and got the LCD backlight blinking properly on the board (board powered by battery)
* Loaded the HelloKeypad sketch and got it working with the chip in the Uno linked to the board (board powered by Uno)
* Loaded my sketch and got it almost working - one of the switches always returns HIGH regardless of switch position
* If I take the chip out of the Uno and place it into my board I get completely different behaviour
    - States aren't updating as they should
    - Input doesn't seem to work at all
    - Seems as if the program locks up internally

Now I've been trying to link the chip up externally to the Uno so I can do some serial debugging on the board, but to no avail. Is this possible?

The board is powered by 12v so I can use a loud buzzer - would I be right in thinking that I just need to link the Uno with my 5v output and GND? Will that conflict with the USB power? Or will it know not to bother so I can do some good ol' fashion console debugging?

Thanks for the help

PS - Please note the board pics are a bit out of date, as the resistors for the keypad are now hooked up to the 5V line as shown in the schematic. Will upload a better picture later as I don't currently have one
Pages: [1] 2 3