Show Posts
Pages: [1] 2 3 ... 358
1  Using Arduino / Programming Questions / Re: How to read values over serial on: Today at 05:56:44 pm
You get serial by checking Serial.available().

How would you get a button? That may be trickier than just checking a pin, depending on the button but still the principle is the same.

The important part is to not let doing one thing hold everything else up, which is called blocking code.



2  Using Arduino / Programming Questions / Re: Arduino Program Stop Working after 1 sec on: Today at 04:03:18 pm
I thought for sure that a led has a minimum trigger voltage by color and a maximum voltage not much more.
Red led needs > 1V doesn't it?
3  Using Arduino / Programming Questions / Re: How to read values over serial on: Today at 03:59:58 pm
If you map something to degrees and then degrees to servo value not degrees then you set up for 2 round offs.
 
4  Using Arduino / Programming Questions / Re: How to read values over serial on: Today at 03:22:01 pm
The whole page of Nick's blog covers the whole subject.
The second part is state machine showing exactly what you want and it does not need atoi().

When digits are received, as they arrive an unsigned int is 'moved up' 1 digit then the new digit is added.
This is because text numbers arrive high-value to low-value order.
123 is 1x100 + 2x10 + 3. So you read the 2 then x10 and add the 1, then x10 and add the 3 all as they come.

Code:
void processIncomingByte (const byte c)
{
  if (isdigit (c))
  {
    currentValue *= 10;
    currentValue += c - '0';
  }  // end of digit
  else
  {

    // The end of the number signals a state change
    handlePreviousState ();

    // set the new state, if we recognize it
    switch (c)
    {
    case 'R':
      state = GOT_R;
      break;
    case 'S':
      state = GOT_S;
      break;
    case 'G':
      state = GOT_G;
      break;
    default:
      state = NONE;
      break;
    }  // end of switch on incoming byte
  } // end of not digit  
  
}

If all you do is grab the code, you won't learn half of the tutorial there.
if you learn it all, this kind of thing will not be a problem again.

5  Using Arduino / Programming Questions / Re: Arduino Program Stop Working after 1 sec on: Today at 01:39:33 pm
the 4 LEDs are turned on for 500 ms using PMW for a value of 10mv.
the 4 LEDs are turned on for 500 ms using PMW for a value of 250mv.
the 4 LEDs are turned on for 500 ms using PMW for a value of 50mv.
the 4 LEDs are turned on for 500 ms using PMW for a value of 0mv.

Unless you have a circuit to turn PWM into analog, it doesn't work that way.
PWM is full VCC for value/256 time out of each duty cycle.
Default duty cycle is about (just over) 2 ms. PWM 127 should get just over 1 ms ON, 1 ms OFF at 5V pattern.
6  Using Arduino / Programming Questions / Re: How to read values over serial on: Today at 01:28:04 pm
Simple letter + value code is exactly what Nick Gammon addresses with example in this tutorial blog:
 http://gammon.com.au/serial

7  Using Arduino / Programming Questions / Updated SMlib code on: July 30, 2014, 10:24:47 am
I've reached the 9500 char post limit.
GitHub doesn't like XP.
Verizon is taking my web space in 2 months.
I won't be posting updated code though the code is updated.
8  Using Arduino / Programming Questions / Re: Non linear list and sensor values (that are not Integers) on: July 30, 2014, 09:09:21 am
Yeah but still, if the temperature is used to directly determine something else (formula of temperature and constants) then table the something else and save needing those steps and constants as well.

IE, don't table factors if results or partial results are more useful.

PS I could go farther.
Suppose you have 2 factors and a number of constants used in a complex formula, what do you do?
You use a 2 dimensional table as Jiggy-Ninja did. The formula reduces to lookups and interpolation if any is needed.
9  Using Arduino / Programming Questions / Re: How to share code? on: July 30, 2014, 07:54:17 am
I saw your code from github. to make your code easy for beginner like us to use. you need to write simple examples using your code. still, if you think your code is great. write a book about it.

That takes a lot. The book is likely more effort than the code.
But if 2 or more were to collaborate successfully, a group project of shared skills might go easier or devolve into a mess and fights, it's hard to say.  smiley-eek-blue

10  Using Arduino / Programming Questions / Re: Non linear list and sensor values (that are not Integers) on: July 30, 2014, 06:28:57 am
You can make a linear based table easily enough. Question is would you have room to store it? And does it need to cover how many kinds of refrigerant?

You can get more flash and RAM with a bigger chip. You can build or buy that. Just keep in mind that you're not stuck with one set of limits when you're looking at approaches to solutions. A $7 (if you're just buying one) ATmega1284P chip has 128K flash, 16K RAM and 2 serial ports. A less than $3 ATmega328P (the UNO chip) has 32K flash, 2K RAM and 1 serial port (software serial can be used for more). Both are covered in build your own tutorials, both are covered in kits and sold on assembled and tested boards. And those are just 2 choices out of a very wide range.

What you explore for techniques now can likely be taken farther through easy choices down the road.
So definitely explore before you choose!

You can use an external flash chip or EEPROM as easily as an SD adapter and get away with a smaller AVR.

Half a second is a long time to Arduino, 8 million CPU cycles. But with the wrong code that spends most of the time waiting for things to finish it can be hardly any time at all, 500 milliseconds to fritter and waste. The right code can run a small circus of devices smoothly simultaneously.

Lookup tables are a great way to save computation. If the pressure you look up in the table is going to be used to calculate PWM values to run a servo or motor and nothing else, store those instead of pressure! PWM is only 8 bits!

Here's an example of using a lookup table of integer sine x 10000 (Y = radius x value / 10000) vs using sin() to generate floats. It does the process many times for each (adding every value and printing it to keep the compiler from optimizing the calculations away and providing result checks in the process) to take long enough to notice  smiley-mr-green then shows that calculating just the sines with floats takes 100x as long. Tables work.
It runs on a bare UNO just fine and it is not the fastest or most efficient code for what it does.

Code:
// Getting 16 bit unsigned vars in and out of PROGMEM (flash)
// Written for public domain by GoForSmoke 2014


// this program also compares the time to do table lookup vs using floats and sin()
// sorry about having to add and print the totals but otherwise the compiler optimizes the test to nothing

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

const word PROGMEM PROGTBL[ 91 ] = { // sine * 10000 by degree 0 to 90
  0U,  175U,  349U,  523U,  698U,  872U, 1045U, 1219U, 1392U, 1564U,
  1736U, 1908U, 2079U, 2250U, 2419U, 2588U, 2756U, 2924U, 3090U, 3256U,
  3420U, 3584U, 3746U, 3907U, 4067U, 4226U, 4384U, 4540U, 4695U, 4848U,
  5000U, 5150U, 5299U, 5446U, 5592U, 5736U, 5878U, 6018U, 6157U, 6293U,
  6428U, 6561U, 6691U, 6820U, 6947U, 7071U, 7193U, 7314U, 7431U, 7547U,
  7660U, 7771U, 7880U, 7986U, 8090U, 8192U, 8290U, 8387U, 8480U, 8572U,
  8660U, 8746U, 8829U, 8910U, 8988U, 9063U, 9135U, 9205U, 9272U, 9336U,
  9397U, 9455U, 9511U, 9563U, 9613U, 9659U, 9703U, 9744U, 9781U, 9816U,
  9848U, 9877U, 9903U, 9925U, 9945U, 9962U, 9976U, 9986U, 9994U, 9998U,
  10000U, };

void setup( void )
{
  Serial.begin( 115200 );

  char  formatted[ 8 ];

  word  W;

  PGM_P  Addr; // define a const prog_char pointer named Addr
  Addr = (const prog_char *) PROGTBL; // cast the table address to fit Addr

  for ( byte i = 0; i < 91; i++ )
  {
    W = pgm_read_word( Addr + 2 * i ); // read unsigned 16 bits at Addr
    sprintf( formatted, "%6u", W );
    Serial.print( formatted );
    if ( i < 90 ) Serial.print( ", " );
    if (( i % 5 ) == 4 ) Serial.println( );
  }
  Serial.println( "\n\n Timed test" );
 
  Serial.flush(); // getting serial interrupts out before time test
  delay( 1000 );  // making sure of it

  unsigned long accum = 0UL; // to make the compiler use all the lines below

  unsigned long mics = micros();
  for ( byte i = 0; i < 91; i++ )
  {
    W = pgm_read_word( Addr + i * 2 );
    accum += (unsigned long) W;
  }
  unsigned long tookMics = micros() - mics;
 
  Serial.print( "\n accum = " );
  Serial.print( accum );
  Serial.print( " took " );
  Serial.print( tookMics );
  Serial.print( " micros to add up" );
  Serial.println( );
 
  Serial.flush(); // getting serial interrupts out before time test
  delay( 1000 );  // making sure of it
 
  float fAccum = 0.0;
 
  mics = micros();
  float T = sin( M_PI * 30.0 / 180.0 );
  for ( byte i = 0; i < 91; i++ )
  {
    T = sin( M_PI * (float) i / 180.0 );
    fAccum += T;
  }
  tookMics = micros() - mics;
 
  Serial.print( "\n float accum = " );
  Serial.print( fAccum );
  Serial.print( " took " );
  Serial.print( tookMics );
  Serial.print( " micros to add up" );
  Serial.println( );
 
}

void loop( void )
{
}

11  Using Arduino / Programming Questions / Re: Non linear list and sensor values (that are not Integers) on: July 29, 2014, 11:14:17 pm
If your table goes by linear temperature, you only need to store pressure for each and use temperature to get the index.
You may have room for 1/10th degree steps for integer values in Pa instead of kPa.

Another route from flash is to use data store in an SD card. It's slower but still fast and the limits are sky high.


12  Using Arduino / Project Guidance / Re: program to find rpm on: July 29, 2014, 05:59:57 pm
Bulldog has the right of it. He keeps his units straight.

To scale from revs per second to revs per minute:
10 revs / 1 second =  ( 10 revs / 1 second ) * ( 60 seconds / 1 minute )
10 revs / 1 second = ( 10 revs * 60 seconds ) / ( 1 second * 1 minute ) = 600 revs / minute

To scale from revs per Y ms
X revs / Y ms = ( X revs / Y ms ) * ( 60000 ms / 1 minute )
X revs / Y ms = ( X revs * 60000 ms ) / ( Y ms * minute ) = X * ( 60000 ms / Y ms )  revs / minute
When Y = 5000, rpm = X * 12

13  Using Arduino / Programming Questions / Re: How to find number of "rows" in an array of strings? on: July 29, 2014, 10:27:33 am
No problem, but I am still intrigued by the dynamic creation of the strings/Strings and not knowing how many there are.  Either the array will need to be declared large enough to hold the maximum number of strings to be created, in which case the sizeof() trick will not work, or declared just big enough to hold the strings in which case you will know how many of them there are.

You can do buffer management with C strings but by design, C++ Strings are a can of worms.
14  Using Arduino / Programming Questions / Re: How to find number of "rows" in an array of strings? on: July 29, 2014, 10:22:47 am
Quote
LOL, what Arduino are you planning to run dynamic String arrays on?

you know you kinda come off as arrogant when you do this, lets say I don't know why running dynamic arrays is bad, instead of telling me why, you laugh in my face. I realize you're tired of answering newbie questions, but you could just ignore the whole thing if you don't want to deal with that. As it is you've contributed little to the whole discussion.

I realize dynamic strings are memory expensive, but it's not my choice to do it, all I needed to know is how to know how many entries there are and econjack has answered that with explicit detail. So thanks to him for answering the question.

Look, a number of people tried to reason with you and I did try an point out the obvious and I did give other information but you stepped on and over every bit of that and not just what I gave.

So fine, I'm looking at someone who knows better while being unable to understand why what he is trying Doesn't Work even as he misses and rejects true answers over and over on a quest to even more and bigger problems then get this...

I'm supposed to be arrogant for laughing about the futility that this person Insists Upon!

You blew through so much, you shouldn't toss the arrogant label much less play victim.
15  Using Arduino / Project Guidance / Re: Arduino UNO Pinout Diagram on: July 29, 2014, 07:32:48 am
MUCH Appreciated!
Karma to you! Perhaps others will follow suit.
Pages: [1] 2 3 ... 358