Show Posts
Pages: 1 2 [3] 4 5 ... 62
31  Using Arduino / Programming Questions / Re: Serial ASCII on: September 22, 2014, 01:17:27 pm
This fragment:

Code:
SEROUT TXPIN,2400,[1,"p+",01,3].

suggests that you are writing out four arguments which are int, string, int, int. The code in your loop() is string, string, int, string. Could that be an issue?
32  Using Arduino / Programming Questions / Re: Arduino - general tutorials/lessons on coding? (C/C++?) on: September 22, 2014, 01:01:28 pm
Paul:
Quote
Big books usually have more examples than little books.

And that's a good thing when it's true. However, all of us have also seen "big books" that are little more than a repeat of the documentation that comes with the software (e.g., an online tutorial supplied by the vendor). In other cases, a lot of examples is a "shotgun approach" where the writer hopes that one of the examples "sticks". However, there are other cases where one or two well-chosen examples can get you from point A to point D by carefully planning of the steps along the way. If you have 150 pairs of eyes staring at you like deer in the headlights, you've probably picked a bad example. Using different examples in the classroom can help to refine the examples list to a point where the book is of manageable size for a one semester class. True, there will still be some who "don't get it" for any given example, but usually they can Google the topic and find plenty of other examples that might suit their learning needs.

33  Using Arduino / Programming Questions / Re: how to get specific data from the web server code? on: September 22, 2014, 09:04:44 am
Paul raises a good point: 'Yes' has three chars, so how does reading one character magically turn into three characters? Second, you have made several posts on this Forum, and yet you don't post your code according to the directions explained in the first two posts made at the top of this Forum. Please read those and use the code tags ('#') to surround your code. Finally, would this make more sense to you:

Code:
if (client.available())  {
    char c = client.read();
    c = toupper(c);
     if (c == 'Y')  {
        Serial.println("Yes obtained");
     }
}
34  Using Arduino / Programming Questions / Re: Arduino - general tutorials/lessons on coding? (C/C++?) on: September 22, 2014, 08:30:55 am
I think most beginners find it easier to learn C first and then take on C++. Paul's statement:

Quote
The bigger, the better, usually.

has the right caveat on it: usually. One of the brightest coders I ever knew could take over an hour telling you how to add two numbers together.  By the time he finished making his point, the student's eyes would be totally glazed over. Lesson: Great coders don't necessarily make good teachers.

As far as books, my suggestion is to go to Amazon and search for a topic like "C, Arduino" and read the reviews. The search will produce quite a few books for you to consider and read the reviews for those that have at least four or five reviews. In an unabashed plug, my book, Beginning C for Arduino is written for the reader who has zero prior programming experience. It's distilled from teaching university-level programming courses since my first C book back in 1982 (The C Programming Guide). Once you are comfortable with C, use the same process to tackle C++ and OOP.
35  Using Arduino / Programming Questions / Re: Need help with storage of multiple ints on: September 22, 2014, 08:15:19 am
@Bob, Paul: If he plans to take the samples at regular intervals (every 6 seconds), why not just use a single array and let the values "roll off the end". If he needs to know the sample time for a given sample, he can subtract 6 seconds for each sample from the last one taken. I don't know the granularity he needs for the time, but this would likely do for gov't work.

Perhaps something like:

Code:
#define UPDATESAMPLE 6000  // Update every 6 seconds for 50 samples in 5 minutes
#define SAMPLESTAKEN   50

unsigned long currentTime, previousTime;
int myTagValues[SAMPLESTAKEN];

void setup() {
  previousTime = 0L;
  currentTime = millis();
}

void loop() {
  int tagValue;
 
  currentTime = millis();
  if (currentTime - previousTime > UPDATESAMPLE) {                            // Time to update?
     memmove(&myTagValues[1], myTagValues, sizeof(int) * (SAMPLESTAKEN - 1)); // Bump all old values up 1
     myTagValues[0] = ReadNFCTag();                                           // Get the new tag value
     previousTime = millis();
  }
}

int ReadNFCTag() {
    // Code to read NFC value
}
   
36  Using Arduino / Programming Questions / Re: Excessive use of IF statements on: September 21, 2014, 07:29:58 pm
Your example:

Code:
#define LIMIT = 42;

has two errors in it, the equal sign and the semicolon. The error message is misleading, suggesting the compiler would benefit from a little improved error checking.

Given the statements:

Code:
template< typename T, size_t N > size_t ArraySize (T (&) [N]){ return N; }
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))

which do you think would be more likely to be understood and used by most readers of this Forum? The entire reason I even brought up the macro was to get rid of "magic numbers" in the source code and to make it easier to change an array size without having to read through the entire source file. Indeed, there are probably a fair number of readers of this Forum who don't even know how to use templates.
37  Using Arduino / Programming Questions / Re: Excessive use of IF statements on: September 21, 2014, 12:22:08 pm
@pYro_65:
Quote
But I do not understand what you are referring to when you talk about const, its not a declaration, but an expression, which can be assigned to a const value.

What I mean is that const is a data modifier and is part of a data definition (not declaration), which means it is tied to a given data type. While it is true that a const data definition does allocate memory for the variable and a #define does not, that does not mean you can't change the const value. For example:

Code:
  const int val = 20;
  int number = val;
  int *ptr;

  ptr = (int *) &val;
  *ptr = 25;

  if (number == 20)
     ; // do something

A major premise behind the const modifier is to create a variable whose rvalue doesn't change. I think allowing a pointer to change a const value is not a good thing, otherwise you wouldn't have used const in the first place. However, use a #define doesn't allow you to get into the same mischief because there is no memory allocated for the #define, so there's nothing for a pointer to access.



38  Using Arduino / Programming Questions / Re: Reading RS485 packet - one byte always wrong - HELP PLEASE on: September 21, 2014, 11:01:30 am
Paul: You're right. He could try modifying the line:

Code:
    bytesRead = Serial.readBytesUntil('\n', receivePacket, PACKETLENGTH);

to be:

Code:
    bytesRead = Serial.readBytes(receivePacket, PACKETLENGTH);

checking that bytesRead is the packet length and convert the string to numerics as needed.
39  Using Arduino / Programming Questions / Re: SOS using AnalogWrite on: September 21, 2014, 10:42:44 am
You can simplify the code a bit by using functions. Also, note that some of your comments don't square up with your code. Misleading comments are worse than no comments. Your last call to the RED led passed in a value of 259...probably not what you want. Also, Morse has a pretty well-defined relationship for spacing, but here I've just used the dot-to-dash ratio.  Finally, delay() is not a good choice for program delays. (See the Blink Without Delay example provided with the IDE.)

Code:
//LED Red=256, Green=127, Blue=0
#define DIT 100
#define DAH (DIT * 3)

int Pin_LED_Red=3;
int Pin_LED_Green=5;
int Pin_LED_Blue=6;

void setup() {
  // set mode of pins as outputs to run once
  pinMode(Pin_LED_Red,OUTPUT);
  pinMode(Pin_LED_Green,OUTPUT);
  pinMode(Pin_LED_Blue,OUTPUT);
}

void loop() {
  int x;

  for(x = 0; x < 3; x++)    // Three dits
  {
    sendChar(DIT);  
    pause(DIT);
  }
  delay(DIT + DAH);         // Inter-character spacing for Morse
  
  for(x = 0; x < 3; x++) {  // Three dahs
    sendChar(DAH);  
    pause(DIT);
  }
  delay(DIT + DAH);    

  for(x = 0; x < 3; x++)    // Three dits
  {
    sendChar(DIT);  
    pause(DIT);
  }
  delay(DAH * 5);              // Rest between messages.
}

void pause(long ms)
{
  analogWrite(Pin_LED_Red,0);
  analogWrite(Pin_LED_Green,0);
  analogWrite(Pin_LED_Blue,0);
  delay(ms);
}

void sendChar(long ms)
{
  analogWrite(Pin_LED_Red,250); // Mav intensity of red;
  analogWrite(Pin_LED_Green,125); // Half intensity of Green;
  analogWrite(Pin_LED_Blue,0); //No Blue because Red and Green make Orange
  delay(ms);
}
40  Using Arduino / Programming Questions / Re: Reading RS485 packet - one byte always wrong - HELP PLEASE on: September 21, 2014, 10:19:03 am
If you know that a valid pack always contains 5 bytes (e.g., "23454"), this might work:

Code:
#define PACKETLENGTH 5

char receivePacket[6];

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

void loop() {
  int bytesRead;

  if ((Serial.available () > 0)) {
    bytesRead = Serial.readBytesUntil('\n', receivePacket, PACKETLENGTH);
    Serial.print("bytesRead = ");
    Serial.println(bytesRead);
    receivePacket[bytesRead] = '\0';                              // Make it a C string
    if (bytesRead == PACKETLENGTH && receivePacket[4] == '4') {   // Right message?
      Serial.println(receivePacket);
    }
  }

}

The readBytesUntil() method in the example above reads a max of 5 chars of data. bytesRead tells you how many bytes were actually read. You then append a null to the character array to make it a C string. The if block simply tests to see if it conforms to the type of data your are expecting. In this example, I'm using the Serial monitor to send the data. Since your "real" data is receiving hex numerics, you'd need to adjust accordingly. However, the general idea might work.
41  Using Arduino / Programming Questions / Re: Excessive use of IF statements on: September 20, 2014, 08:33:06 pm
@spicetraders: I agree. switch/case is usually easier to read that cascading if statement blocks, especially when the list gets fairly long.
42  Using Arduino / Programming Questions / Re: Excessive use of IF statements on: September 20, 2014, 08:07:56 pm
@pYro_65:
Quote
You keep mentioning this, however, it is not true.

What I said is true:

Quote
The macro ELEMENTCOUNT(x) returns the number of elements in the x array regardless of data type.

Perhaps I should have clarified that the macro is useful for an array of basic data types, like char, int, long, etc. I was, however, quite clear that I was talking about an array. It is typeless in that you can use any basic data type with the macro whereas with const you must state the data type.
43  Using Arduino / Programming Questions / Re: Excessive use of IF statements on: September 20, 2014, 01:17:50 pm
@MarkT: I agree. Too often students try to make a function do more than one task. Creating a Swiss Army Knife (SAK) function is rarely a good idea. In terms of OOA&D terms, a function should be cohesive. By that, I mean you should be able to describe exactly what the function does in two sentences or less. Any more than that and it is likely trying to do too much. SAK function tend to have long argument lists.

Almost all computer programs break down into five steps:
1. Initialization
2. Input
3. Processing
4. Output
5. Termination

Step 1 is usually found in setup(). It sets the environment before the program seems apparent to the user. Steps 2 through 4 are part of loop(). Microcontroller programs usally don't have an obvious Step 5, as they are designed to run until failure, power loss, or some explicit event. It's nice when loop() contains three function calls which can then call sub-functions in a "sideways" refinement of loosely-coupled functions. It makes debugging much easier.
44  Using Arduino / Programming Questions / Re: interfacing on: September 20, 2014, 12:53:33 pm
Odd...do you and arunmohan08ar have the same homework assignment?
45  Using Arduino / Programming Questions / Re: interfacing on: September 20, 2014, 12:42:20 pm
Google is your friend. It appears that the CC2500 has been replaced with the CC3000.

https://learn.adafruit.com/adafruit-cc3000-wifi

Pages: 1 2 [3] 4 5 ... 62