Show Posts
Pages: 1 2 [3] 4 5 ... 55
31  Using Arduino / Programming Questions / Re: Anyone please help me on: August 27, 2014, 08:23:29 am
It would seem that the displayChar() function could be simplified a bit:

Code:
#define ARRAYSIZE(x) (sizeof(x) / sizeof(x[0]))   // Place near top of sketch

void displayChar(char array[])
{
   int i;

   for (i = 0; i < ARRAYSIZE(array); i++) {
      displayLine(array[i]);
      delay(delayTime);
   }
   displayLine(0);
}

Also, in displayLine(), why define myline? Just use line as it is passed into the function.
32  Using Arduino / Programming Questions / Re: reconstructing 3 byte array to long after EEPROM read on: August 27, 2014, 08:00:13 am
@Paul: I think Mike and I are talking at cross purposes to some degree. I was thinking of exactly what you mentioned as I have run across that problem in the past. While I haven't checked, my guess is the packing/unpacking might have a speed/size advantage, but I think the union approach is easier for a newbie to understand.
33  Using Arduino / Programming Questions / Re: reconstructing 3 byte array to long after EEPROM read on: August 27, 2014, 07:43:26 am
@Mike: Not sure I follow, or perhaps we're talking at cross purposes. Are you suggesting that the packing/unpacking code works for an Uno and a Due for all basic data types without change, and that I can move that same code to a PIC, chipKIT, or other  controller without modification?
34  Using Arduino / Programming Questions / Re: problem with my code :/ on: August 26, 2014, 10:11:41 pm
In loop():
Code:
void loop() {
 // Measure distance
  digitalWrite(TRIGPIN, LOW);                   // Set the trigger pin to low for 2uS
  delayMicroseconds(2);
  digitalWrite(TRIGPIN, HIGH);                  // Send a 10uS high to trigger ranging
  delayMicroseconds(10);
  digitalWrite(TRIGPIN, LOW);                   // Send pin low again
  int distance = pulseIn(ECHOPIN, HIGH);        // Read in times pulse
  distance= distance/58;                        // divide by 58 gives cm.

// Measure distance 2
  digitalWrite(TRIG_PIN2, LOW);                   // Set the trigger pin to low for 2uS
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN2, HIGH);                  // Send a 10uS high to trigger ranging
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN2, LOW);                   // Send pin low again
  int distance2 = pulseIn(ECHO_PIN2, HIGH);        // Read in times pulse
  distance2= distance2/58;                        // divide by 58 gives cm.
  
 // Convert measured value1 to value between 0-11, to display on LCD
  // Use Arduino built-in map and constrain functions
  int scaledValue = map(constrain(distance, highWater, lowWater), lowWater, highWater, 0, 11);  // =======  Pick this one or... ====
  lcd.clear();
  lcd.print("E    SENSOR 1    F");
  lcd.setCursor(0,1);
  while (scaledValue > 0) {
     lcd.print((char)0);
     scaledValue--;
  }
      
   delay(2000);  // Wait 2 seconds before printing the 2nd value.

  // Convert measured value2 to value between 0-11, to display on LCD
  // Use Arduino built-in map and constrain functions
  int scaledValue = map(constrain(distance2, highWater, lowWater), lowWater, highWater, 0, 11);  // ======= ...this one, not both ===
;//<<<<<<<<note distance2
  lcd.clear();
  lcd.print("E   SENSOR 2   F ");
  lcd.setCursor(0,1);
  while (scaledValue > 0) {
     lcd.print((char)0);
     scaledValue--;
  }
      
      delay(2000);  // Wait 2 seconds before measuring again. We're in no hurry!
}

You have define scaledValue twice. You can't use the same variable name twice at the same scope level.
35  Using Arduino / Programming Questions / Re: Cannot convert serial input from int to ASCII using atoi function on: August 26, 2014, 09:48:50 pm
Quote
I am trying  to convert int to ASCII.
If that's the case, then why are you reading the Serial object?

The following code waits for an integer to be entered from the keyboard and the user to click Send. It also takes the arbitrary value of 12345 and converts it to ASCII.
Code:
void setup() {
  Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}

void loop() {

  char buf[12];
  int byteReceived;
  //delay(5000);
  // send data only when you receive data:
  if (Serial.available() > 0) {
    // read the incoming byte:
    byteReceived = Serial.readBytesUntil('\n', buf, 11);   // Keep room for null
    buf[byteReceived] = '\0';   // At this point buf[] hold the ASCII for the int that was entered
    Serial.print("Received: ");
    Serial.println(buf);
    Serial.print("To make the ASCII value an int: ");
    Serial.println(atoi(buf));
  // If you want to convert an int to ASCII, why are you reading the Serial object?
  // To convert:
    int val = 12345;
   
    itoa(val, buf, 10);
    Serial.print("val = ");
    Serial.println(val);    // Done...
  }
}
36  Using Arduino / Programming Questions / Re: reconstructing 3 byte array to long after EEPROM read on: August 26, 2014, 09:26:35 pm
I still think a union offers an easy way to do this kind of stuff and allows you to ignore the endian problem.

Code:
union longConverter {
   char buff[4];
   long val;
} myData;

To write a long to EEPROM, use something like:

Code:
   myLong = 123456L;
   myData.val = myLong;   // Put the long into the union...
   for (int i = 0; i < sizeof(long); i++)
    EEPROM.write(i, myData.buff[i]);   // Write the data to EEPROM, starting at EEPROM address 0

To retrieve the value back from EEPROM:

Code:
   for (int i = 0; i < sizeof(long); i++)
     myData.buff[i] = EEPROM.read(i);   // Read data back from EEPROM, starting at EEPROM address 0
   myLong = myData.val;   // Retrieve the long from the union

The same approach can be done for other data types, too. It can be more portable, too. For example, doubles and floats are the same on an Uno, but doubles are 8 bytes on a Due. The code above, written for a double data type, would work without change if ported to a Due. Bit shifting would not.
37  Using Arduino / Programming Questions / Re: How would I include 10 functions into an array? on: August 26, 2014, 09:11:01 pm
You can use Purdum's Right-Left Rule to figure out what complex data definitions are, like BulldogLowell's:

void (*myFunction[2])(void);

Verbalizing: "myFunction is an array of two pointers to function with void arguments that return void". See:

http://jdurrett.ba.ttu.edu/3345/handouts/RL-rule.html

38  Using Arduino / Programming Questions / Re: Cannot convert serial input from int to ASCII using atoi function on: August 26, 2014, 09:04:45 pm
Well, several things:

1) part of the problem is that 2147483648 is a tad bit bigger than will fit in an int
2) incomingByte is a horrible name for an int
3) itoa() converts from integer to ASCII but you're reading it as ASCII using the Serial object, so why convert it to what it already is?

I'm not sure I understand what you're trying to do.
39  Using Arduino / Programming Questions / Re: Can integer values be assigned to I/O prts? on: August 26, 2014, 09:29:50 am
This might be of interest:

http://arduino.cc/en/Tutorial/Fade
40  Using Arduino / Programming Questions / Re: Serial.available() returns wrong value. on: August 26, 2014, 08:45:51 am
Sometimes you can use the Serial object's readBytesUntil() method to simplify reading an input stream of text that has a known termination character (e.g., the newline, '\n').  A typical use can be shown with the following example:

Code:
#define MAXCHARS 50

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

void loop() {
 char message[MAXCHARS];
 int charCount;
 
 if (Serial.available() > 0) {
   charCount = Serial.readBytesUntil('\n', message, MAXCHARS - 1);
   message[charCount] = '\0';    // Make it a string
   Serial.print("characters read = ");
   Serial.print(charCount);
   Serial.print("   message = ");
   Serial.println(message);
  }
}
41  Using Arduino / Programming Questions / Re: Hi, where does the compiler look for libraries and which one does he take on: August 26, 2014, 08:31:50 am
You might find this helpful:

http://arduino.cc/en/Hacking/BuildProcess

42  Using Arduino / Programming Questions / Re: Running out of ram, help please. on: August 24, 2014, 11:57:43 am
Which Arduino board are you using and which version of the IDE? Also, what's the purpose of this line?

Code:
   while (index < ENTRY_SIZE);

If the code gets here, it seems like you'll spend a lot of time here since the loop body doesn't change anything.
43  Using Arduino / Programming Questions / Re: Passed data to method is null on: August 23, 2014, 11:22:24 pm
You don't have that many different elements in the notes[] array, so I would code those as bytes.  The frequency() function would have to be modified to interpret the byte values that come in to the proper frequencies, but I don't think that would be too hard. Next, I would change the beats[] array into byte values. Since you have a minimum value of .25 and a max value of 1.5, you could have a byte value of 1 correspond to .25, 2 = .50, 3 = .75, and so on.  This reduces the memory by 3 bytes per note. You can adjust the math for tempo * beats for the new values. The same indexing scheme can be applied to the notelist[] array. I think you'd have enough memory after doing that.
44  Using Arduino / Programming Questions / Re: Passed data to method is null on: August 23, 2014, 11:06:34 pm
That's a lot of arrays. What board are you using? My guess is that you're running out of memory.
45  Using Arduino / Programming Questions / Re: ERROR- 'mainColors () ; was not declared in scope. on: August 22, 2014, 10:07:13 pm
Your first post...welcome. You're missing the opening brace on the function:

Code:

void mainColors () {      // Note the opening brace at the end of the line


BTW, please read the first two posts on this Forum, which lay down the guidelines for posts to the Forum. The guidelines will help you get responses from the readers. First, please post your code using code tags. Second, try to reformat your code before posting using Ctrl-T in the IDE source window. These and other things should help you down the road.
Pages: 1 2 [3] 4 5 ... 55