Show Posts
Pages: 1 ... 155 156 [157] 158 159 ... 361
2341  Development / Suggestions for the Arduino Project / Re: Is there a project to improve/replace the official examples? on: May 15, 2013, 08:33:20 am
My next step was to try and address organizing examples and knowledge as it has piled up and is frustrating trying to find solutions. We need a library card catalog equivalent or better.

But first, the examples. I made some changes to BlinkWithoutDelay since I send so many new people to that. BTW, there's 12 actual lines of code in this one.

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);
  }
}
2342  Using Arduino / Programming Questions / Re: Issues with Pass Array of Structures to Library by Reference on: May 15, 2013, 08:26:05 am
Also, always keep in mind that an array name, such as DefMenu, is exactly the same as the base address (i.e., the lvalue) of the array.

With a 2D array it is a bit more complicated to get at, but the address is there.

Quote
Therefore, if I want to march through an array using a for loop and want to look at things along the way, I need to use the address of a particular element of the array. That's why the address-of operator (&) is used in the call to your test() method.

To address an element I add the number of elements into the array to the base. Usually I assign that to a pointer but last night when I wrote that post and now I'm too tired to lay it all out. If I have something that I can & I don't need to compute an address.

Quote
Also, the indirection operator (i.e., *) is always used to fetch the contents of what it stored at a given memory address (i.e., the rvalue).

int A[ 10 ] = { 0,1,2,3,4,5,6,7,8,9 };

int *B = A + 5; // B is an int pointer now pointing at A[5]

int C = *B + 10; // C now == 15

*B = 21; // *B is A[5], the * makes the pointer act as the variable, A[5] now == 21

Here is a nice tutorial on pointers, better than I can write and more than I can post.
http://pw1.netcom.com/~tjensen/ptr/pointers.htm
2343  Using Arduino / Programming Questions / Re: Issues with Pass Array of Structures to Library by Reference on: May 15, 2013, 01:33:49 am
I am humbled to be back at the well so soon but I have spent the better part of the day fighting with pointers.   I am able to pass a pointer for one row of my structure but not for the entire structure.   I recognize that I am doing something stupid and would appreciate a [gentle] slap on the head...here is my code

Gently now, C pointers are just addresses with data type info. The details you write on a card until you get used to them again. Thank someone you're not trying to deal with trig and log identities, huh?

I can make a pointer to int and set it to the base of an int array of known dimensions. If I know how the array is arranged then I can reach any spot by adding a number to that base address.
Simple example.
int A[ 10 ] = { 0,1,2,3,4,5,6,7,8,9 };

the base pointer is A, the name of every 1 dimension array is a pointer
if I want the 1st element then

int X = *( A + 0 );

the * can be thought of as 'at'
the parens are to evaluate A + 0 as an address before using 'at' to reference it

in a loop or operation or arg passed to a function, ( A + i ) works as well.

or I can go A[ 0 ] or A[ i ] after passing A as the base address.

Multiple dimensions.
int A[][], the name A is a pointer to a pointer. But the entire array is still ints, one after the other 2 bytes wide so a simple int pointer can address the whole shebang and offsets are not really that hard to work out.

What goes for ints goes for structs.

In practice, use the basics to keep from getting completely lost and draw the occasional map of your data. I hope this helps, I'm sure it's stuff you know somewhere.

The first code I wrote that ran on a computer was Fortran 4 on punch cards in 1975. I'd hate to try Fortran today.
2344  Using Arduino / Programming Questions / Re: Averaging analog inputs without delay() help needed on: May 15, 2013, 01:07:04 am
I think I have to keep the two lines reading "Battery voltage is" though since I want two lines printed with different information right?

I dunno what you want there. I might make one longer line saying like
Battery voltage is LOW at 2.9V.

Quote
I attempted to change what I have as the battery reading averaging code to what you suggested with no luck.

That "might as well be" was not a suggestion to do that. It was an observation that you are using a loop to add the same value to an accumulated value 50 times. The averaging code is something else you should get help with (and a book) as it's been so long since I messed with statistics (1977) that I can't even remember the names of the methods.

Quote
Quote
I hope you like the state machine and see how simple one can be. If need be you can run code on several state variables but take care that they don't step on each other.
Yeah, the state machine is very cool, it was kind of hard to understand at first, but after looking it over a few times and changing different things I am starting to understand it better. Very powerful.

Beginner programming teaches a start - middle - end approach which fits a lot of things done with computers. But real time code only ends when the user chooses, power runs out or you hit a nasty bug. Otherwise it goes around and around keeping track of what went on to know what to do with what goes on.

Quote
I must say that it is exciting to see how many different ways there are to do similar things in C. I have never messed with any kind of programming until I started with this project and it really plows me away with how it can be so complex, yet simple at the same time.

Keep it simple and use the Feynmann learning technique when something looks hard.
https://www.youtube.com/watch?v=FrNqSLPaZLc
You can probably find more on that in a book in one of those places they keep lots of books.  smiley-grin

I grew up on pencil and paper. It slows you down, gives you time to think and if you're good it gives you a record or picture of what you have done or want to do. Thinking at the keyboard I find rushes things and you end up with something you can't see the whole of at once.

Quote
I have always worked with the hardware side of things in the past and it is neat to cross over to the software side of things now too.

It just gets better and learning cross-discipline lets you talk better with that crowd that can't change a lightbulb because it's a hardware problem (old joke, how many programmers does it take to....).
2345  Using Arduino / Project Guidance / Re: comparing an array of inputs on: May 15, 2013, 12:39:01 am
I made a mistake earlier, I actually need 16 inputs.  can I still use the byte format?  I am using a mega2560.  I was hoping I could Just digital read all the inputs, subtract the two arrays, and then if the value was >0 increment a value.  Im not a great programmer, and my education is in electronics not programming.  Its just a final project before I graduate.  I got everything done but the arrays.  I seen this code and thought it would be good to use but can't get it to work.  This is what I was Trying.  No need to laugh when you see it  smiley-grin

Code:
void sensorControlLoop(){
  
  unsigned long sensMillis = millis();
  long prevSensMillis = 0;
  long sensInterval = 1000;
  int n;
    int pinArray1[16];
    int pinArray2[16];
    pinArray1[0] = digitalRead(31);
    pinArray1[1] = digitalRead(32);
    pinArray1[2] = digitalRead(33);
    pinArray1[3] = digitalRead(34);
    pinArray1[4] = digitalRead(35);
    pinArray1[5] = digitalRead(36);
    pinArray1[6] = digitalRead(37);
    pinArray1[7] = digitalRead(38);
    pinArray1[8] = digitalRead(39);
    pinArray1[9] = digitalRead(40);
    pinArray1[10] = digitalRead(41);
    pinArray1[11] = digitalRead(42);
    pinArray1[12] = digitalRead(43);
    pinArray1[13] = digitalRead(44);
    pinArray1[14] = digitalRead(45);
    pinArray1[15] = digitalRead(46);
    
    if(sensMillis - prevSensMillis > sensInterval){
      pinArray2[0] = digitalRead(31);
      pinArray2[1] = digitalRead(32);
      pinArray2[2] = digitalRead(33);
      pinArray2[3] = digitalRead(34);
      pinArray2[4] = digitalRead(35);
      pinArray2[5] = digitalRead(36);
      pinArray2[6] = digitalRead(37);
      pinArray2[7] = digitalRead(38);
      pinArray2[8] = digitalRead(39);
      pinArray2[9] = digitalRead(40);
      pinArray2[10] = digitalRead(41);
      pinArray2[11] = digitalRead(42);
      pinArray2[12] = digitalRead(43);
      pinArray2[13] = digitalRead(44);
      pinArray2[14] = digitalRead(45);
      pinArray2[15] = digitalRead(46);
      
      if (array_cmp(pinArray1, pinArray2, 16, 16) == false){
        ++alarmCount;
        if(alarmCount==10){
          digitalWrite(alarmPin, HIGH);
         }
      }
      else{
        alarmCount = 0;
       }    
    }
}
 boolean array_cmp(int *pinArray1, int *pinArray2, int len_a, int len_b){
      int n;

      // if their lengths are different, return false
      if (len_a != len_b) return false;

      // test each element to be the same. if not, return false
      for (n=0;n<len_a;n++) if (pinArray1[n]!=pinArray2[n]) return false;

      //ok, if we have not returned yet, they are equal :)
      return true;
}void sensorControlLoop(){
  
  unsigned long sensMillis = millis();
  long prevSensMillis = 0;
  long sensInterval = 1000;
  int n;
    int pinArray1[16];
    int pinArray2[16];
    pinArray1[0] = digitalRead(31);
    pinArray1[1] = digitalRead(32);
    pinArray1[2] = digitalRead(33);
    pinArray1[3] = digitalRead(34);
    pinArray1[4] = digitalRead(35);
    pinArray1[5] = digitalRead(36);
    pinArray1[6] = digitalRead(37);
    pinArray1[7] = digitalRead(38);
    pinArray1[8] = digitalRead(39);
    pinArray1[9] = digitalRead(40);
    pinArray1[10] = digitalRead(41);
    pinArray1[11] = digitalRead(42);
    pinArray1[12] = digitalRead(43);
    pinArray1[13] = digitalRead(44);
    pinArray1[14] = digitalRead(45);
    pinArray1[15] = digitalRead(46);
    
    if(sensMillis - prevSensMillis > sensInterval){
      pinArray2[0] = digitalRead(31);
      pinArray2[1] = digitalRead(32);
      pinArray2[2] = digitalRead(33);
      pinArray2[3] = digitalRead(34);
      pinArray2[4] = digitalRead(35);
      pinArray2[5] = digitalRead(36);
      pinArray2[6] = digitalRead(37);
      pinArray2[7] = digitalRead(38);
      pinArray2[8] = digitalRead(39);
      pinArray2[9] = digitalRead(40);
      pinArray2[10] = digitalRead(41);
      pinArray2[11] = digitalRead(42);
      pinArray2[12] = digitalRead(43);
      pinArray2[13] = digitalRead(44);
      pinArray2[14] = digitalRead(45);
      pinArray2[15] = digitalRead(46);
      
      if (array_cmp(pinArray1, pinArray2, 16, 16) == false){
        ++alarmCount;
        if(alarmCount==10){
          digitalWrite(alarmPin, HIGH);
         }
      }
      else{
        alarmCount = 0;
       }    
    }
}
 boolean array_cmp(int *pinArray1, int *pinArray2, int len_a, int len_b){
      int n;

      // if their lengths are different, return false
      if (len_a != len_b) return false;

      // test each element to be the same. if not, return false
      for (n=0;n<len_a;n++) if (pinArray1[n]!=pinArray2[n]) return false;

      //ok, if we have not returned yet, they are equal :)
      return true;
}

This is the reference Code I was looking at and the address. http://forum.arduino.cc/index.php/topic,5157.0.html
Code:
boolean array_cmp(int *a, int *b, int len_a, int len_b){
      int n;

      // if their lengths are different, return false
      if (len_a != len_b) return false;

      // test each element to be the same. if not, return false
      for (n=0;n<len_a;n++) if (a[n]!=b[n]) return false;

      //ok, if we have not returned yet, they are equal :)
      return true;
}

int arr_a[] = {1, 4, 7, 1, 3};
      int arr_b[] = {1, 4, 7, 2, 3};

      if (array_cmp(arr_a, arr_b, 5, 5) == true){
            // do this if they are equal
      }else{
            // do this if they are different
      }

If you still have reasonable time then not panicking will serve you.

An unsigned int is 16 bits and with a MEGA2560 you can arrange those as the pins on two Ports and read them in two Port reads.

You can compare 2 different times using XOR of two unsigned ints. Unsigned ints because signed ints use the top bit as sign.

My oh my, you have an array compare function that includes the lengths of the arrays with logic on if they differ? But they should never differ in length, should they?

I see that you get as far as comparing arrays and if they are different, light up a led. Is that it? Is that all you need to do? That would be a blessing, just tell IF there was movement and not actually from where to where!

Without using Port reads this:
Code:
    pinArray1[0] = digitalRead(31);
    pinArray1[1] = digitalRead(32);
    pinArray1[2] = digitalRead(33);
    pinArray1[3] = digitalRead(34);
    pinArray1[4] = digitalRead(35);
    pinArray1[5] = digitalRead(36);
    pinArray1[6] = digitalRead(37);
    pinArray1[7] = digitalRead(38);
    pinArray1[8] = digitalRead(39);
    pinArray1[9] = digitalRead(40);
    pinArray1[10] = digitalRead(41);
    pinArray1[11] = digitalRead(42);
    pinArray1[12] = digitalRead(43);
    pinArray1[13] = digitalRead(44);
    pinArray1[14] = digitalRead(45);
    pinArray1[15] = digitalRead(46);

can change to this:
Code:
// up near the top of the code, declaring 2 global variables
  unsigned int matBits;
  unsigned int lastBits = 0xFFFF; // 0x denotes hexadecimal notation. 0xFFFF is all bits set
....
then we go down into loop() for the reads
....
  for ( byte i = 31; i < 47; i++ )
  {
    bitWrite( matBits, i - 31, digitalRead( i ) );
  }

  if ( matBits != lastBits )
  {
    then light the alarm led or compare the bits to find out which changed, etc
  }

  lastBits = matBits;
  delay( 250 );
} // end of loop()

Magic part. You only read the pins once per time through loop. Then you compare that read to the last time through loop. And maybe you check more often than 4 times a second which the delay( 250 ) is for, so perhaps no delay() and get immediate response to all changes which is better BTW.

Coding loop() to handle what is going on NOW with variables holding states determined in PAST executions of loop() affecting how results from NOW are used is how to code for REAL TIME. In PC code this is often done by placing a while-loop in the main code and it is generally referred to as the MAIN LOOP. I go back to the 70's there but it's probably as old as operating systems.

You are a EE major? Have you gotten to logic gates yet? The software logical operations are equivalent to logic gates. The bitwise logical operations are equivalent to working with arrays of logic gates which actually on the chip level THEY ARE.
2346  Community / Bar Sport / UDOO --- hybrid board does Linux or Android with Arduino on: May 14, 2013, 06:44:38 pm
I'm thinking "send them $5 and maybe get a board a tiny bit sooner than never".

http://www.kickstarter.com/projects/435742530/udoo-android-linux-arduino-in-a-tiny-single-board?ref=card


    Freescale i.MX 6 ARM Cortex-A9 CPU Dua/Quad core 1GHz
    Integrated graphics, each processor provides 3 separated accelerators for 2D, OpenGL® ES2.0 3D and OpenVG™
    Atmel SAM3X8E ARM Cortex-M3 CPU (same as Arduino Due)
    RAM DDR3 1GB
    54 Digital I/O + Analog Input (Arduino-compatible R3 1.0 pinout)
    HDMI and LVDS + Touch (I2C signals)
    Ethernet RJ45 (10/100/1000 MBit)
    WiFi Module
    Mini USB and Mini USB OTG (micro with the final release)
    USB type A (x2) and USB connector (requires a specific wire)
    Analog Audio and Mic
    SATA (Only Quad-Core version)
    Camera connection
    Micro SD (boot device)
    Power Supply (5-12V) and External  Battery connector

2347  Using Arduino / Project Guidance / Re: comparing an array of inputs on: May 14, 2013, 06:37:46 pm
What TH posted only with more details:

You can keep the status of those 8 pins in a bit array, otherwise known as a byte.

You can use bitRead(), bitWrite(), bitSet() and bitClear() to work with the bits and/or the bitwise operators, the shifts and logic operations. All those are found here:
http://arduino.cc/en/Reference/HomePage

If you have a pattern 11111011 that changes to 11110111 then XOR the two and get 00001100 showing the bits that changed (the 1's in the result) and work it from there.

If you arrange which parts of the mat match which bits then bit position might be something you can use in your algorithm, relating spatial movement to bit movement.

Yes it requires knowing or learning bits and bit logic. Is using a variable-per-sensor array going to be easier because you don't have to learn bits? Well there's not all that much to bits and bit logic and by using those you can compare all 8 sensors in one operation while using arrays will require looping and extra variables.

If you are using an UNO then you can read all 8 sensors in 2 direct Port reads (search on Port Manipulation). All UNO Ports have some pins tied up by default. The MEGA2560 has some ports with all 8 pins (bits) open for use, you can read the whole mat in one operation.

Do some studying and ask questions before you choose a path, and you will be better prepared either way.

2348  Using Arduino / Programming Questions / Re: Strange Characters in Serial Monitor on: May 14, 2013, 10:44:35 am
You set the Arduino serial baudrate to 57600. Did you set Serial Monitor to match?
2349  Using Arduino / Programming Questions / Re: Averaging analog inputs without delay() help needed on: May 14, 2013, 10:38:36 am
Quote
Analog read takes about 105 millis which in itself isn't much but -do- you need to check the battery that often?

Uhh...  Are you sure that's right?  I calculate about 104 micros, which is about 3 orders of magnitude different.


Ooops, yes, micros! I forget if the doc says 104 or 105 or how exact that is.

I go now to edit my screwup above.
2350  Development / Suggestions for the Arduino Project / Is there a project to improve/replace the official examples? on: May 14, 2013, 06:46:05 am
I know they work but in many cases they are teaching new people bad habits that come back in Help! Help! threads. The whole communication set is rife with C++ String use. Blink Without Delay has longs mixed with unsigned longs for time work. Throughout, ints are used where bytes or shorts will do.

These are 8 bit machines with very limited RAM. Awareness of that from the start will result in fewer problems and the examples are not helping.

2351  Using Arduino / Audio / Re: Arduino library for WTV020-SD-16P audio module on: May 14, 2013, 12:27:53 am
Hello!
I have a question!
How to manually change modes on vtv020-sd-16p,all ekys are on/off(loop),key 5 group of voice...
Thanks!!!

I want to ask which control mode module do you use?for example ,If you buy 2-wire series mode can not us key 5 group of voice,control mode is fixed when we program main chip,and you need tell supplier what you want,i think they will give you correct control mode module.

Please. On eBay the supplier tells little. This is typical and notice it says "MP3":
http://www.ebay.com/itm/U-disk-audio-player-SD-card-voice-module-MP3-Sound-module-WTV020-SD-16P-Arduino/310629013078?_trksid=p2045573.m2042&_trkparms=aid%3D111000%26algo%3DREC.CURRENT%26ao%3D1%26asc%3D27%26meid%3D7644561902605214505%26pid%3D100033%26prg%3D1011%26rk%3D2%26sd%3D271024886800%26

Nothing about mode. This one also uses mini-SD I don't find easily, nor adapter mini to micro-SD so this module says 'problems' to me.

I am having problems enough with the VS1003 modules I got. Yes there is software for other modules with the same chip that do almost what I want. In the meantime I have someone who wants dozens of Arduino-controlled sound players.

What I find that "works out of the box" is expensive and the rest are take your chances when buying and see how many hours before you get it to work if ever. Does that sound like any way to sell a lot of anything here?


2352  Using Arduino / Audio / Re: Arduino library for WTV020-SD-16P audio module on: May 13, 2013, 10:55:42 pm
Welcome and you are clear to go. Just be careful about the micro SD card you are using. Got a lot of pain in the b... figuring out why mine did not work from the start.

I hope you share on that later in this thread!
Learning traps to avoid after stepping in so many is why I read so many posts!
2353  Using Arduino / Programming Questions / Re: Averaging analog inputs without delay() help needed on: May 13, 2013, 10:30:25 pm
Code:
   int BatFadeRead = 50;                              //Number of readings to average
    int BA(0);                                         //BA = battery average
    for (int i = 0; i< BatFadeRead; i++) BA += BatFade;

That for loop might as well be:
Code:
   BA += BatFade * BatFadeRead;

I don't see where in that code 50 readings are averaged at all.
There are statistics methods that let you keep one running value that each new data point changes but it's been so long ago for me that I can only remember squares being involved.


You can save a bit on parts like this:
Code:
     if (Photodiode > 25)                             //Display Vent LED status in Serial Monitor
        Serial.println("Vent LEDs are ON");
      else
        Serial.println("Vent LEDs are OFF");

by doing like this:
Code:
     Serial.println("Vent LEDs are ");
      if (Photodiode > 25)                             //Display Vent LED status in Serial Monitor
        Serial.println("ON");
      else
        Serial.println("OFF");

Go over conditions and often you will find prints or commands that are common and can be moved above or below the block they are in. Just checking your code over and working out what can and can't be done and how will not only get you to improve your code but your own code skills as well.
The more aggressive you get with your code, the tighter it will get and the better you will do next time, etc. I've seen Comp-Sci Masters (as in degree holders) that hadn't learned that lesson and High School students that had... back in the 80's! It's a crazy world.

Quote
GoForSmoke, Thanks for your input on that too. I'm actually incorporating most of that code you posted into a different project and it is proving to be very helpful!

I hope you like the state machine and see how simple one can be. If need be you can run code on several state variables but take care that they don't step on each other.

For responsive code you want to make sure that loop() runs fast, always. One thing you do in this sketch is an analog read every time through loop. Analog read takes about 105 millis (edit: MICROS, but still 100 micros is a good while in AVR-land) which in itself isn't much but -do- you need to check the battery that often?

Quote
Edit: Anyone have any ideas why this thread has a crazy amount of views? Glitch? I cant see there really being that many views.

There's been some forum admin work going on. OTOH maybe a lot of people are interested but don't have much to say that hasn't been said. The thread title is interesting in itself.
2354  Using Arduino / Project Guidance / Re: Using Tx and Rx ports with several modules on: May 13, 2013, 10:01:38 pm
Does the tractor have a fuse box?
2355  Community / Website and Forum / If you see this screen again, report it to an administrator. on: May 13, 2013, 07:31:29 pm
Got that database error twice when trying to list replies.

I hope this is where I should report, I dunno where else.

Pages: 1 ... 155 156 [157] 158 159 ... 361