Show Posts
Pages: 1 2 [3] 4 5 ... 7
31  Using Arduino / Microcontrollers / Arduino UNO 3 Digital I/O count on: February 04, 2012, 03:02:01 am
I've looked at the standard "pins_arduino.h" and there are definitions:

NUM_DIGITAL_PINS with a value of 20 and NUM_ANALOG_INPUTS with a value of 6. 

The page for the Arduino Uno says they're are 14 digital I/O and 6 analogue inputs.

I've run a bit of code to query the digital I/O information, and the results I got back are:
Pin: 1, bit: 2, port: 4
Pin: 2, bit: 4, port: 4
Pin: 3, bit: 8, port: 4
Pin: 4, bit: 16, port: 4
Pin: 5, bit: 32, port: 4
Pin: 6, bit: 64, port: 4
Pin: 7, bit: 128, port: 4
Pin: 8, bit: 1, port: 2
Pin: 9, bit: 2, port: 2
Pin: 10, bit: 4, port: 2
Pin: 11, bit: 8, port: 2
Pin: 12, bit: 16, port: 2
Pin: 13, bit: 32, port: 2
Pin: 14, bit: 1, port: 3
Pin: 15, bit: 2, port: 3
Pin: 16, bit: 4, port: 3
Pin: 17, bit: 8, port: 3
Pin: 18, bit: 16, port: 3
Pin: 19, bit: 32, port: 3

I can see from the header that some of the digital pins are also used for analog output.  Is there a function I can call that will tell me if a pin is available for use?

I would like to create a class which enables me to read and write packed bytes to a port, so I can read and write to mutiple digital I/O pins with a single call.  It certainly looks like this is possible from the source.
32  Development / Other Software Development / Re: Event Library: clsEvent on: February 03, 2012, 01:48:05 pm
Sample sketch...

This just waits for a push button to be pressed then turns on an LED.
Code:
#include <clsEvent.h>

#define PUSH_BUTTON 8
#define MY_LED          13

bool pushButton(clsEvent* pEvent) {
  unsigned long uldTime = 0;    
  while( (digitalRead(PUSH_BUTTON)) == LOW ) {
    if ( uldTime == 0 ) {
// Get reference time
      uldTime = millis();    
    } else if ( (millis() - uldTime) > 200 ) {
// Time expired whilst still high
      return true;
    }
  }
  return false;
}

void swithcOnLED(clsEvent* pEvent) {
  digitalWrite(MY_LED, HIGH);
}

void setup() {  
  pinMode(MY_LED,             OUTPUT);
  pinMode(PUSH_BUTTON,    INPUT);
  digitalWrite(PUSH_BUTTON, HIGH);

  new clsEvent("pbTest", pushButton, swithcOnLED, NULL);
}

void loop() {
// Service any events
  clsEvent::serviceEvents();
// Everything else...
}
33  Using Arduino / Networking, Protocols, and Devices / Re: SCADABR with modbus RTU on: February 02, 2012, 03:38:35 pm
What form of modbus are you using rs232 or tcp?  Also what variant, ASCII or rtu?

34  Development / Other Software Development / Re: Event Library: clsEvent on: February 02, 2012, 03:34:16 pm
Will do, have to wait until tomorrow, on iPad atm
35  Using Arduino / Programming Questions / Re: if() and for() structures on: February 02, 2012, 03:27:47 pm
Loop will run indefinitely.  If you want it to stop at 50, try:

Code:
void loop() {
  If ( x++ <= 50 ) {
    Serial.println( x );
    Serial.println( "Hello" );
    delay( 100 );
  }
}
36  Development / Other Software Development / Event Library: clsEvent on: February 02, 2012, 01:57:05 pm
Please find below link and source to an event library:
http://avasig.com/clsEvent.zip

Prototype, clsEvent.h
Code:
/**
 * File:
 *  clsEvent.h
 *  
 * Notes:
 *  This file contains the prototype for the class clsEvent.
 *  
 * Methods:
 *  clsEvent      Class constructor
 *  debug         Enable or disable debugging msgs on serial O/P
 *  getName       Returns the name assigned to this event
 *  serviceEvents Should be called in main loop to service events  
 *
 * Members:
 *  m_pTest       Test callback should return true if event should occur
 *  m_pCallback   Call-back routine to call when event test expression is true
 *  m_pData       Data to associate with this instance
 *  m_pstrName    The name assigned to this event
 *  m_pNext       Next event in Linked-list      
 *            
 * History:
 *  29/01/2012  Created by Simon Platten
 */            
#ifndef EVENT_MANAGER_H
  #define EVENT_MANAGER_H
  
  #include <Arduino.h>
  
  class clsEvent {
  private:
    char*     m_pstrName;
    bool      (*m_pTest)(clsEvent* pEvent);
    void      (*m_pCallback)(clsEvent* pEvent);
    void*     m_pData;
    clsEvent* m_pNext;
      
  public:
    static void debug(bool blnDebug);    
    static void serviceEvents();
  
  public:
    clsEvent(char* pstrName,
             bool (*pTest)(clsEvent* pEvent),              
             void (*pCallback)(clsEvent* pEvent),
             void *pData = NULL);
    char* getName();
  };
#endif

Implementation, clsEvent.cpp
Code:
/**                                                                                 /**
 * File:
 *  clsEvent.cpp
 *  
 * Notes:
 *  This file contains the implementation of the class clsEvent.
 *
 * History:
 *  29/01/2012  Created by Simon Platten
 */  
#include "clsEvent.h"
// Pointer to all events linked list
static clsEvent* pAllEvents = NULL;
static bool blnDebugFlag = false;
/**
 * Event constructor
 *  
 * Paramters:
 *  pstrName,   A description of this event
 *  pTest,      callback routine to test if event should occur
 *  pCallback,  the routine to call when the timer expires
 *  pData,      pointer to data block accessible for this timer  
 */        
clsEvent::clsEvent(char* pstrName,
                   bool (*pTest)(clsEvent* pEvent),              
                   void (*pCallback)(clsEvent* pEvent),
                   void *pData) {
// Configure this event
  m_pstrName = pstrName;
  m_pTest = pTest;
  m_pCallback = pCallback;
  m_pData = pData;
  
  if ( pAllEvents != NULL ) {
// Insert this event into the list of all events
    m_pNext = (clsEvent*)pAllEvents;              
  } else {
// This is the first node in the list, ensure that there is no Next node    
    m_pNext = NULL;
  }
// Insert this timer at the beginning of the linked list    
  pAllEvents = this;                    
}
/**
 * Enable or disable debugging msgs on serial O/P
 */
void clsEvent::debug(bool blnDebug) {
  blnDebugFlag = blnDebug;
}
/**
 * Returns the name assigned to this event
 */
char* clsEvent::getName() {
  return m_pstrName;
}
/**
 * Should be called in main loop to service events
 */  
void clsEvent::serviceEvents() {
  for( clsEvent* pEvent=pAllEvents; pEvent!=NULL;
       pEvent=pEvent->m_pNext ) {
    if ( (*pEvent->m_pTest)(pEvent) == true ) {
      if ( blnDebugFlag == true ) {
        Serial.print(pEvent->getName());
        Serial.println(" is true!");
      }    
      (*pEvent->m_pCallback)(pEvent);
    }      
  }
}

The idea behind this class is simple, create your own event using:
Code:
new clsEvent(pstrName, pTest, pCallback, pData);

pstrName is a text string that describes the event, used only when debugging enabled, to show output on serial port
pTest, is a pointer to a test function, prototype: bool (*pTest)(clsEvent* pEvent)
pCallback, is a pointer to a function to call when the result of pTest is true, prototype:  void (*pCallback)(clsEvent* pEvent)
pData, is an option pointer to your own data structure that you can reference via the pEvent pointer passed to your callback.

I am using this to register a test for a push button, the test routine debounces the button and returns true or false, my call back routine then performs an action.

Forgot to mention, you will also need to call the static routine:
Code:
clsEvent::serviceEvents();
From within you loop.
37  Using Arduino / Programming Questions / Re: Debounce routine works once only on: February 02, 2012, 01:43:04 pm
Modified debounceDI routine:
Code:
bool clsApp::debounceDI(uint8_t ud8DI, int intState, unsigned long uldMS) {
  unsigned long uldTime = 0;
  while( (digitalRead(ud8DI)) == intState ) {
    if ( uldTime == 0 ) {
// Get reference time
      uldTime = millis();    
    } else if ( (millis() - uldTime) > uldMS ) {
// Time expired whilst still high
      return true;
    }
  }
  return false;
}

This one works fine, I think that what was happening before is that arduino was probably running out of memory probably due to fragmentation due to the creation of the timer object in the function.

Thank you to those that gave assistance.
38  Using Arduino / Programming Questions / Re: Debounce routine works once only on: February 01, 2012, 04:56:57 pm
Yes, I did edit the post realising my error.  As mentioned above I will try modifying the Debounce, implementing a static instance of the timer
39  Using Arduino / Programming Questions / Re: Debounce routine works once only on: February 01, 2012, 04:47:29 pm
I meant to say portable, not portal, sorry.

It isn't complicated, i asked for help because I couldn't see why it shouldnt work.  The aim is to develop a robust set of classes that will make application development easier.  No doubt the solution to this problem is quite simple, just not found yet.

I will try a version tomorrow with a statically created timer, removing the dynamic element from the Debounce routine and see if that makes any difference.
40  Using Arduino / Programming Questions / Re: Debounce routine works once only on: February 01, 2012, 04:15:26 pm
Readability and good practice, the fact that the method just returns a member is not the point, it could be expanded to do more.  However none of these are the cause of this problem.
41  Using Arduino / Programming Questions / Re: Debounce routine works once only on: February 01, 2012, 04:03:58 pm
Ok, new version uploaded hopefully with a fix for the linked list indexing, however looking at the code, the delete is only performed for timers with a callback, this timer doesn't.
42  Using Arduino / Programming Questions / Re: Debounce routine works once only on: February 01, 2012, 03:56:26 pm
In that case I haven't uploaded the very latest version, very sorry...will see to that now.

I do see your point in serviceTimers and will see to that before I upload new release.
43  Using Arduino / Programming Questions / Re: Debounce routine works once only on: February 01, 2012, 03:45:28 pm
Code:
    if ( pTimer->m_pCallback != NULL ) {
// Perform call-back 
      (*pTimer->m_pCallback)(pTimer);
    }
The if condition before the callback ensures that the callback is only performed if a callback has been supplied.  In this case a callback has not been supplied.

@PeterH, I am building a suite of classes and functions, of course it can be done other ways, however I want to create a re-entrant portal class library.
44  Using Arduino / Programming Questions / Re: Debounce routine works once only on: February 01, 2012, 03:37:33 pm
TCNT2 = 0x06;

Is a oversight, I should have deleted it...will remove it, but I don't think that's anything to do with the problem.
45  Using Arduino / Programming Questions / Re: Debounce routine works once only on: February 01, 2012, 03:33:57 pm
Did you download the zip clsTimers.zip?  I stopped pasting the code and just updated the zip.  It is there, it has to be, code compiled without any warnings.
Pages: 1 2 [3] 4 5 ... 7