Show Posts
Pages: 1 ... 15 16 [17] 18 19 20
241  Using Arduino / Project Guidance / Re: Waiting for a byte to received in the Serial buffer before moving on on: August 04, 2012, 06:42:51 pm



It's not a particularly good way top handle serial port input,l but if you want your sketch to wait and do nothing until input arrives on the serial port, that while loop will do it.

What would be a better way to wait for serial data to show up?  The objective is to have two arduino's talk to each other over bluetooth.  To coordinate their activities I want to make sure the master device waits until the slave device has finished executing a command before sending another command.  So it will send a command over bluetooth (by writing to the serial port) and then wait for the slave device to respond with a '1' (not ascii...just a byte) and then it will send the next command.
242  Using Arduino / Project Guidance / Waiting for a byte to received in the Serial buffer before moving on on: August 04, 2012, 05:47:03 pm
I have tried:

Code:
while(!(Serial.available())){}

but the code seems to be rolling right over that.  There shouldn't be anything in the serial buffer at that point in the code.  On a sort of related note:

Code:
Serial.read();

should empty a byte from the buffer shouldn't it?
243  Using Arduino / Project Guidance / Re: Code to have two arduinos talk to each other over bluetooth on: August 02, 2012, 10:33:32 am
As soon as it sends a "1" back though it should break the Master out of that loop and in to the master's "while(1)" loop shouldn't it?

Yes but the damage has already been done. If the slave ever has more than one byte in its buffer it will never clear out those bytes faster than the master sends more.

The fix might be as simple as:

Code:
//If you receive a 1 respond with a 1
    if (Serial.read()==1){
      Serial.write(1);
      while (Serial.read()) {} // <---------------------------
    }

Nice!  I will try that.  I'm surprised that in the Serial library there isn't a Serial.purge or something to empty the buffer :-)
244  Using Arduino / Project Guidance / Re: Code to have two arduinos talk to each other over bluetooth on: August 02, 2012, 10:04:53 am
In the master:

Code:
void loop() {
 
  //Query device and wait for a response
  while(!(Serial.available()))
  {
    delay(20);
    Serial.write(1);
  }

It's sending "1"s every 20ms. If it manages to send more than one before the slave can respond then the slave will store the extra in its buffer. Your slave will never purge its buffer but instead, in the main loop, keep asking for another "1" after each is processed.


Thanks!  I will increase the delay and see what happens.  I think what you are saying is that the slaves Serial Buffer is filling up with a bunch of 1's and it is trying to work through all of those.  As soon as it sends a "1" back though it should break the Master out of that loop and in to the master's "while(1)" loop shouldn't it?
245  Using Arduino / Project Guidance / Re: Code to have two arduinos talk to each other over bluetooth on: August 02, 2012, 09:05:22 am
Does anyone see any thing?  I can explain what is supposed to happen more clearly.

1. Slave waits for the master to contact
2.  Master sends a '1' integer (not ascii) and waits for a response
3.  Slave receives the '1' and responds with a '1' to let the master know that it is there
4.  Master sends an integer (again not in ascii) and waits for the slave to sent a '1' to let the master know that the command has been executed
5.  Slave receives the integer and blinks its LED whatever number of times the master requested.  Then sends a '1' to notify the master that the command has been executed.
6.  Master receives '1' from the slave and sends the next integer and again wait for a '1' to come back
7.  Slave receives the integer from the master and blinks the led.....etc.

So the communication would go something like this:

Master Sends '1'
Slave Sends '1'
Master Sends '5'
Slave blinks the led 5 times and then send '1'
Master Sends '6'
Slave blinks the led 6 times and then send '1'
Master Sends '7'
Slave blinks the led 7 times and then send '1'
and so on
246  Using Arduino / Project Guidance / Re: Wireless arduino on: August 02, 2012, 08:57:22 am
Just break the big complicated project down into a lot of small easy steps and get them working one at a time.  That is how I have been tackling my projects as I am very much an amateur with this.
247  Using Arduino / Project Guidance / Code to have two arduinos talk to each other over bluetooth on: August 01, 2012, 08:58:17 pm
Hello,

So far I have been able to get things to work in terms of communication between two arduinos but it has been using delays to give the slave device time to execute a request.  Now I would like to use a query/response type of system where the master sends a 1 and waits to receive a 1 before sending its first request.  Then it waits to receive a 1 from the slave before sending the next request.  Right now it is just sending numbers that the slave will use to blink the pin13 led.  When I run it the LED pin just blinks continuously rather than once, twice, three times, and so on.  Hopefully someone can help me spot my problem in my logic.  The Master code is:
Code:
void setup() {               
  // initialize the digital pin as an output.
 
  Serial.begin(38400); 
 
}


void loop() {
 
  //Query device and wait for a response
  while(!(Serial.available()))
  {
    delay(20);
    Serial.write(1);
  }
  //Once we get a response begin sending data
  if(Serial.read() == 1){
    while(1)
    {
 
    for(int i = 1; i<11; i++)
      {
        Serial.write(i);
       
        //wait to get a response back from the slave saying
        //that the command was executed
        while(!(Serial.available())){}
        if(Serial.read() == 1)
        {
          delay(500);
        }
       
       
      }
    }
  }
}

and the Slave code is:
Code:
#define led 13


void setup() {
  // initialize serial:
  Serial.begin(38400);
  pinMode(led, OUTPUT);
 
}

void loop() {
  // wait for a query from the master
  while(!(Serial.available())){}
 
  //If you receive a 1 respond with a 1
    if (Serial.read()==1){
      Serial.write(1);
    }
    while(1){
    //wait for the data to come in
    while(!(Serial.available())){}
      int i = Serial.read();
    for(int j = 0; j < i; j++)
    {
      digitalWrite(led,HIGH);
      delay(200);
      digitalWrite(led, LOW);
      delay(200);
     
    }
    //send a 1 to notify the master that the command has been executed
    Serial.write(1);
    }
  }
248  Using Arduino / Project Guidance / Re: Potted plant watering system. (Involves water pumps.) on: July 23, 2012, 08:43:53 am
Probably late to the party here and somewhat irrelevant unless you can get the code working but I happened to run across this on ebay while I was looking for cheap arduinos.  Perhaps it could be useful to you.

http://www.ebay.com/itm/Moisture-Sensor-For-Arduino-urban-garden-tool-Arduino-Compatible-/150845999918?pt=LH_DefaultDomain_0&hash=item231f1f4b2e
249  Using Arduino / Project Guidance / Re: Trouble using analogRead when ethernet shield is plugged into the arduino on: July 20, 2012, 10:01:10 am
In case anyone runs into this same problem I will post the solution.  The ebay seller replied that the board is based off of the arduino ethernet shield v5.  I have attached a link to the schematic for that.  Sure enough pins 1 and 2 are connected to X1 which was causing a problem.  I changed my ADC input pin to A2 and it worked just fine. 

http://arduino.cc/en/uploads/Main/arduino-ethernet-shield-05-schematic.pdf

250  Using Arduino / Project Guidance / Re: Trouble using analogRead when ethernet shield is plugged into the arduino on: July 19, 2012, 12:52:05 pm
Sorry this was the item:

http://www.ebay.com/itm/Ethernet-Shield-W5100-Arduino-2009-UNO-Mega-1280-2560-/280916824651?_trksid=p5197.m1992&_trkparms=aid%3D111000%26algo%3DREC.CURRENT%26ao%3D1%26asc%3D14%26meid%3D722189686334089343%26pid%3D100015%26prg%3D1006%26rk%3D1%26

To be fair....the board does what it is supposed to do in terms of providing internet connectivity for my arduino as I have tested it in this capacity.  There seems to be some differences in the connections to the pin headers which I can hopefully resolve.
251  Using Arduino / Project Guidance / Re: Trouble using analogRead when ethernet shield is plugged into the arduino on: July 19, 2012, 12:03:47 pm
I purchased it here:

http://cgi.ebay.com/ws/eBayISAPI.dll?ViewItemVersion&item=320917833445&view=all&tid=914068615011

I will post a detailed picture of the shield when I get home where you can see the traces.  I will try to figure out where they go and if they aren't important i may just cut them.
252  Using Arduino / Project Guidance / Re: Trouble using analogRead when ethernet shield is plugged into the arduino on: July 19, 2012, 07:40:10 am
Looking closely at the ethernet shield, I do actually see some tracks leading off of the A0 and A1 pins.  I can't really tell where they go because the tracks are jumping from the top of the board to the bottom.  A2 through A5 look clean so I will try those when I get home tonight.
253  Using Arduino / Project Guidance / Re: Trouble using analogRead when ethernet shield is plugged into the arduino on: July 19, 2012, 05:31:16 am
I tried it on two of the pins and it happened on both of them.  I have the divider set up, with a 100K resistor from the arduino 5V pin to A1, and a thermistor connected from A1 to the arduino ground pin.  When I connect it this way directly to the arduino without the shield I measure 4.97 volts across the 5V and ground pins, 330mv across the 100K resistor and 4.64V across the thermistor.  When I use the exact same configuration, except I connect it to the arduino through the ethernet shield I get 4.97V across the 5V and ground pins, 30mv across the 100K resistor and 4.94V across the thermistor.  So it's not really the ADC that is the problem.
254  Using Arduino / Project Guidance / Trouble using analogRead when ethernet shield is plugged into the arduino on: July 17, 2012, 08:31:13 pm
Is there something about the ethernet shield that would cause a problem with the ADC on the mega328?  I made a small voltage divider that incorporates a 100K resister and a thermistor and when it is plugged into the arduino directly it works fine but when it is plugged into the arduino through the ethernet shield I get values that are completely off.  Can anyone shed any light on this for me? Sketch is below.

Code:


/*
 * BBQControl.c
 *
 * Created: 8/7/2011 12:34:32 PM
 *  Author: Saleem and Leslie
 */

 

#define LCD_Command_A 0x7C
#define LCD_Command_B 0xFE
#define LCD_Clear_Screen 0x01
#define LCD_Cursor_Position(a) {Serial.print(LCD_Command_B,BYTE); Serial.print(a+128,BYTE);}
#define chipSelect 10
#define thermPin A1

#include <math.h>
#include <stdint.h>
#include <PID_v1.h>
#include <stdio.h>
#include <string.h>

//PID Variables
double Input = 0;  //Our Current Temperature
double Output = 0;  //Our Control Variable
double Setpoint = 0;  //Our Setpoint
double Kp = 10;      //Proportional
double Ki = .1;      //Integral
double Kd = 50;      //Derivative

PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
 
void setup()
{
pinMode(thermPin,INPUT);  //Thermistor is connected to A1
Serial.begin(9600);
        myPID.SetMode(AUTOMATIC);  //start PID
}       



void loop()
{
    uint16_t ADCVal;
    uint16_t temperature;
   
    //String dataString = "";
   
    //ADCVal = analogRead(thermPin);
   
    //temperature = (get_temperature(ADCVal));  //get temperature conversion
   
    for(int i = 0;i<100;i++)
    {
      ADCVal = analogRead(thermPin);
      temperature += (get_temperature(ADCVal));  //add 99 more conversions
    }
   
    temperature = temperature/100;  //average them
    Input = (double)temperature;    //load new temperature into our PID Algorithm
    myPID.Compute();                //compute new output value
    //Serial.println(Input);          //print output to out monitor
    //Serial.println(Output);
   
   if(Serial.available())   //check to see if the user has sent
                                   //any new parameters
    {
      char myString[40];   //buffer to hold my received string
      char *str;           //pointer for strtok function
      receive_serial(&myString[0]);  //get serial data
      //Serial.println(myString);      //debugging
      double PID_Params[4];          //array to hold my new parameters
      double *my_params = PID_Params;
      str = strtok (myString,",");   //split mystring into 4 pieces using comma delimiter, reference:
      while (str != NULL)            //http://www.cplusplus.com/reference/clibrary/cstring/strtok/
      {
        *my_params = atof(str);    //convert strings segments to doubles
        my_params++;
        //Serial.println(str);
        str = strtok (NULL, ",");  //strtok expects a NULL pointer for all
      }                            //subsequent calls after the first call
      Setpoint = PID_Params[0];
      myPID.SetTunings(PID_Params[1],PID_Params[2],PID_Params[3]);
     
     
    }
   
    //Send all of the data to our monitor in comma delimited format
    Serial.print(temperature);   
    Serial.print(",");
    Serial.print(ADCVal);
    Serial.print(",");
    Serial.print(Setpoint,0);
    Serial.print(",");
    Serial.print(myPID.GetKp(),4);
    Serial.print(",");
    Serial.print(myPID.GetKi(),4);
    Serial.print(",");
    Serial.println(myPID.GetKd(),4);
   
    delay(100);
}

void receive_serial(char *String)
{
  int i = Serial.available(); //Serial.available returns number of bytes
  delay(10);                  //in the Serial buffer
 
  //wait to make sure that all bytes have been received
  while(i != Serial.available())  //if i does not equal Serial.available then
  {                              //that means we are still receiving bytes.
    i = Serial.available();
    delay(10);
  }
 
  //Read serial data into address pointed to by String
  while(Serial.available())
  {
    *String = Serial.read();
    String++;
    *String = '\0';
  }

}



void get_parameters(uint16_t ADCvalue, uint16_t *beta, float *r_infinity)
{

if      (ADCvalue < 15)   {*beta = 5191; *r_infinity=0.07028;}
else if (ADCvalue <= 39)  {*beta = 5085; *r_infinity=0.08597;}
else if (ADCvalue <= 158) {*beta = 4942; *r_infinity=0.11637;}
else if (ADCvalue <= 530) {*beta = 4767; *r_infinity=0.17766;}
else if (ADCvalue <= 577) {*beta = 4671; *r_infinity=0.23249;}
else if (ADCvalue <= 670) {*beta = 4641; *r_infinity=0.25289;}
else if (ADCvalue <= 757) {*beta = 4604; *r_infinity=0.28168;}
else if (ADCvalue <= 832) {*beta = 4566; *r_infinity=0.31618;}
else if (ADCvalue <= 892) {*beta = 4526; *r_infinity=0.35779;}
else if (ADCvalue <= 938) {*beta = 4485; *r_infinity=0.40755;}
else if (ADCvalue <= 1023){*beta = 4453; *r_infinity=0.45243;}
return;
}


//*************Hardware SetUp***************
//
//     ADC Input
//        |
//        Rpad    |  Rtherm
// Vcc--------/\/\/\------/\/\/\------ground
//
//******************************************


float get_temperature(uint16_t ADCVal){
//temperature in kelvin = beta/ln(R/Rinfinity)
uint16_t beta = 0;
        uint8_t servo_command;
float Ri = 0;
float Rpad = 100000;
float Rtherm = Rpad/(1-((float)ADCVal/1024.0)) - 100000;
float Temperature;
float t;
//Serial.println(Rtherm);
get_parameters(ADCVal, &beta, &Ri);

t=beta/log(Rtherm/Ri);
t = ((9.0/5.0)*(t-273))+32;
Temperature = t;


//Out to LCD
//LCD_Cursor_Position(72);
//Serial.print(ADCVal);
        //Serial.print("    ");
//LCD_Cursor_Position(28);
        //Serial.print(beta);
        //Serial.print("    ");
//LCD_Cursor_Position(8);
return Temperature;

}





255  Using Arduino / Installation & Troubleshooting / Re: .hex file path on: July 17, 2012, 08:19:31 pm
I had to "make visible" all folders and files to find the .hex file when I did what you are doing.  For some reason on my Mac it was putting the files in a hidden folder.
Pages: 1 ... 15 16 [17] 18 19 20