Show Posts
Pages: [1] 2 3 ... 45
1  Using Arduino / Programming Questions / Re: How can I send and recive multiple values over serial? on: Today at 09:08:10 am
If you are using Visual Basic to send the serial data, type in the data packet into a textbox and then append a newline character ('\n') to the data before sending it to the Arduino. Then use the Serial.readBytesUntil() method to read the data into your input[] array until the newline is read. Then break it apart as you wish.
2  Using Arduino / Programming Questions / Re: arduino serial erratic behaviour missing characters on: Today at 06:33:39 am
I commented out the calls to the Adafruit library and ran the code just fine. However, if I included the angle brackets ("<", ">") as part of the string, the parseint method fails. I don't know if you're doing that or not, but your post says you are. Remove the brackets and see what happens.
3  Using Arduino / Programming Questions / Re: reset static variable on: July 20, 2014, 09:26:54 pm
Yet another example of why posters need to post all of their code at the outset.
4  Using Arduino / Programming Questions / Re: Unexpected pow() behaviour on: July 20, 2014, 12:39:13 pm
It is never a good idea to pass in the wrong type of data or ignore what is returned. Change your code to:

Code:
#include <math.h>

void test(float a) {
  float b = pow(2.0, a);
  Serial.print("b = ");
  Serial.println(b);            // Outputs 3
}

void setup() {
  Serial.begin(115200);
  test(2.0);
}

void loop() {}

and the answer 4.0 is returned. Note that I passed 2.0 (a float) to test() and passed two floats to pow() and treated the return type as a float.
5  Using Arduino / Programming Questions / Re: Void Setup on: July 17, 2014, 10:42:26 am
@grove: I should have said it cannot have a return statement like:

Code:
   return expression1;

where expression1 is some expression that resolves to some form of data. If the return is simply being used as a control element, it can appear within the function body.
6  Using Arduino / Programming Questions / Re: Using 'switch () { case#: do something.. Simpler method or a better alternative? on: July 17, 2014, 08:47:47 am
Quote
>ManuOUTval' cannot be used as a function

You might be seeing this message because your are using parentheses with ManuOUTval rather than brackets. That is, you are using:

Code:
  temp = ManuOUTval(x);      // WRONG: parentheses

when actually you mean:

Code:
  temp = ManuOUTval[x];      // RIGHT: brackets

7  Using Arduino / Programming Questions / Re: calculate running 60 minute total on: July 17, 2014, 08:40:13 am
How are you tracking the time? Does the Arduino have an RTC module on it, or are you counting the minutes in some other way? Is the data being sent to the WU an average for the hour? Something like the following might work if I understand the problem correctly:

Code:
void setup()
{
   // Whatever it takes to set things up??
 }

void loop()
{
  static int currentMinute, lastMinute = 0;
  static float hourlyRainTotal = 0.0;

  currentMinute = GetMinute();      // Might be a call to a RTC module

  if (currentMinute != to lastMinute) {      // Has a minute gone by since last reading?
    hourlyRainTotal += GetRainTotal(currentMinute);
    if (lastMinute == 59) {
      hourlyRainTotal = GetRainTotal(59);
      hourlyRainTotal = 0.0;      // Get ready for next hour
    }
    lastMinute = currentMinute;
  }
}

float GetRainTotal(int minute)
{
  static byte rainPerMinute[60];
  int i;
  int sum= 0;

  total[minute] = ReadDavisVP2();      // Get the current rain value

  if (minute == 59) {      // We are ready to find an hour's worth of rain
    for (i = 0; i < 60; i++) {
      sum += rainPerMinute[i];
    }
    return sum * .1;      // The total rain during the last hour
  } else {      // Return the current rain total for the current minute
    return total[minute] * .1;
}


Clearly I haven't tested the code, so this is just an outline of how it might work.
8  Using Arduino / Programming Questions / Re: 'PINC' was not declared in this scope on: July 16, 2014, 12:05:38 pm
It compiles without error on my Arduino Uno, so the hiccup might be with the use of the Due board.
9  Using Arduino / Programming Questions / Re: #define, int or const int on: July 16, 2014, 10:52:05 am
True, but compiling with the default settings for the IDE is alarmingly quiet, and those are the settings that most newbies use.
10  Using Arduino / Programming Questions / Re: #define, int or const int on: July 16, 2014, 09:47:10 am
Quote
Its the normal integral conversions which allow them to be converted and assigned, and the same rules apply regardless of weather whether the integer is a literal, variable, or constant.

I was very careful to use the term "expression" in what I said. Normally, if the compiler assigns a "smaller" data type into a "larger" data type (e.g., long = int), the silent cast is done without fanfare. However, the compiler should complain when it assigns a "larger" data type into a "smaller" data type (e.g., byte = int) and it doesn't. While the compiler must use some default data type in the preprocessor pass, it's the resolution of that expression that makes it seem to be typeless.
11  Using Arduino / Programming Questions / Re: #define, int or const int on: July 16, 2014, 09:15:47 am
Quote
But its replaced value is not.

True, but it can assume the resolved data type used in the expression. It is typeless in that it can be used in almost any expression where a data type is used and the compiler won't flag it as a type mismatch where a cast would normally be required.
12  Using Arduino / Programming Questions / Re: #define, int or const int on: July 16, 2014, 08:58:22 am
Quote
They are both integer variables

Well, not really. Because a #define is a textual replacement, it is "typeless". That is: the code

Code:
#define MYVAL 500

void setup() {
  // put your setup code here, to run once:
  int number = MYVAL;
  byte value = MYVAL;
}

void loop() {
  // put your main code here, to run repeatedly:
}

compiles without error, but assigning 500 into a byte probably isn't going to work. This is likely why James says they can introduce hard-to-find bugs. There are, however, times when the fact that a symbolic constant is typeless can be a good thing (e.g., when working with a union). While James clearly doesn't like #define's, they do have their place. If the IDE had a symbolic debugger, I would rarely use #define, since it cannot be traced by a debugger once the preprocessor pass completes.

In either case, I think it helps to upper-case both #define's and const data definitions to signal the reader that the expression is different.
13  Using Arduino / Programming Questions / Re: Void Setup on: July 16, 2014, 08:43:58 am
Quote
What do you mean 'returning'  ?....Returning from / going to ?

Consider this example:
Code:
void setup()         // Function signature
{
   int val;

   val = myFunction();
}

int myFunction()      // Function signature
{
   int analogValue = analogRead(A0);
   return analogValue ;
}

In the sequence of things, in setup() the compiler generates code that defines what val is (an int) and immediately transfer program control to myFunction(). The process of transferring program control from setup() to myFunction() is often referred to as "calling myFunction()".  You may also hear the process as "going to", "envoking", and other similar verbs. You will also hear programmers refer to setup() in this process as "the caller"; that is, the caller is the name of the function that is giving up control to the function being called (i.e.,  myFunction()).

Once control is transferred to myFunction(), the code reads analog pin A0. This also means that myFunction() is "calling" analogRead().  (This process of one function calling another function, calling another function, can go on as many times as you need.) If you look at the documentation for analogRead(), it "returns" or "gives back" an int to whomever called it. In our example, that int value is assigned into analogValue.

The return keyword means that the code in myFunction() wants to give back that int value to the caller.  Note: the data type being returned (e.g., analogValue) MUST match the first word on the function's signature. In this example, the function signature is: int myFunction(), so analogValue must be an int or the compiler is going to get a little cranky. Let's assume that analogValue has the numeric value of 300.

Because the code to process the "return analogValue;" statement has been executed, the value 300 has been placed on a "serving platter" called the stack and program control reverts back to the caller. This process of transferring control back to the caller is often referred to as "returning to the caller".

When program control gets back to setup(), the program executes code that slides the int data on the serving platter (i.e., the stack) and assigns it into val.

Whenever you see the word void in a function signature, that means there is no return statement in that function and, therefore, it cannot return a value to the caller. The technical term for the data type mentioned in a function signature is "function data type specifier".  So if you look up the signature for random(), you will find its signature is: long random(int seed). Because the function type specifier is a long, this function returns a long data type to its caller. This also means that somewhere in that function is the equivalent statement return val, where val is the pseudo-random number generated by the function. The int seed in the signature says that the called must supply an integer value when it is called.

While long-winded, I hope this is helpful.
14  Using Arduino / Programming Questions / Re: simple logic question on: July 15, 2014, 03:14:04 pm
There are some idioms that are commonly used that you may want to use.

Code:
#define ElementsInArray(x) (sizeof(x) / sizeof(x[0])   // use below                   ***  #1 ***

//const int numReadings = 10;

int readings[10];      // the readings from the analog input
int index = 0;                  // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average
int lowest = 500;
int inputPin = A0;
int newlowest;
int counter = 0;

void setup()
{
  // initialize serial communication with computer:
  Serial.begin(9600);                   
  // initialize all the readings to 0:
/*
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0;
*/
      memset(readings, 0,  ElementsInArray(readings) * sizeof(int));    //          *** #2 ***

}

void loop() {
  // subtract the last reading:
  // counter = counter + 1;                    //                 *** #3 ***
  counter++;

 // total= total - readings[index]; 
  total -= readings[index];                   //    ***#4 ***   
  // read from the sensor: 
  readings[index] = analogRead(inputPin);
  // add the reading to the total:
  //total= total + readings[index];                                       //            *** #4 ***
total += readings[index]; 
  // advance to the next position in the array: 
 //  index = index + 1; 
  index++;                              // *** #3 ***   
  int temp2 = newlowest;
 
  // if we're at the end of the array...
//  if (index >= numReadings)             
  if (index >=  ElementsInArray(readings))     //   ** #1 ***         
             
    // ...wrap around to the beginning:
    index = 0;                           

  // calculate the average:
 // average = total / numReadings;   
  average = total / ElementsInArray(readings);   //    *** #1 ***
 
  if(counter > 100){
     if ( average < lowest){
       newlowest = average;
       lowest = newlowest;     
     }
  }
   
       // delay in between reads for stability           
  int temp1 = newlowest;
  if (temp2 < temp1){
    Serial.println(temp1);
    Serial.println(temp2);
  }
 
   delay(100);
}

#1 above is simply a macro that calculates how many elements an array has. The advantage is that you change the size at its point of definition and everywhere that size is used is automatically changed. #2 is an assembler routine that sets a block of memory to a set value. It doesn't do much here, but can save some time if it's in a tight loop. #3 and #4 are just common idioms for incrementing or decrementing a variable.
15  Using Arduino / Programming Questions / Re: Programme stuck in setup on: July 15, 2014, 10:54:47 am
I don't know squat about the GPS library, but these two lines make me wonder...

Code:
  Serial.begin(115200);              // Serial com rate
  Serial.println("Into Setup");
  bmp.begin();
  Serial.println("BMP complete");
 
  GPS.begin(9600);                   // Same com rate???
Pages: [1] 2 3 ... 45