Loading...
  Show Posts
Pages: 1 [2] 3
16  Using Arduino / Programming Questions / Re: header file: order of definitions - using typdefs / structs in each others on: April 23, 2013, 08:41:48 am
sorry - i commented the definition out..
here comes the full code:
SwitchPCB_Test_RotaryEncoder_v02.h
Code:
/**
 ** see SwitchPCB_Test_RotaryEncoder_v02.ino                                                     **
 **/

/**
  * Includes Core Arduino functionality
  **/
#if ARDUINO < 100
#include <WProgram.h>
#else
#include <Arduino.h>
#endif
 
/** type and struct       definition                                                             **/

const byte state_UNDEFINED = 0;
const byte state_CW = 1;
const byte state_CCW = 2;
const byte state_HalfStep = 3;



// Input Event
struct sEncoder
{
const byte bNr;

byte pin_A;
byte pin_B;

boolean bA;
boolean bB;
boolean bA_last;
boolean bB_last;

boolean bLastFullStep;

byte bState;

unsigned long ulTimeStamp;

tCBF_OnStep cbfOnStep;
};
typedef struct sEncoder tEncoder;

// typdef for step callback
typedef void (* tCBF_OnStep) (tEncoder *encObj);

/** END                                        **/

SwitchPCB_Test_RotaryEncoder_v02.ino
Code:
/**************************************************************************************************
 ** RotaryEncoder Tester                                                                         **
 **  Tests the Rotary Encoders of the SwitchPCBs at 2ChControll Project                          **
 ** based on infos and ideas from:                                                               **
 **  ~ http://www.mikrocontroller.net/articles/Drehgeber                                         **
 **  ~ http://www.mikrocontroller.net/topic/drehgeber-auslesen                                   **
 **  ~ http://www.mikrocontroller.net/topic/38863                                                **
 **  ~ http://hacks.ayars.org/2009/12/using-quadrature-encoder-rotary-switch.html                **
 **  ~ http://playground.arduino.cc/Main/RotaryEncoders#Waveform                                 **
 **  written by stefan krueger (s-light), stefan@s-light.eu, http://s-light.eu                   **
 **  changelog / history                                                                         **
 **   17.02.2013 22:25 created                                                                   **
 **   23.04.2013 15:06 uncomment code for posting and testing in forum.                          **
 **                                                                                              **
 **                                                                                              **
 **  TO DO:                                                                                      **
 **   ~ ask for help with the call back function                                                 **
 **   ~ get this into a library or                                                               **
 **   ~ a 'addon' file to add to projects (to much customization needed to fit every encoder?!   **
 **                                                                                              **
 **************************************************************************************************/
 
/** Include yourselfs header file  (used to define structs and co)                               **/
#include "SwitchPCB_Test_RotaryEncoder_v02.h"
// use "" for files in same directory as .ino


/** definitions                                                                                  **/

tEncoder enc_left = {

111, //const byte bNr;

22, // byte pin_A;
26, // byte pin_B;

0, // boolean bA;
0, // boolean bB;
0, // boolean bA_last;
0, // boolean bB_last;
0, // boolean bLastFullStep;

state_UNDEFINED, // byte bState;

0, // unsigned long ulTimeStamp;

callback_Encoder_OnStep, // tCBF_OnStep cbfOnStep;

};

tEncoder enc_right = {

222, //const byte bNr;

24, // byte pin_A;
28, // byte pin_B;

0, // boolean bA;
0, // boolean bB;
0, // boolean bA_last;
0, // boolean bB_last;
0, // boolean bLastFullStep;

state_UNDEFINED, // byte bState;

0, // unsigned long ulTimeStamp;

callback_Encoder_OnStep, // tCBF_OnStep cbfOnStep;
};



/** functions                                                                                    **/

void callback_Encoder_OnStep (tEncoder *encObj) {
/** Debug Out **/
Serial.print((*encObj).bNr);
Serial.print(":");
Serial.println((*encObj).bState);
/** **/
}

/************************************************/
/** read Encoder and rect                      **/
/************************************************/
void readEncoder(tEncoder *encObj) {

/**
rast punkt immer bei 0,0 oder 1,1
CCW
---- A, B
111: 0, 1
111: 0, 0 !
111: 1, 0
111: 1, 1 !
---- A, B
111: 0, 1
111: 0, 0 !
111: 1, 0
111: 1, 1 !

CW
---- A, B
111: 1, 0
111: 0, 0 !
111: 0, 1
111: 1, 1 !
---- A, B
111: 1, 0
111: 0, 0 !
111: 0, 1
111: 1, 1 !

Error
---- A, B
111: 1, 0
111: 0, 0
111:1
111+1
111: 0, 1
111: 1, 1
111:1
111+1
111: 1, 0
111: 1, 1
111:2 <--Wrong reading!!!
111-1
111: 1, 0
111: 0, 0
111:1
111+1

this reading is wrong.
it is not alowed to have 1,1; 1,0; 1,1

**/

boolean bTemp_A = digitalRead((*encObj).pin_A);
boolean bTemp_B = digitalRead((*encObj).pin_B);
if ( (bTemp_A != (*encObj).bA) || (bTemp_B != (*encObj).bB)) {
//status changed.
(*encObj).bA = bTemp_A;
(*encObj).bB = bTemp_B;

Serial.print((*encObj).bNr);
Serial.print(": ");
Serial.print((*encObj).bA);
Serial.print(", ");
Serial.println((*encObj).bB);

// half step; save state of encoder pins for direction check
if ((*encObj).bA != (*encObj).bB) {
(*encObj).bA_last = (*encObj).bA;
(*encObj).bB_last = (*encObj).bB;
(*encObj).bState = state_HalfStep;
} else {
// full step; one step is finished. --> This is Only true if the last full step was not the same level (high or low)
if ( ((*encObj).bA == (*encObj).bB) && ((*encObj).bA != (*encObj).bLastFullStep) ) {
//remember LastFullStep (error correction)
(*encObj).bLastFullStep = (*encObj).bA;
// Encoder turns CW
if ((*encObj).bA == (*encObj).bB_last)  {
//Do Something
(*encObj).bState = state_CW;
/**  RUN CALLBACK Function: **/
(*encObj).cbfOnStep(encObj);

// direct call for testing..
  //callback_Encoder_OnStep(encObj);
/** **/
Serial.print((*encObj).bNr);
Serial.println(": +1");
/** **/


} else {
// Encoder turns CCW
if ((*encObj).bB == (*encObj).bA_last)  {
//Do Something
(*encObj).bState = state_CCW;
/**  RUN CALLBACK Function: **/
(*encObj).cbfOnStep(encObj);

// direct call for testing..
  //callback_Encoder_OnStep(encObj);
/** **/
Serial.print((*encObj).bNr);
Serial.println(": -1");
/** **/


}
}
}
}
}



}

void setup() {
pinMode(enc_left.pin_A, INPUT_PULLUP);
pinMode(enc_left.pin_B, INPUT_PULLUP);
pinMode(enc_right.pin_A, INPUT_PULLUP);
pinMode(enc_right.pin_B, INPUT_PULLUP);

} // setup


/** Main Loop                                                                                      **/
void loop() {
readEncoder(&enc_left);
readEncoder(&enc_right);
}

and the errors when i try to compile:
Code:
In file included from SwitchPCB_Test_RotaryEncoder_v02.ino:51:
SwitchPCB_Test_RotaryEncoder_v02.h:46: error: 'tCBF_OnStep' does not name a type
SwitchPCB_Test_RotaryEncoder_v02:89: error: too many initializers for 'tEncoder'
SwitchPCB_Test_RotaryEncoder_v02:109: error: too many initializers for 'tEncoder'
SwitchPCB_Test_RotaryEncoder_v02.ino: In function 'void readEncoder(tEncoder*)':
SwitchPCB_Test_RotaryEncoder_v02:208: error: 'struct sEncoder' has no member named 'cbfOnStep'
SwitchPCB_Test_RotaryEncoder_v02:224: error: 'struct sEncoder' has no member named 'cbfOnStep'

if i put the typedef of the function for the sEncoder struct i have some other / similar errors
Code:
In file included from SwitchPCB_Test_RotaryEncoder_v02.ino:51:
SwitchPCB_Test_RotaryEncoder_v02.h:27: error: typedef 'tCBF_OnStep' is initialized (use __typeof__ instead)
SwitchPCB_Test_RotaryEncoder_v02.h:27: error: 'tEncoder' was not declared in this scope
SwitchPCB_Test_RotaryEncoder_v02.h:27: error: 'encObj' was not declared in this scope
SwitchPCB_Test_RotaryEncoder_v02.h:48: error: 'tCBF_OnStep' does not name a type
SwitchPCB_Test_RotaryEncoder_v02:89: error: too many initializers for 'tEncoder'
SwitchPCB_Test_RotaryEncoder_v02:109: error: too many initializers for 'tEncoder'
SwitchPCB_Test_RotaryEncoder_v02.ino: In function 'void readEncoder(tEncoder*)':
SwitchPCB_Test_RotaryEncoder_v02:208: error: 'struct sEncoder' has no member named 'cbfOnStep'
SwitchPCB_Test_RotaryEncoder_v02:224: error: 'struct sEncoder' has no member named 'cbfOnStep'

i think i missing some basic understanding of this..

thanks for your help

sunny greetings stefan

PS: how do you prefer such code? in-line or as attached file? i had to remove some formating comments to get under 9500 characters..
17  Using Arduino / Programming Questions / Re: Web submit button that disables after click for 15 minutes and then enables on: April 22, 2013, 10:47:49 pm
if i understand you right -
you are using an arduino as webserver with hardware control ?
than it is realy easy-


Code:
// PSEUDO CODE!!!

// create a variable to trac state of sytem / define states (for readabilty)
const byte cbState_Undefined = 0;
const byte cbState_free = 1;
const byte cbState_locked = 2;
byte bState = cbState_free;

//create a variable to hold the remaining time (15min*60seconds = 900 --> word)
word wRemainingTime = 0;

//create a variable to hold a TimeStamp (unsined long)
unsigned long ulTimeStamp = 0;

/////////////////////////////

// function skeleton for getting state and remaining time info for web
void getInfo() {
if ( bState == cbState_locked ) {
// Display remaining time --> found in wRemainingTime as seconds.
} else {
// Display Button
}
}

// function that is triggerd if button on webside is pressed:
void pushButton() {
if ( bState == cbState_free ) {
// set system state to locked
bState = cbState_locked;
// start wait time with 900seconds (=15min)
wRemainingTime = 900;
//original feed thing goes in this sub function
feedPets();
}
}

//original feed thing goes in this sub function
void feedPets() {
// Your Feeding things...
}


//in your main loop:
if (millis()-ulTimeStamp >= 1000) {
ulTimeStamp = millis();
//do something every second:
if ( bState == cbState_locked ) {
if ( wRemainingTime > 0 ) {
wRemainingTime = wRemainingTime - 1;
} else {
// time is over - pets are allowed to get some thing..
bState = cbState_free;
}
}

}
hey - this is just a dirty hack free out of my mind -
i hope it helps to get a idea how to go for this.

if you want to see the count down counting on the webside  - you need javascript!
the concept at this part would be to get the webside with the remaining time and a small javascript that counts down this time.
and if it is reaching 0 it just reloads the page.
i have set up a small example: http://jsfiddle.net/s_light/eZ8DJ/2/
have fun
sunny greetings stefan
18  Using Arduino / Programming Questions / Re: Ports on the Arduino Mega Pins 22-53 on: April 22, 2013, 09:43:39 pm
just look in the first column.
there are the arduino digital-pin numbers.
if you have find the number - for example Digital-Pin 7 you can follow the line and find that this arduino pin is on Port C 7
sunny greetings stefan
19  Using Arduino / Audio / Re: I2C volume control on: April 22, 2013, 09:08:23 pm
Hi christianlykke,

hm the datasheet is not so clear -
you can try to use the i2c_scanner
there is another I2C Scanner out there.
as fare as i know normaly you have to use the begin and end Transmission funcitons.
otherweise the i2c or TWI will not work.
you can also check the return of the Wire.endTransmission() :
Quote
Returns

byte, which indicates the status of the transmission:

    0:success
    1:data too long to fit in transmit buffer
    2:received NACK on transmit of address
    3:received NACK on transmit of data
    4:other error

for your chip are not much infos available..
pleas report back if you found something out.

sunny greetings stefan
20  Using Arduino / Programming Questions / header file: order of definitions - using typdefs / structs in each others on: April 22, 2013, 08:26:47 pm

Hello Community,

i am Experimenting with Structures and Callback functions. (build a 'Input Event System')
Now iam at the point where i try to make the calling 'object' (a typdefed struct) available in the called function
in my headerfile i have the following definitions:
Code:
// Encoder 'Object'
struct sEncoder
{
const byte bNr;

byte pin_A;
byte pin_B;

boolean bA;
boolean bB;
boolean bA_last;
boolean bB_last;

boolean bLastFullStep;

byte bState;

unsigned long ulTimeStamp;

//tCBF_OnStep cbfOnStep;
};
typedef struct sEncoder tEncoder;

// typdef for input state change callback
//typedef void (* tCBF_OnStep) (tEncoder *encObj);

in my ino file iam using it this way:
Code:
...
/**************************************************************************************************/
/** Include yourselfs header file  (used to define structs and co)                               **/
/**************************************************************************************************/
#include "SwitchPCB_Test_RotaryEncoder_v02.h"
// use "" for files in same directory as .ino

/**************************************************************************************************/
/** definitions                                                                                  **/
/**************************************************************************************************/

tEncoder enc_left = {

111, //const byte bNr;

22, // byte pin_A;
26, // byte pin_B;

0, // boolean bA;
0, // boolean bB;
0, // boolean bA_last;
0, // boolean bB_last;
0, // boolean bLastFullStep;

state_UNDEFINED, // byte bState;

0, // unsigned long ulTimeStamp;

};

tEncoder enc_right = {
....
};

/**************************************************************************************************/
/** functions                                                                                    **/
/**************************************************************************************************/

void callback_Encoder_OnStep (tEncoder *encObj) {
/** Debug Out **/
Serial.print((*encObj).bNr);
Serial.print(":");
Serial.println((*encObj).bState);
/** **/
}

void readEncoder(tEncoder *encObj) {
boolean bTemp_A = digitalRead((*encObj).pin_A);
boolean bTemp_B = digitalRead((*encObj).pin_B);
.....
/**  RUN CALLBACK Function: **/
(*encObj).cbfOnStateChange(encObj);
.....
}

/****************************************************************************************************/
/** Main Loop                                                                                      **/
/****************************************************************************************************/
void loop()
{
readEncoder(&enc_left);
readEncoder(&enc_right);
}

so with this setup the compiler stops and says:
Code:
In file included from SwitchPCB_Test_RotaryEncoder_v02.ino:42:
SwitchPCB_Test_RotaryEncoder_v02.h:42: error: 'tCBF_OnStep' does not name a type
- ok thats clear - on this point he does not know the definition that comes later  -
but i also cant reorder this - because in the function i use the type tEncoder - and if i write this for the tEncoder definition he also cant find it..

i hope it is clear what iam trying to do -
is there a solution or iam just on the absolute wrong way with this concept?!
i just know a little about this 'callback function' and pointer and reference and co system.
if you know a good tutorial or example how to do such things i am ready to learn new things..
(iam coming from the computer and javascript world - and sometimes its hard to think in limited memory and co..)

sunny greetings stefan
21  Using Arduino / Installation & Troubleshooting / Re: Arduino IDE start up parameter: sketchbook folder on: April 22, 2013, 05:05:43 pm
Thanks for your answers,

you are right with the sub folder -
but i enjoy to have only the user library that i use in this project -
ok - sometimes its a bit to much copy and paste -
and some times its the cleanest way to have things separated from each other..
i will look into command-line compilation in some time..
but for now this is a step to big for me..
i love the 'just work' concept of the arduino IDE :-)

have fun
stefan
22  Using Arduino / Installation & Troubleshooting / Arduino IDE start up parameter: sketchbook folder on: April 22, 2013, 02:51:15 pm
Hello Community,

is there a option / commandline flag to set the sketchbook folder on startup of the IDE?
why i do want to do this?
I have for every project a separate folder. in every project folder there is a folder called 'arduino_sketchbook'.
Example:
Code:
- prj1_HelloWorldClock
    - infos
    - datasheets
    - arduino_sketchbook
       - sketch1
       - sketch2
       - libraries
       - examples
 - prj2_WakeUpLight
    - infos
    - datasheets
    - arduino_sketchbook
       - sketch1
       - sketch2
       - libraries
       - examples
....

so my idea was to make some File shortcuts all pointing to the arduino IDE but with different command line parameters so that my needed arduino_sketchbook is loaded automatically.
(i use windows 7 and Arduino IDE 1.0.4)

if someone has an idea how to do this - just answer :-)

sunny greetings
stefan
23  Community / Website and Forum / Bootloader on: January 08, 2013, 05:11:07 pm
Hey Community :-)

i was on the search of how to burn bootloaders to fresh microcontrollers.
on the side of the boards it is always linked to
http://arduino.cc/en/Tutorial/Bootloader
but this side redirects to
http://arduino.cc/en/Guide/Environment?from=Tutorial.Bootloader
i think it should link to the Uploading part in the document:
http://arduino.cc/en/Guide/Environment?from=Tutorial.Bootloader#uploading

In the Tools Section are also two link pointing to the Tutorial/Bootloader page  - that means they pointing to the same document you reading in  smiley-confuse
Quote
Programmer
For selecting a harware programmer when programming a board or chip and not using the onboard USB-serial connection. Normally you won't need this, but if you're burning a bootloader to a new microcontroller, you will use this.

Burn Bootloader
The items in this menu allow you to burn a bootloader onto the microcontroller on an Arduino board....

is there some where some more information on the concept of the bootloaders for the different boards?

sunny greetings
stefan
24  Using Arduino / Networking, Protocols, and Devices / I2C TWI Scanner on: December 28, 2012, 01:00:13 pm
Hey comunity,

i found this by searching for an I2C / TWI  Scanner.
in the sketch posted here is a smal error with the Arduino IDE 1.0.3 (others not tested.)

Corrected Sketch:
Code:
/**
 * TWI_I2C_Scanner.ino
 * I2CScanner.pde -- I2C bus scanner for Arduino
 *
 * 2009, Tod E. Kurt, http://todbot.com/blog/
 * http://todbot.com/blog/2009/11/29/i2cscanner-pde-arduino-as-i2c-bus-scanner/comment-page-1/
 * http://arduino.cc/forum/index.php/topic,95067.msg714094.html#msg714094
 * https://getsatisfaction.com/thingm/topics/cant_get_blinkm_communicator_to_compile_and_upload
 *
 */

#include "Wire.h"
extern "C" {
#include "utility/twi.h"  // from Wire library, so we can do bus scanning
}

// Scan the I2C bus between addresses from_addr and to_addr.
// On each address, call the callback function with the address and result.
// If result==0, address was found, otherwise, address wasn't found
// (can use result to potentially get other status on the I2C bus, see twi.c)
// Assumes Wire.begin() has already been called
void scanI2CBus(byte from_addr, byte to_addr,
                void(*callback)(byte address, byte result) )
{
  byte rc;
  byte data = 0; // not used, just an address to feed to twi_writeTo()
  for( byte addr = from_addr; addr <= to_addr; addr++ ) {
    rc = twi_writeTo(addr, &data, 0, 1, 0);
    callback( addr, rc );
  }
}

// Called when address is found in scanI2CBus()
// Feel free to change this as needed
// (like adding I2C comm code to figure out what kind of I2C device is there)
void scanFunc( byte addr, byte result ) {
  Serial.print("addr: ");
  Serial.print(addr,HEX);
  Serial.print( (result==0) ? " found!":"       ");
  Serial.print( (addr%4) ? "\t":"\n");
}


byte start_address = 1;
byte end_address = 127;

// standard Arduino setup()
void setup()
{
    delay(2000);
    Wire.begin();
    Serial.begin(115200);
    Serial.println("\nI2CScanner ready!");

    Serial.print("starting scanning of I2C bus from ");
    Serial.print(start_address,HEX);
    Serial.print(" to ");
    Serial.print(end_address,HEX);
    Serial.println("...Hex");

    // start the scan, will call "scanFunc()" on result from each address
    scanI2CBus( start_address, end_address, scanFunc );

    Serial.println("\ndone");
}

// standard Arduino loop()
void loop()
{
    // Nothing to do here, so we'll just blink the built-in LED
    digitalWrite(13,HIGH);
    delay(300);
    digitalWrite(13,LOW);
    delay(300);
}

the correction is in line 27:
twi_writeTo needs one additional parameter: 0 for a normal stop command.
i found this info here: https://getsatisfaction.com/thingm/topics/cant_get_blinkm_communicator_to_compile_and_upload

Have fun with I2C / TWI
sunny greetings
stefan
25  Using Arduino / Project Guidance / Re: 24 motorized fader mixer with Arduino on: June 26, 2012, 01:41:42 am
hi Mytx,

thanks for the explanation :-)
i think the idea to use a PID is good idea - but eventually over-sized ;-)

the trick at my concept is: the motor controller is set to active state if there is a new target value.
than it drives in the right direction to reach that point.
the speed with which the fader drives depends on the remaining distance - long distance = fast; short distance = slow;
if it is near this point (+- 1 Digit) (or an timeout occurs as example if the fader is blocked mechanically) the controller stops the motor and sets itself to standby.
in standby it waits until the state is set to active.

so at the first test i tested this without the active thing. than the fader is oscillating (drives a smal step to far - the controller switches the direction and it will drive the next step to far...)

so this system is not a real pid or something similar - its much easier and justs needs an activation.
it is not always active...

i hope i have the time to make the video this evening -
for the library there are some small things i have to do and if this is finised i will realeas it - i will let you know^^


sunny greetings

stefan
26  Using Arduino / Project Guidance / Re: 24 motorized fader mixer with Arduino on: June 25, 2012, 03:30:49 pm
hey :-)

thanks for the welcome.

@Mytx what do you mean with PID? i dont knwo the term?
tomorrow i can upload a video of the current state of my project.

to the TouchThing:
in the Playground are two interesting options for Touch / Capacitive Sensing:
Capacitive Sensing Library by Paul Badger a full library - but if i understand this right it needs a lots of time...
Native Capacitive Sensors without additional Hardware really basic - just a function and a short piece of wire..
i do not have tested them yet - i will com to this point on my list earliest at the end of the year.

i hope one of this things work for you and the fader - and let me know i am interested too^^

sunny greetings

stefan
27  Using Arduino / Project Guidance / Re: 24 motorized fader mixer with Arduino on: June 23, 2012, 03:12:37 pm
Hi @All :-)

thanks Mytx for your info about your project.
(he answered to my project info/ request)

i have an motorfader - and a arduino sketch that controls it.
My actual working state is as following:
you can set the fader to a position:
driveTo(900); // 0..1023
the software runs a speed/ distance map.
so if the fader must move a long way it will drive with maximum speed - and when it comes nearer to the target position it will slow down.
with this system i get a relative small tolerances. ( i think its 2% - on an 100mm fader means 2mm ;-) )

at the moment i work on the packaging into an arduino lib.
will be ready the next days. (i hope ;-) )

I used this fader:
Sparkfun Slide Pot - Motorized (10k Linear Taper)
and there are also some with audio taper profiles:
Sparkfun Slide Pot - Motorized (10k Audio Taper)
this things are from Top-Up-Industries
an other motorized fader from ALPS

as driver i used the Arduino Motor Shield
with this i can drive two faders.
my next step on the hardware side will be also to use an L293D QUADRUPLE HALF-H DRIVER from TI
it has the Output Clamp Diodes build in - thats really cool - so you dont need so much things around. (i just missing the inverter thing)

sunny greetings

stefan
28  Using Arduino / Programming Questions / Re: struct and arrray size on: June 22, 2012, 04:30:47 pm
ok thanks majenko!
i will define a maximal for my use case!
29  Using Arduino / Programming Questions / struct and arrray size on: June 22, 2012, 03:28:52 pm
Hey @all,

i would love to know how to set up a struct with some arrays.
the challenging is, that the array size is not know.
The Idea is as following:
there is this really cool MultiMap function (http://arduino.cc/playground/Main/MultiMap)
and my idea was to build a struct that holds all needed things for a nice mapping:
Code:
struct sMultiMapStruct
{
word mm_In [];
word mm_Out [];
byte size;
};

typedef struct sMultiMapStruct tmm;

for testing:
Code:
/****************************************************************************************************
  *   structTest sketch
  *   
  *   
  *   Created:  22.06.2012
  *   copyright by Stefan Krueger
  *   written by Stefan Krueger stefan@s-light.eu
  *   
  *   Last modified: 22.06.2012 21:00
  *   By Stefan Krueger
  *   stefan@s-light.eu
  *   
  *   changelog / history :
  *
  *
****************************************************************************************************/


struct sMultiMapStruct
{
word mm_In [];
word mm_Out [];
byte size;
};

typedef struct sMultiMapStruct multiMap_type;


multiMap_type mmMyMap = {
{  0, 20,  30},
{  0, 80, 100},
3
};





/**
  * MultiMap
  * http://arduino.cc/playground/Main/MultiMap
  *  modified for 16bit word datatype
  **/
word multiMap(word val, word* _in, word* _out, uint8_t size)
{
  // take care the value is within range
  // val = constrain(val, _in[0], _in[size-1]);
  if (val <= _in[0]) return _out[0];
  if (val >= _in[size-1]) return _out[size-1];

  // search right interval
  uint8_t pos = 1;  // _in[0] allready tested
  while(val > _in[pos]) pos++;

  // this will handle all exact "points" in the _in array
  if (val == _in[pos]) return _out[pos];

  // interpolate in the right segment for the rest
  return map(val, _in[pos-1], _in[pos], _out[pos-1], _out[pos]);
}


/**  struct sMultiMapStruct used so you need no headerfile for struct definition **/
word mapIt(word wInputValue, struct sMultiMapStruct* mmTheMap)
{
// '(*mmTheMap).mm_In' could be also written as 'mmTheMap->mm_In'
return multiMap(wInputValue, (*mmTheMap).mm_In, (*mmTheMap).mm_Out, (*mmTheMap).size);
}


/***************************************************************************************************/
void setup() 
{
/**************************************************/
/* Welcom @ This Sketch!!                         */
/**************************************************/
Serial.begin(115200);
Serial.println();
Serial.println("       ");
Serial.println("   oo  ");
Serial.println("  (  ) ");
Serial.println("   ^^  ");
Serial.println();
Serial.println("Welcome to structTest Sketch!");
Serial.println("system is starting!");
Serial.println(":-)");
Serial.println("");
}

void loop()
{
for ( word x = 0; x < 30; x++)
{
Serial.print(x);
Serial.print("-->");
Serial.println(mapIt(x, &mmMyMap ));
//delay(10);
if ( x == 20 )
{
Serial.println("*** 20 ***");
delay(100);
}
}

Serial.println();
delay(500);
Serial.println("**************************************");
delay(500);
Serial.println();
delay(500);
Serial.println("**************************************");
delay(500);
Serial.println();
delay(500);
Serial.println("**************************************");
delay(500);
Serial.println();
}
(

now the problem is - i try to setup the size of the arrays at declaration of the variable.
error:
Code:
structTest:32: error: too many initializers for 'word [0]'
structTest:32: error: too many initializers for 'word [0]'

is there some easy possibilities to do this an other way?

sunny greetings

stefan
30  Using Arduino / Project Guidance / Re: BPM Counter / Metronome for triggering a DMX console - need a little help on: June 18, 2012, 03:24:15 pm
Hi Helmuth,

i have not read the hole code.
but here are some ideas about timing:
lcds uses normally 'really much time' :-)
and also float calculation is slow:
Reference:Float
Quote from: Arduino Reference - Float
Floating point math is also much slower than integer math in performing calculations, so should be avoided if, for example, a loop has to run at top speed for a critical timing function. Programmers often go to some lengths to convert floating point calculations to integer math to increase speed.
as example - if you need some accuracy like 1.3 you can use 13 as int and know that you have to divide by 10...
so in the last step before printing it on serial or lcd you can print the right value :
13/10 = 1  ( 13 divided by 10 in integer math gives 1)
            .
13%10 = 3  ( 13 modulo 10 (the rest of the division) is 3)


it would help if you just write up a smal overview about how you think it should work - something like an 'flowchart'
then it is faster for others to look through the code.
just write something like:
read input of Pushbutton1
count up
write lcd message
 - status
 - name of the day
make a tone
...

its just an idea-
i will look into your code in the next days-

some general thing - you could give your variables some cleaner naming scheme:
i name them something like this:
Code:
// Pin Definitions
byte pin_PushButton_Up = 1;
byte pin_PushButton_Down = 2;
byte pin_LightSensor = A1;

// Light Sensor
word wLightSensor_Raw = 0;
word wLightSensor_Filtert = 0;

// PushButton States
byte bPushButton_State_Up = 0; // is > 1 if Button is Active; is >2 if Button is Pressed a long time
byte bPushButton_State_Down = 0; // is > 1 if Button is Active; is >2 if Button is Pressed a long time
and hey - thats just how i like it^^
there will be others that hate so long variable names ;-)

i think there will be some nice 'code style guides' out there in the net..

for me its easy to understand what i can do with the thing i just ride down..

sunny greetings

stefan
Pages: 1 [2] 3