Show Posts
Pages: 1 ... 3 4 [5] 6 7 ... 142
61  Using Arduino / Programming Questions / Re: Keypad example compiling error on: July 08, 2014, 06:50:42 am
As he are complaining about redefinition, maybe you can have more than one tab opened.

There might be a .h or .cpp included without include guards.

If you are asking weather you can have more than one tab, then yes you can: http://arduino.land/FAQ/content/7/42/en/how-to-use-multiple-tabs.html
62  Using Arduino / Programming Questions / Re: Shift registers - other way around. on: July 08, 2014, 01:50:55 am
Quote
( vertical ? y : x ) = 1;
This one doesn't look valid to me.

I know, that's why I posted it  smiley-cool. I have written a little more about it here:  What does this code do? a = x ? y : z;
63  Using Arduino / Programming Questions / Re: Shift registers - other way around. on: July 07, 2014, 05:06:04 am
Quote
You're right and I was wrong.
Me too. I missed the i= part of the statement. I've never seen a ternary operator (mis)used that way.

Its not misusing at all, it just seems it as most of us would probably write:
Code:
i = ( digitalRead( inPin )==HIGH ? 1 : 0 );
//Or
i = ( digitalRead( inPin ) == HIGH );

But when there are different items to write to, it looks fine.
Code:
vertical ? y=1 : x=1;
//or
( vertical ? y : x ) = 1;
64  Using Arduino / Programming Questions / Re: If statement takes 1kb of memory on: July 06, 2014, 10:11:20 pm
Ok, I managed to drop it from 2046 to 1682 bytes, saving 364 bytes.

Then after a few inlines and econjacks note about the trailing decimals. Its now 892 bytes, saving 1194 bytes.

For an instant 42 bytes off, use the extended version of my SmallSetup lib ( I just added support for the Attiny25, more can follow ).
http://arduino.land/Code/SmallSetup/

Get this library: DigitalIO and install it ( fat16libs thread ).

Open the file DigitalPin.h ( in the above lib )  and add in the pins for the attiny, there is a large #if/#elif block, add this to it ( at line 70 will do ):
Code:
#elif defined( __AVR_ATtiny25__ )
//Attiny 25
const static pin_map_t pinMap[] = {
  {&DDRB, &PINB, &PORTB, 0},  // D0  0
  {&DDRB, &PINB, &PORTB, 1},  // D1  1
  {&DDRB, &PINB, &PORTB, 2},  // D2  2
  {&DDRB, &PINB, &PORTB, 3},  // D3  3
  {&DDRB, &PINB, &PORTB, 4},  // D4  4
  {&DDRB, &PINB, &PORTB, 5},  // D5  5
};

The sketch I have now is as follows, however it still contains the errors byte minimum = 320;
Code:
#include <avr/sleep.h> //Needed for sleep_mode
#include <avr/wdt.h> //Needed to enable/disable watch dog timer
#include <avr/power.h>
#include <DigitalIO.h>

const byte pumpPin = 3;
const byte sensorPowerPin = 4;
const byte sensorPin = A1;
DigitalPin< pumpPin > pump;
DigitalPin< sensorPowerPin > sensorPower;  

const byte minimum = 320;
const byte maximum = 950;
byte wdt_timer = 0;

ISR(WDT_vect)
{
    wdt_timer += 1;
}



inline void setup() {    
// initialize the digital pins as an output.
 pump.mode( OUTPUT );  //pinMode(pumpPin, OUTPUT);
 sensorPower.mode( OUTPUT );  //pinMode(sensorPowerPin, OUTPUT);
 
  /* Clear the reset flag. */
  MCUSR &= ~(1<<WDRF);
  /* In order to change WDE or the prescaler, we need to
   * set WDCE (This will allow updates for 4 clock cycles).
   */
  WDTCR |= (1<<WDCE) | (1<<WDE);
  /* set new watchdog timeout prescaler value */
  WDTCR = 1<<WDP0 | 1<<WDP3; /* 8.0 seconds */
  /* Enable the WD interrupt (note no reset). */
  WDTCR |= _BV(WDIE);
  
}

inline void measure(){
  sensorPower.high();  //digitalWrite(sensorPowerPin,HIGH);
  delay(100);
  int reading = analogRead(sensorPin);
  sensorPower.low();  //digitalWrite(sensorPowerPin, LOW);
  
  int sensorValue = 100-(reading-minimum)/(maximum-minimum)*100;

  if (sensorValue < 35){
    
    pump.high();  //digitalWrite(pumpPin, HIGH);
    delayReset();
    delayReset();
    pump.low();  //digitalWrite(pumpPin, LOW);
  }
}

 inline void loop() {
  enterSleep();
  
  if (wdt_timer > 225){
    wdt_timer = 0;
    measure();
  }
}

void delayReset(){
  delay(5000);
  wdt_reset();
}

void enterSleep(void)
{
  /* Disable ADC */
  ADCSRA &= ~(1<<ADEN);
  
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);   /* EDIT: could also use SLEEP_MODE_PWR_DOWN for lowest power consumption. */
  sleep_enable();
  
  /* Now enter sleep mode. */
  sleep_mode();
  
  /* The program will continue from here after the WDT timeout*/
  sleep_disable(); /* First thing to do is disable sleep. */
  
  /* Re-enable the peripherals. */
  power_all_enable();
  
  /* Enable ADC */
  ADCSRA |= (1<<ADEN);
}


#include <SmallSetup.h>

Hopefully this gives you enough room to finish your code. Using pinMode, digitalRead, digitalWrite will cause the code to grow. Stick to the DigitalPin library for constant pins, and it has a class PinIO for dynamic pins.
You could just use port mapping directly, however the lib provides an easy to understand interface ( and is just as efficient ).
65  Using Arduino / Programming Questions / Re: odd error i cant figure out on: July 05, 2014, 08:21:17 am
Hi, have you done a "TOOLS"  then "Auto Format" tags at the top of the IDE.
I have found that it will apart from align your code indents, it will also check for correct  { and }.

Tom..... smiley

What IDE, in 1.5.6 I can't get any errors about missing '{' or '}'.

A worst case scenario even works: "void setup(){{{{{{{{void loop(){int a;" will happily auto format to:
"void setup() {  {{{{{{{                void loop() {                  int a;"

Not even a new line gets inserted. Maybe some features of Auto format were dropped after 1.0.5
66  Using Arduino / Programming Questions / Re: I've broken print . on: July 05, 2014, 08:12:59 am
I suspect Ian was hoping to access the array while it is in Progmem without needing to copy it to sram because he is short of sram space.

Looks like Progmem can't be used like that.

...R

I think there has been a misunderstanding here. Both memcpy_P and memcmp_P only load one byte at a time, its the destination or comparison data that needs to be in ram.
67  Using Arduino / Networking, Protocols, and Devices / Re: how to remove HTTP response header received from webservice on: July 05, 2014, 07:35:36 am
In your IDE, open up the example WebClient in the Ethernet section.

It implements a variable currentLineIsBlank to detect the two CRLF's ( one on the end of the last header, and one on a new line by itself )

You can also view the sketch here:
https://github.com/arduino/Arduino/blob/master/libraries/Ethernet/examples/WebServer/WebServer.ino#L54
68  Using Arduino / Programming Questions / Re: Sketch Optimization and Feasibility On An ATtiny on: July 05, 2014, 07:26:57 am
Explicitly initialize the variables improves the robustness, readability and portability of the code.
- robustness: when you decide the global array can be moved to a local array it will be initialized correctly
- readability: you can see explicit what the initial value is.
- portability: if another compiler (setting) initializes global var differently (or not at all)

For sure with respect to global variables that aren't initialized at all on the AVR, however {} is an explicit 'default initialization'. I wouldn't recommend manually defaulting all the elements of say: bool Settings[ 4000 ]; over using a single short line.

Default initialization is not just an idiom of arrays, it is used for POD's too.
Code:
struct Foo{
  char a;
  int b;
  float c;
  bool d;
};

//Can be default initialized:
Foo f = {};


Also when using dynamic memory, default initialization is all you can do with arrays ( slightly different syntax ).
Code:
char *arr = new char[ max ]();
69  Using Arduino / Networking, Protocols, and Devices / Re: how to remove HTTP response header received from webservice on: July 05, 2014, 01:11:26 am
just skip the data until you reach what you are looking for.

The standard says this too ( for http 1.1 ):
Quote
Request (section 5) and Response (section 6) messages use the generic message format of RFC 822 [9] for transferring entities (the payload of the message). Both types of message consist of a start-line, zero or more header fields (also known as "headers"), an empty line (i.e., a line with nothing preceding the CRLF) indicating the end of the header fields, and possibly a message-body.


70  Using Arduino / Project Guidance / Re: How to do a software reset on: July 05, 2014, 01:06:42 am
Why can you not re-sync just like a reset would do? A reset seems quite an overkill for a sync error!

Something like a state machine you can 'reset' in code might achieve what you want.

Or simply change the context of your app. Rather than thinking of the hardware/global scope as the top layer, make your loop function the top layer, as in: one loop is an entire life time.

Maybe this example will help get my view across:
Code:
volatile bool errorFound;  //Only volatile cos an interrupt uses it.

void setup() {}

void loop() {
 
  //Initialize system here:
  errorFound = false;
 
  while( !errorFound ) loop2();
 
  //Shutdown / clean up anything here:
}

void loop2(){
  //Application loop.
}

void someInterrupt(){
 
  if( !errorFound ){
   
    //Application is still in a running state, do something.
  }
}
71  Using Arduino / Programming Questions / Re: Sketch Optimization and Feasibility On An ATtiny on: July 04, 2014, 07:32:25 pm
pYro_65, it sounds like
Code:
boolean buttonToggle[3] = {false, false, false};
is the same as
Code:
boolean buttonToggle[3] = {};
as the values would default to 0, which is the same as false?

Yep, that is correct. Also on an AVR, unless you specify, all global variables will contain zero on start up, so you can save some flash and remove the initialization as the variable is already zero/false. This does not apply to local variables though ( variables defined inside a function )
72  Using Arduino / Programming Questions / Re: I've broken print . on: July 04, 2014, 07:30:17 pm
Quote
I've been unable to find one so far. As AWOL mentioned, you can use memcpy_P, but that kind of defeats the point of storing the array in PROGMEM in the first place. I am using memcmp_P  to compare the contents of an array in RAM with an array in PROGMEM, but it's not reading the contents of the array in the way we would like.

I can't help but think there must be a neater way of doing it without a FOR loop, but I've yet to find it. I need to do more reading on pointers.

Only 1 byte at a time ( limit set by the hardware ). And memcpy_P/memcmp_P just run pgm_read_byte in a loop. If you want to compare without using memcmp_P, then use a loop, its not untidy. There are functions like pgm_read_word/dword, which will read 2 or 4 values ( however they just call read_byte 2 or 4 times ).

In what way do you need to read the array where memcpy_P/memcmp_P  does not suffice?
73  Using Arduino / Programming Questions / Re: Sketch Optimization and Feasibility On An ATtiny on: July 04, 2014, 08:25:54 am
Code:
boolean buttonToggle[3] = {false};
this sets only the first element to false, the others are undefined....

The others are default initialized. As boolean is an uint8_t, its default value is zero. The line above produces the same result as:
Code:
boolean buttonToggle[3] = {};
74  Using Arduino / Programming Questions / Re: I've broken print . on: July 04, 2014, 07:29:25 am
When I learned C,  this would have been an error without the keyword struct  or a typedef for it.

It should still be the case if you wrote it in a .c file.

@OP, you are using pgm_read_byte wrong.

Code:
byte DSP_STATUS_REPLY_RST [6]  PROGMEM = { 0x6A , 0x04 , 0xFF , 0x02 , 0x01 , 0x92 };

To read the first byte of the array you could do this:
Code:
byte b = pgm_read_byte( DSP_STATUS_REPLY_RST );

To read the whole array in a loop, you could do this:
Code:
for( char i = 0 ; i < sizeof( DSP_STATUS_REPLY_RST ) ; ++i ){
    byte b = pgm_read_byte( DSP_STATUS_REPLY_RST + i );
    Serial.println( b, HEX );
}

Note: sizeof works in this instance cos the array is an array of single bytes, anything larger needs help: http://arduino.land/FAQ/content/6/29/en/how-to-get-the-size-of-an-array.html

You can also rewrite the access code to use array syntax rather than pointer arithmetic:
Code:
pgm_read_byte( DSP_STATUS_REPLY_RST + i );
//Or
pgm_read_byte( &DSP_STATUS_REPLY_RST[ i ] );


75  Development / Other Software Development / Re: Problems With Include File Dependencies on: July 02, 2014, 06:56:16 am
Unfortunately, its the way the IDE does it.

For now, this is the way it'll be, I'm sure they have plans to one day incorporate a full recursive scan.
You will, for now, have to ensure appropriate documentation is included in the library, and also in any examples.

I have written an article describing some of the IDE's behaviors ( including this one )  if you are interested. There are also links to some examples where the IDE can break your code because of these changes.
http://arduino.land/FAQ/content/1/3/en/what-does-the-ide-change-in-my-sketch.html
Pages: 1 ... 3 4 [5] 6 7 ... 142