Show Posts
Pages: 1 ... 157 158 [159] 160 161 ... 361
2371  Using Arduino / Project Guidance / Re: Algorithm on: May 10, 2013, 09:11:13 pm
I can forsee some Help! Help! moments in my future.

I was studying Gammon's blink code and suddenly what came to me was, why are the initializations outside of the setup function?

Functions are things that happen during run time.
Defines and includes are pre-compiled.

Variable declarations may be made pre-compiled or during run time and may be stored on the heap or on the stack. What code can access a variable depends on scope and pre-compiled variables are usually global in scope with exceptions requiring extra syntax.




2372  Using Arduino / Project Guidance / Re: Algorithm on: May 10, 2013, 04:38:09 pm
Quote
Does anyone bother with creating an Algorithm before writing their code?
Is there another way? smiley-wink

Yes. I've been seeing it since I got here. It usually starts with words like "Help! Help!".

2373  Using Arduino / Project Guidance / Re: Algorithm on: May 10, 2013, 03:27:43 pm
Might I suggest that you learn the Blink Without Delay example as a way to make things happen on a time basis? Then look up 'finite state engine' on the site search here as the other part to real-time coding?

Or --- both lessons combined with clear and simple explanations to go with the code?
http://www.gammon.com.au/forum/?id=11411

Quote
But what if you want to blink the two LEDs at different rates? Like, once a second for LED 1 and twice a second for LED 2?

This is where the delay function doesn't really help.

Let's look at an analogy. Say you want to cook breakfast. You need to cook:


    Coffee - takes 1 minute
    Bacon - takes 2 minutes
    Eggs - takes 3 minutes




Now a seasoned cook would NOT do this:


    Put coffee on. Stare at watch until 1 minute has elapsed. Pour coffee.
    Cook bacon. Stare at watch until 2 minutes have elapsed. Serve bacon.
    Fry eggs. Stare at watch until 3 minutes have elapsed. Serve eggs.



The flaw in this is that whichever way you do it, something is going to be cooked too early (and get cold).

In computer terminology this is blocking. That is, you don't do anything else until the one task at hand is over.

What you are likely to do is this:


    Start frying eggs. Look at watch and note the time.
    Glance at watch from time to time. When one minute is up then ...
    Start cooking bacon. Look at watch and note the time.
    Glance at watch from time to time. When another minute is up then ...
    Put coffee on. Look at watch and note the time.
    When 3 minutes are up, everything is cooked. Serve it all up.



In computer terminology this is non-blocking. That is, keep doing other things while you wait for time to be up.

2374  Using Arduino / Programming Questions / Re: Bluetooth library on: May 10, 2013, 03:11:24 pm
+1, but it's pins not ports. An AVR port is 8 pins together and there are commands to read/write ports.

Post corrected for port correction!

It's just words, we talk about the same thing and know we do, but try an do a forum or net search with the wrong ones!
2375  Using Arduino / Project Guidance / Re: Using Tx and Rx ports with several modules on: May 10, 2013, 03:08:17 pm
I guess a 5V regulator on a heat sink is out of the question? Arduino has one and should be able to run off IIRC 14V through it. Maybe add an inductor to help avoid spikes ruining the regulator?

I got one of the car MP3 players that plugs in the cig lighter socket and plays through the car radio and PITA problem, car hits a bump and power is interrupted! I guess that's what I get for buying a cheap device.

2376  Using Arduino / Project Guidance / Re: Algorithm on: May 10, 2013, 03:01:08 pm
Does anyone bother with creating an Algorithm before writing their code?

If I need any I do. I might even write pseudocode as comments and use that as a guide to write my working code. But I draw the line at flowcharts. I don't do flowcharts.

I hope that any of this can help you.

I googled 'DDS signal generator avr' and got a load of hits, some like this up to 8 MHz one:
http://www.scienceprog.com/avr-dds-signal-generator-v20/

And there are modules capable of far higher speed for good prices:
http://www.lctech-inc.com/Hardware/Detail.aspx?id=38fd4f22-f54c-4b31-9c8e-d0aaa7a0931d
I know that company through deals I get there. You can ask for docs in English, they have even provided me with schematics when asked before.


2377  Using Arduino / Programming Questions / Re: Bluetooth library on: May 10, 2013, 02:15:43 pm
I recommend using the SoftwareSerial library.

This way, you can use any other 2 ports on the arduino (not necessarily hardware TX and RX), which also allows you to debug your program using the USB-to-serial interface, which cannot be done if the Bluetooth module is using the hardware serial pins.

+1, but it's pins not ports. An AVR port is 8 pins together and there are commands to read/write ports.

I knew someone who used SoftSerial with XBee and the ability to see debug messages made the difference in getting it all to work.

Question for Hazard's Mind: isn't there protocol to get the modules to identify each other and do other network-y things and how much -has- to be done?
2378  Using Arduino / Programming Questions / Re: Start (Momentary) Button, safety on: May 10, 2013, 02:06:03 pm
I like debounce to ensure deliberate contact. It's a little extra code is all. Your mileage may vary.

Most of the time I don't get bounce anyway but sometimes I wonder if there's an app just waiting for that switch.

It would still be better to measure the water level in the pot directly.

2379  Using Arduino / Programming Questions / Re: Start (Momentary) Button, safety on: May 10, 2013, 08:58:40 am
At the end of setup (a bit of pseudocode here)

while (start_button_ not_ pressed());

I made it a function call so you can add debounce and be sure against falsing.
You -can- just have a digitalRead(pin) if you don't believe in Murphy.



If i put a function inside "while" I get error on compile.
PWM_distiller_working_noLCD_v2.ino: In function 'void setup()':
PWM_distiller_working_noLCD_v2:25: error: could not convert 'buttonpress()' to 'bool'

The while line in my setup() is then highlighted.

Code:
#include "TimerOne.h"


unsigned long time;
//unsigned long Cutout = 16800000;
unsigned long Cutout = 30000;
unsigned long Fanon = 20000;
unsigned long Fanoff = 40000;

int fanrelay =  13;
int duty = 614;
int buttonPin = 12;
int buttonState = 0;





void setup()
{
  pinMode(9, OUTPUT);
  pinMode(fanrelay, OUTPUT);
  Timer1.initialize(500000);  // initialize timer1, and set a 1 second period
  pinMode(buttonPin, INPUT);
  while (buttonpress());
  }
 
void loop()
 {
  if (time >= Fanon)
  digitalWrite(fanrelay, HIGH);
  time = millis();
  Timer1.pwm(9,duty);                // setup pwm on pin 9, 0-1024 max resolution /100*% to get a % duty eg; 614.4 is 60%
  if (time >= Cutout)
  shutdown();
  }
 
void shutdown()
{
  Timer1.disablePwm(9);
  while(1);  // endless loop
}

void buttonpress()
{
 
}

When I debounce a button/switch I watch for many reads in a row all the same for maybe 5 to 10 ms. There are others who do a more thorough job that works on switches mine might and not and some who check a button and if it's the same 20 to 100 ms later then that's good enough.
Debounce doesn't have to be a function, I just wasn't ready to trot out a lot of code and explanation.

For this, consider the pin to be INPUT HIGH and pressing the button grounds the pin to LOW.
Not tested, not compiled, it's just easier to write this than make tech sense in English.

Code:
byte buttonNotPressed() // returns the opposite of last debounced button state
{
  static unsigned long lastChangeMillis = 0UL;
  static lastPinState = HIGH;  // button not pressed, pullup not grounded

  int pinState = digitalRead( buttonPin ); // most of these int used should be byte, save RAM

  if ( pinState != lastPinState ) // pin state has changed, set debounce start time
  {
    lastChangeMillis = millis();
  }
  else // pin state has not changed, see if it has been so for 10 ms
  {
    if ( millis() - lastChangeMillis >= 10 ) // debounced at last!
    {
      buttonState = pinState;
    }                                       
  }
  lastPinState = pinState;

  return buttonState; // will be 1=TRUE for button NOT pressed
}


2 if's,  some variables and a digital read. not so hard?

Code:
  while ( buttonNotPressed() ); // this will wait for the button to be pressed and debounced

The function could fit into that while loop but as it is it could also be put into loop() as an if() that does not block other code from running in between checks on the button. This way the while() at the end of setup is the only blocking line, and you -want- it to block!




2380  Using Arduino / Programming Questions / Re: [help,urgently] arduio programing problem on: May 10, 2013, 06:41:43 am
A keyboard is from computer , that was a exam , and tomorrow i will do it for my examination .

You have much bigger problems than that exam.
1 day before the exam and you start to ask for help. You were sick before the project was assigned? Consider before you answer that no one here can save your exam. It is tomorrow already and you have a lifetime of tomorrows coming.

You don't have to 'look good' so why not 'be real' just because being real gives you a real chance to not have this happen to you again. Yah, it comes down to you and none of us here. Please help yourself, get the incomplete and come back ready to work your brains hard. It will do you a lot of good and when you learn, you 'look good'.

2381  Using Arduino / Programming Questions / Re: writing into pin - it takes effect after several attempts? on: May 10, 2013, 06:16:28 am
Using code to change a pin so that other code can read the pin (even if you did ground pin 2 through a big resistor to make it stop floating) is ... redundant to say the least.

AVR I/O pins used for INPUT have an option to turn power on through a resistor (20k to 50k) so if not grounded it reads HIGH and if grounded it reads LOW. Use this and you can signal by grounding (button down) or not (button up). Since the power flows through the pullup resistor there will never be too much used, you can't burn it out by grounding like you could with OUTPUT straight to ground. Using pullups can save you lots of resistors especially when you multiplex pins to read many buttons.

2 ms is a long time to Arduino. 32000 cycles long where short commands run in 1 or 2 cycles. How about that?

To make things happen on time (at least very close depending on your code, maybe 1 usec close) there is the BlinkWithoutDelay example showing how. But... I have problems with the example provided as it has some simple errors that let it work but teach bad habits.

I provide a cleaned up version. Use the version you want.
If you take the comments out and don't count braces, mine is 12 lines of code to understand. With comments, a small book.

Code:
/* Blink without Delay -- with minor fixes by GFS

  Turns on and off a light emitting diode(LED) connected to a digital 
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.
 
 The circuit:
 * LED attached from pin 13 to ground.
 * Note: on most Arduinos, there is already an LED on the board
 that's attached to pin 13, so no hardware is needed for this example.
 
 
 created 2005
 by David A. Mellis
 modified 8 Feb 2010
 by Paul Stoffregen
 
 This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
 
 GFS fixes and modifications -- May 5 2013:
 . changed time variables to be ALL unsigned longs, as they should be.
 . added UL to numbers being assigned to unsigned longs as should be.
 . changed the variable name 'interval' to 'blinkTime' as interval is now a
 word used by the IDE, it shows up red (like the word 'if') instead of black.
 . changed the if-else logic to change the ledState variable to 1 line XOR logic.
 . added comments about adding more tasks to the sketch.
 
 */

// constants won't change. Used here to
// set pin numbers:
const byte ledPin =  13;      // the number of the LED pin

// Variables will change:
byte ledState = LOW;             // ledState used to set the LED

unsigned long previousMillis = 0UL;  // will store last time LED was updated

unsigned long blinkTime = 1000UL;  // interval at which to blink (milliseconds)

void setup()
{
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);     
}

void loop()
{
  unsigned long currentMillis = millis();
 
  // here is where you'd put code that needs to be running all the time.
 
  // GFS adds -- you see the if() { } block below? You can add more blocks
  // whether if() or switch-case or whatever to do other tasks and as long
  // as they run quick without delays or prolonged loops, your sketch will
  // be responsive as if everything runs at the same time.
  // just as the blink runs on time, another task can run when a button or
  // sensor or serial data comes in or changes.
  // simple commands run in less than a millionth of a second so you can pack
  // a good bit of process into a block and still run quick. analog read takes
  // longer, about 9 per millisecond so it's best not to do a bunch of those
  // in a row but instead 1 analog read per time through loop() so other tasks
  // can get a chance in between analog reads.
  // it's also good to avoid using floating-point as that is slooowww and avoid
  // using C++ Strings as they mess with your RAM and suck up CPU cycles doing it.

  // Back to the original program:
  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  if(currentMillis - previousMillis >= blinkTime) // is it time to change the led?
  {
    // save the last time you blinked the LED
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    ledState = ledState ^ 1; // ^ is logical XOR, true if the values are different
    // using logic operations can save a lot of tedious, pain to debug if's

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

2382  Using Arduino / Programming Questions / Re: Start (Momentary) Button, safety on: May 10, 2013, 05:37:13 am
At the end of setup (a bit of pseudocode here)

while (start_button_ not_ pressed());

I made it a function call so you can add debounce and be sure against falsing.
You -can- just have a digitalRead(pin) if you don't believe in Murphy.

2383  Using Arduino / Programming Questions / Re: Compile errors with Progmem on: May 10, 2013, 05:30:20 am
A little surfing got me here:
http://www.controllerprojects.com/2011/05/23/determining-sram-usage-on-arduino/

And I grabbed the function:
Code:
int freeRam () {
  extern int __heap_start, *__brkval;
  int v;
  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}

After yet another try getting #ifdef to work (this time using 1.03, last time using 0023) I said screw it and made two versions of a sketch to check what I can:

This one uses PROGMEM and reports 1811 bytes free on my UNO using IDE 1.03.
Code:

#include <avr/io.h>
#include <avr/pgmspace.h>

const char PROGMEM textTable[] = { // all this text stored in flash
  "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\0"
};

PGM_P tT; // will point into namesTable

/*
const char textTable[] = { // all this text goes into RAM
  "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\0"
};
*/

void  printProgstring( PGM_P FM )
{
  char fb;
  do
  {
    fb = pgm_read_byte( FM++ );   
    if ( fb )  Serial.print( fb );
  }
  while ( fb );
}


int freeRam () {
  extern int __heap_start, *__brkval;
  int v;
  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}

void setup(void)
{
  Serial.begin( 9600 );
  Serial.print( "\nRAMSIZE_P Free RAM = " );
  Serial.print( freeRam());
  Serial.println( "\n" );

  tT = textTable;
  printProgstring( tT );
//    Serial.println( textTable );   
    Serial.println( );   
}

void loop(void)
{
}

This one uses RAM and reports 1523 bytes free on my UNO using IDE 1.03.
Code:
#include <avr/io.h>
#include <avr/pgmspace.h>

/*
const char PROGMEM textTable[] = { // all this text stored in flash
  "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\0"
};

PGM_P tT; // will point into namesTable
*/

const char textTable[] = { // all this text goes into RAM
  "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\0"
};

/*
void  printProgstring( PGM_P FM )
{
  byte fb;
  do
  {
    fb = pgm_read_byte( FM++ );   
    if ( fb )  Serial.print( fb );
  }
  while ( fb );
}
*/

int freeRam () {
  extern int __heap_start, *__brkval;
  int v;
  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}

void setup(void)
{
  Serial.begin( 9600 );
  Serial.print( "\nRAMSIZE_R Free RAM = " );
  Serial.print( freeRam());
  Serial.println( "\n" );

//  tT = textTable;
//    printProgstring( tT );
    Serial.println( textTable );   
    Serial.println( );   
}

void loop(void)
{
}

2384  Community / Bar Sport / Re: Arduino based replicator on: May 10, 2013, 02:13:32 am
You wrote a bit more than that and it was aimed politically.

As written the new laws can't succeed, that seems evident. But the situation shaped by the old laws which still dominates has grown into a monster of enormous proportions, a capitalistic cancer of sorts (as opposed to capitalistic bone, muscle, and brains) with whole industries feeding off the easy money side and fighting like hell to keep their ride going.

The new law is the crippled child of people trying to fix the old system and people who did and still do everything they can to kill it and get back to something that can be used to bring about the soul-ugly form of Social Darwinism that was practiced over 100 years ago and brought about such things as Company Towns, child labor, crushing poverty of whole segments of society in ways that school children are only shown the edges of. You know, the kind of things that Ayn Rand fans salivate over.

But the new law shows that change -can- happen even after decades of NO.

And for a while we're going to have both new and old ways and pay the price of both. You can't get off your butt without some effort. Too bad the same ones screaming about the cost of such effort being the same ones undermining it with loads of support from a large cigarette smoking bad diet eating high cancer risk morons in government supplied powered wheelchairs living on social security and/or welfare and medicaid expecting full treatment for their future health needs even as they protest "big government" and swap lies about it. 

Yah, let's keep people away from preventative medicine because of the cost and then when they get desperately sick there will be a choice of giving them operations at astronomical prices or dumping them somewhere to die while acting sorry there's no money. But perhaps a family member can sell a kidney to someone who needs one and has money to pay and eventually that silly law against euthanasia can be struck down and we'll have some -real- progress in solving the population problem.
Does that sound like a plan? Because from what I see that IS the direction a lot of people are pushing and the Tea Party CHEERS at the mention of letting people who don't have health insurance die is a f__king good indicator of just that.

So lets see what it takes to break the grip of the NO people and get something that can work going?
 
I can't see how the pay-as-you-go right wing fundamentalists can accept the concept of insurance at all anyway. One cancer operation costs more than 'reasonable' insurance payments over 20 years even if the person with the cancer never took a penny out the whole time. That means that, oh gasp horrors, SOMEONE ELSE PAYS and maybe there won't be money for their cancer or dementia or other care. And that's just the start. What about the people that pay in all their life and die before they get sick? What about children born with illness or defects? Any Rand would say either kill them or let them die as long as it is THEM.

Yeah I 'get political' as a reaction to the crap going on. The brownshirts are marching and I certainly do speak out against them. I am not sheeple. I am not a cow. I am not a facist. I can think past my own immediate interests and see that that crowd certainly do not. You LIKE where they're headed then go ahead and support them but no way I sit still and shut up over it.
2385  Using Arduino / Programming Questions / Re: Compile errors with Progmem on: May 09, 2013, 04:40:25 pm
I have some confusion here of my own. The below is speculation.

Some thinking and I feel that if you access the data with a program space pointer then it should be program space data. But that leaves me wondering how come my code seems to work when it violates at least one rule in that article.

Quote
dataType PROGMEM variableName[] = {};   // not this one

Some answer to that is here in the pgmspace library doc.
Code:
Typedefs
typedef void PROGMEM prog_void
typedef char PROGMEM prog_char
typedef unsigned char PROGMEM prog_uchar
typedef int8_t PROGMEM prog_int8_t
typedef uint8_t PROGMEM prog_uint8_t
typedef int16_t PROGMEM prog_int16_t
typedef uint16_t PROGMEM prog_uint16_t
typedef int32_t PROGMEM prog_int32_t
typedef uint32_t PROGMEM prog_uint32_t
typedef int64_t PROGMEM prog_int64_t
typedef uint64_t PROGMEM prog_uint64_t

typedef dataType attribute alias
bang?

Note that PROGMEM appears to be an attribute like private, public or protected are. It is then that I see "prog_int8_t" is the same as "int8_t PROGMEM". If that is right then there's no need to use prog_int8_t and PROGMEM together, is there?

 
Pages: 1 ... 157 158 [159] 160 161 ... 361