Show Posts
Pages: [1]
1  Using Arduino / Programming Questions / Re: Using atoi() to convert Serial data on: January 13, 2012, 05:05:32 pm
PaulS

Thank You - that is exactly what I missed.

I was running this sketch on a Sanguino, which is only compatible up to 023. I had to find a way to call seriallEvent(), this is working now, but realize I will need to take this  if statement out if I want to run this sketch on 1.0.

Thanks for your help!!
2  Using Arduino / Programming Questions / Using atoi() to convert Serial data on: January 13, 2012, 02:34:57 pm
I am working on a sketch that will allow me to control the Arduino from a computer using serial. I have written a program written in Processing that is going to provide the control interface, so I have full control over the Serial link. I have also written a simple sketch in Arduino to test. This sketch is based on the Blink and Serial.available examples. Basically I am just trying to change the blink delay rates using serial commands. Everything works great (I stuck a couple of println() calls in there so I could see what was going on) except when I try to convert my char array (string) to an int. I did some research on the atoi() function, and realize I need to insert a null character at the end of the array for it to be a valid string, and allow atoi() to convert it into an int. I did that - but the function is still returning zero, which from my research means that it is trying to convert an invalid string.

I am stumped...What am I missing?
Code:
#define greenLed 7
#define yellowLed 6

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
int greenDelay = 200;
int yellowDelay = 1000;
char tempChar[10];
int i;



void setup() {
  // initialize serial:
  Serial.begin(9600);
  pinMode(greenLed, OUTPUT);
  pinMode(yellowLed, OUTPUT);
 
}

void loop() {
  // print the string when a newline arrives:
  if (Serial.available()>0) serialEvent();
  if (stringComplete) {
    Serial.println(inputString);
    switch (inputString.charAt(0)){
      case 'A':
      for(i=1 ; i<inputString.length() ;i++){
        tempChar[i] = inputString.charAt(i);
        Serial.println(tempChar[i]);
      }
      tempChar[inputString.length()] = '\0';
      greenDelay = atoi(tempChar);
      Serial.print("New Green Delay Value:");
      Serial.println(greenDelay);
      break;
      case 'B':
      for( i=1 ; i<inputString.length() ; i++){
        tempChar[i] = inputString.charAt(i);
      }
      tempChar[inputString.length()] = '\0';
      yellowDelay = atoi(tempChar);
      Serial.println(tempChar[0]);
      Serial.print("New Yellow Delay Value:");
      Serial.println(yellowDelay);
      break;
      default:
      Serial.println("Invalid Command");
    }
    inputString = "";
    stringComplete = false;
  }
    blinkGreen(greenDelay);
    blinkYellow(yellowDelay);
}


void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

void blinkGreen (int x){

 digitalWrite(greenLed, HIGH);   // set the LED on
  delay(x);              // wait for a second
  digitalWrite(greenLed, LOW);    // set the LED off
  delay(x);              // wait for a second
}
void blinkYellow (int x){

 digitalWrite(yellowLed, HIGH);   // set the LED on
  delay(x);              // wait for a second
  digitalWrite(yellowLed, LOW);    // set the LED off
  delay(x);              // wait for a second
}

3  Using Arduino / Installation & Troubleshooting / Sanguino not uploading on: December 04, 2011, 04:11:01 pm
I have a Sanguino that I have been using for about a month. It was working great until today. The last sketch I uploaded to it was a serial communication sketch I was working on last weekend. When I try to upload a new sketch (I am trying to use the blink example) the IDE says uploading, and just continues to run for more than 10 minutes. Note I do see the file size information come-up, I believe that means the compiler is done. That is when the red RX led light a few times then stops, it goes back to a green TX, over and over (original Sketch). I have tested my USB breakout board with a barebones board, and that is working fine. I have tried it on two different computers, both running Arduino version 22, with the exact same result.

I noticed that after a few seconds of blinking red led receive signals, I see steady serial transmissions (green) coming from the board, I looked in the serial monitor, and it is the original sketch running, which since I have disconnected all the analog sensors I was using, is just sending 0,0,0.

I have read through the troubleshooting page and did not find anything that helped. And tried manually resetting during upload (no luck.) Nothing else seemed to fit my problem. From what I read in forums I am thinking it could be a few things:

1) a bad crystal oscillator - but would that give me an out of sync error? How do I test this?
2)  corrupted bootloader? I was thinking of trying to reload the bootloader - I have never done that before, but will if that really is a possible solution. Is there any way to test this?

Any other suggestions?

Board - Sanguino
Arduino - Version -22
OS - I tried both an XP and a Windows 7-64bit machine

Thanks in advance.
4  Development / Other Hardware Development / Re: tone() with sanguino on: November 20, 2011, 08:30:28 pm
I think I may have figured this out for myself. I do not know too much about changing around the arduino hardware - but after reviewing a lot of forums about this issue and some trial and eerror. This is what I did to make it work.

Open the file named WProgram.h in the arduino-0022\hardware\Sanguino\cores\arduino folder.

add the following two lines:


void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0);
void noTone(uint8_t _pin);

save the file, Close Arduino, reopen Arduino, Upload and it did compile for me.

I do not know why this works but it does. If anyone else knows why, I would like to know so that I could solve a similar issue in the future, and understand what I am doing.
5  Development / Other Hardware Development / tone() with sanguino on: November 20, 2011, 01:19:25 pm
I am trying to use the tone() function with a sanguino board. When I complie the example code toneMelody.pde I get this error:

toneMelody.cpp: In function 'void setup()':
toneMelody:35: error: 'tone' was not declared in this scope
toneMelody:42: error: 'noTone' was not declared in this scope

I can compile as long as a Duemilanove is selected as the board (or any other board for that matter.) I can also upload the sketch, play the tune etc. This is only a problem with the Sanguino board.

I have searched for this problem, and seen several posts, but none have a definate answer. I have tried deleting the Arduino folders, re-downloading, recopying/downloading the Sanguino Hardware folder. and I have tried both Version 22 and 23 of Arduino.

Does anyone know how to fix this issue?

Thanks
6  Using Arduino / LEDs and Multiplexing / Big Numbers from int on: November 11, 2011, 11:54:55 pm
I am new to Arduino and started a project to make a digital guage for a boat. I saw the large fonts all over the forum and on projects like MPGuino and ODBuino, but it took me a while to figure it out. I thought it would be good to post this code for anyone else who is trying to do the same. This is still not perfect, but I think it would be simple for a newbie like me to implement into a project. Here is the code,  like I said I am a newbie, so all comments are welcome.

My main goal is to write a function that takes an int and prints it onto a 16X2 LCD using two lines for each digit.

Code:
/*This sketch uses functions taken from various forums on the Auduino website. It is for a specific project, but could easily be modified for other uses.
the purpose of this sketch is to create a function that can take an integer and print it in a 2 line "large font"
on an Alpha-numberic LCD screen. This sketch uses those functions to demostrate the possibilties in a loop in big numbers
written by Chad Brochard; with a trememdous amount of material obtained from the Arduino forum at www.arduino.cc
Date 11/11/11 started at 11:11 in the afternoon. Just on more chance for that mathematical redundancy this century in one year, one month, one day, one hour,
and one minute from now... wait next year is a leap year... so that is one year, one month, two days, .... you get the idea.
 */

#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

byte custchar[8][8] = {
 {
   B11111,
   B11111,
   B11111,
   B00000,
   B00000,
   B00000,
   B00000,
   B00000
 }, {
   B00000,
   B00000,
   B00000,
   B00000,
   B00000,
   B11111,
   B11111,
   B11111
 }, {
   B11111,
   B11111,
   B11111,
   B00000,
   B00000,
   B11111,
   B11111,
   B11111
 }, {
   B00000,
   B00000,
   B00000,
   B00000,
   B00000,
   B01110,
   B01110,
   B01110
 }, {
   B00000,
   B00000,
   B00000,
   B01110,
   B01110,
   B01110,
   B00000,
   B00000
 }, {
   B00000,
   B00100,
   B01010,
   B10001,
   B01010,
   B00100,
   B00000,
   B00000
 }, {
   B00000,
   B11011,
   B11011,
   B00000,
   B00100,
   B10001,
   B01010,
   B00100
 }, {
   B00000,
   B00000,
   B00000,
   B00000,
   B00000,
   B00000,
   B00000,
   B00000
 }
};

byte bignums[10][2][3] = {
 {
   {255, 0, 255},
   {255, 1, 255}
 },{
   {0, 255, 254},
   {1, 255, 1}
 },{
   {2, 2, 255},
   {255, 1, 1}
 },{
   {0, 2, 255},
   {1, 1, 255}
 },{
   {255, 1, 255},
   {254, 254, 255}
 },{
   {255, 2, 2},
   {1, 1, 255}
 },{
   {255, 2, 2},
   {255, 1, 255}
 },{
   {0, 0, 255},
   {254, 255, 254}
 },{
   {255, 2, 255},
   {255, 1, 255}
 },{
   {255, 2, 255},
   {254, 254, 255}
 }
};

void loadchars() {
 lcd.command(64);
 for (int i = 0; i < 8; i++)
   for (int j = 0; j < 8; j++)
     lcd.write(custchar[i][j]);
 lcd.home();
}


//This function prints an integer between o and 999 in a big font at the left of the display
// Argument is a byte array Digits[# of digits, Digit 1, Digit 2, Digit 3]
// Nothing is returned.
// Numbers outside of the limits of this function will print an error screen.


void printCustom (int x)
{
byte digits[4];
if (x>99)
{
digits[0] = 3;
digits[1] = x/100;
digits[2] = (x%100)/10;
digits[3] = (x%100)%10;
}
if ((x>9) && (x<=99))
{
digits[0] = 3;
        digits[1] = 0;
digits[2] = x/10;
digits[3] = x%10;
}
if (x<=9)
{
digits[0] = 3;
        digits[1] = 0;
digits[2] = 0;
        digits[3] = x%10;
}


if (digits[0] > 3){
  lcd.print("PRINT ERROR");
  return;
}
for(int e = 1; e < 2; e++)
{
if(digits[e] > 9){
            lcd.print("PRINT ERROR");
             return;
            }
}

for(int digitLoc= 0;digitLoc<digits[0];digitLoc++)
{
for (int i = 0; i <2; i++)
{
lcd.setCursor(digitLoc*4,i);
for(int j = 0; j < 3; j++)
{
lcd.write(bignums[digits[(digitLoc+1)]][i][j]);
}
lcd.write(254);
}
}
}


//Setup the lcd screen and load the custom characters

void setup() {
 loadchars();
 lcd.begin(16,2);
 Serial.begin(9600);

}

// main loop, this sketch counts from 1 to 10, then from 90 to 105, Then from 159 to 959 by 100s. to show the capabilty of printing up to 3 digits.
// these nubers are arbitrary, just want to be sure to test numbers with 1, then 2, then 3 digits.


void loop() {

int sampletemp = 75;
int errortest = 1234;
 
for (int i=0; i < 10; i++)
{
  printCustom (i);
         Serial.println(i);
delay(250);
}

for (int i=90; i < 105; i++)
{
  printCustom (i);
         Serial.println(i);
         delay(250);
}

for (int i=159; i < 959; i=i+100)
{
  printCustom (i);
        Serial.println(i);
delay(250);
}
lcd.setCursor(0,0);
printCustom(sampletemp);
 Serial.println(sampletemp);//write the sample temperature, 75. using printCustom (big font)
lcd.setCursor(11,0);
lcd.write(5); //show a 5th custom character which is supposed to look like a degree mark.
delay(2000);

//cause a print error to test the error
lcd.setCursor(0,0);
printCustom(errortest);
delay(2000);

}
7  Using Arduino / Sensors / Speed sensor - Hall effect, Digital Pin? on: October 03, 2011, 02:21:30 pm
I have an old hummingbird trolling indicator, I want to use in my project. I have pulled it out of the boat and done some testing/research. If I put 5V into the sensor from my Arduino, I can rotate the wheel (see attached photo) and Read a range from 0 to 0.68 V on the output of the sensor. I did not think that .68 V was enough to trigger a high value on a digital pin, so I have been trying to read it with an Analog pin: Here is my code (it doesn't work) If I could use the Output of the sensor on a digital pin, then just count the HIGHs it would be a simpler code. I am trying to read the 133 off of the Analog pin, and be sure that I do not get double reads, by comparing it with the old value. Any Tips??

   
Code:
 


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

void loop() {
 
  int revs = 0;
  long endtime = millis() + 500;
  int count = 0;
  int value = 0;
  int old_value = 0;
  do {
  value = analogRead(A0);
  if (value >= 130 && value != old_value){
  count++;
  old_value = value;

  }
  } while (millis() < endtime);
  revs = count / 500;
  Serial.println(revs, DEC);
}
8  Using Arduino / Project Guidance / Re: Boat Digital Guage Project on: September 18, 2011, 09:26:18 pm
#Techone

Thanks for all of the advice. I think I understand what you are saying. Basically you are getting the current by comparing the voltage difference between a circuit with only two resistors, and each circuit with 2 resistors and the sensor. In the end this setup is going to cause additional resistance in the circuit, therefore changing the current measured by the "meters" or exciting gauges. This would be fine if there were some adjustment in the gauges, but there isn't.

I think I am going to go back to your original suggestion, and get 3 new sensors (actually, I am only going to get, two - I can live with a digital only trim gauge, and the mechanics involved with adding another trim sensor would be difficult.)

Thanks again for your time and advice.
9  Using Arduino / Project Guidance / Boat Digital Guage Project on: September 18, 2011, 10:49:37 am
Hi, I am new to Arduino - and electronics to some degree, but I have some experience with programming. I spent this weekend learning the Arduino board and the programming code and would like to get my hands dirty on this first project. It is based on the MPGuino project I saw on the web, however, it is for a boat, and is going to be a digital guage cluster for the boat. The MPGuino project uses all Digital inputs, I am trying to use all Analog inputs. Not only that, I would like to use the exisiting temp,oil pressure and trim sensors on the boat. I will use new sensors for air and water temp, since I currenlty have no sensors reading those. The code should be pretty simple to write starting with the MPGuino code and modifing it for this project. What I am concerned about is if I am correctly wiring the sensors to the Arduino board. I have attached my Circuit diagram, and a parts list to go along with it. I am using diodes to protect/seperate the 5V circiut for the Arduino, from the exisiting 12V circuit that the Analog guages use. Keep in mind I want to have this digital guage as a secondary guage - so I need to keep all the Analog guages intact. Any help is appreciated - I am going to tinker with the resistors and code to get the calibration right, but I do not want to fry the Arduino board the first time I plug it all up.
Pages: [1]