Show Posts
Pages: 1 2 [3] 4 5 ... 13
31  Using Arduino / Project Guidance / Re: heat and cool with programmable profiles. on: April 03, 2013, 04:29:46 pm
If you have your arduino connected to your computer you can use the serial monitor to debug and see what is being read and what the output is.  You can use Serial.println() to print it to your serial monitor.
32  Using Arduino / Project Guidance / Re: heat and cool with programmable profiles. on: April 03, 2013, 04:23:42 pm
Don't add anything until you get the PID working, You'll thank me later.  What I means is get your PID constants ironed out first before continuing.  But yah, you should be able to add whatever you want to it.
33  Using Arduino / Project Guidance / Re: Little problem with gobetwino on: April 03, 2013, 04:22:10 pm
No that is perfect and what I was expecting.  The process ID for your matlab process is equal to 1.  That is where the 1 is coming from.  What you need to do is clear the serial buffer before you try to read lines from the output.txt.  Also you had an extra Serial.begin in there and that's not necissary.

Code:
int serInLen = 25;
int ledGreen = 8; // Groene led op pin 8
int ledRed = 4;  // Rode led op pin 4
char serInString[25];

void setup()                   
{
  pinMode(ledGreen, OUTPUT); 
  pinMode(ledRed, OUTPUT);
  Serial.begin(9600);
  Serial.println("#S|OPENCAMERA|[]#");
  delay(5000);
}

void loop()     
 
{
  char buffer[5];  // long data type has 11 characters (including the minus sign) and a terminating null
  int nrOfBlinks;
  int lineNr;
 
       
      /*
       Your buffer must be large enough to hold the maximum number of characters in the string.
       For 16-bit base 10 (decimal) integers, that is seven characters (five digits, a possible minus sign, and a terminating 0 that always signifies the end of a string);
       32-bit long integers need 12 character buffers (10 digits, the minus sign, and the terminating 0).
       No warning is given if you exceed the buffer size; this is a bug that can cause all kinds of strange symptoms,
       because the overflow will corrupt some other part of memory that may be used by your program.
       The easiest way to handle this is to always use a 12-character buffer and always use ltoa because this will work on both 16-bit and 32-bit values.

      */
        Serial.println("#S|OPNMATLAB|[]#");
        delay(15000);
       
        for(lineNr=1;lineNr<=3;lineNr++) {
        clearSerialBuffer();
        Serial.print("#S|LEESWITHE|[");           //Leest de waarden binnen van matlab
        Serial.print(itoa((lineNr), buffer, 10)); //Converteert de integer naar een string en steekt het resultaat in een array
        Serial.println("]#");                     //Want er kunnen enkel string-waarden doorgegeven worden naar Gobetwino
        readSerialString (serInString, 10000);
        nrOfBlinks = atoi(serInString);    //Converteert een sting naar een integer
         
        delay(2000);
        Serial.print("#S|LOGTEST|[");
        //Serial.print(itoa((nrOfBlinks), buffer, 10));
        Serial.print(serInString);
        Serial.println("]#");
     
         
         //if (nrOfBlinks >= 50)
        //{
        //digitalWrite(ledGreen, HIGH);
        //digitalWrite(ledRed, LOW);     
        //}
       
        //else if (nrOfBlinks < 50)
        //{
        //digitalWrite(ledGreen, LOW);
        //digitalWrite(ledRed, HIGH); 
        //}
       }
       delay(15000);
}

void clearSerialBuffer(){
    char dummy;
    while (Serial.available()  {  //Also check for timeout here, also leave space for '\0' at the end of your C-String
      dummy = Serial.read();
      delay(10); //slight delay to allow the buffer to fill up
   }
}


void readSerialString (char *strArray,long timeOut) {
  // Wait up to timeOut miliseconds for data to arrive at the serial port, then read the data and put it in the char array strArray
   long startTime=millis();
   int i = 0;

   while (!Serial.available()) {
      if (millis()-startTime >= timeOut) {
         return;
      }
   }
    while (Serial.available() && i < (serInLen-1) && (millis()-startTime) < timeOut) {  //Also check for timeout here, also leave space for '\0' at the end of your C-String
      strArray[i] = Serial.read();
      i++;
      delay(10); //slight delay to allow the buffer to fill up
   }
   strArray[i]='\0';
   
}
34  Using Arduino / Project Guidance / Re: heat and cool with programmable profiles. on: April 03, 2013, 03:44:29 pm
I'm doing this in a text editor.  I apologize for any bugs.
35  Using Arduino / Project Guidance / Re: heat and cool with programmable profiles. on: April 03, 2013, 03:32:04 pm
This should combine my code with your getCurrentTemperature function.
Code:
#include <OneWire.h>
#include <PID_v1.h>
#define heatingPin 2
#define coolingPin 12
int RelayPin = 0;
const double RoomTemp = 22;
double heatingPID[] = {2,5,1};  //This needs to be tuned
double coolingPID[] = {3,6,4};  //This needs to be tuned
double Setpoint, Input, Output;
PID myPID(&Input, &Output, &Setpoint,heatingPID[0],heatingPID[1],heatingPID[2],DIRECT);
double temps[]={50,28,31};  //Set Point temperatures are 50C 28C and 31C
unsigned long times[]={120000,60000,120000};  //Durations are 2min, 1min, 2min
unsigned long setPointStartTime;
unsigned long duration = 0;
int direction = 0;
int index = 0;
int WindowSize = 5000;
unsigned long windowStartTime;

const int TEMP_SENSOR_PIN = 3;
OneWire ds(TEMP_SENSOR_PIN);

void setup(){
  pinMode( TEMP_SENSOR_PIN, INPUT );
  pinMode( coolingPin, OUTPUT);
  pinMode( heatingPin, OUTPUT );
  digitalWrite( coolingPin, LOW );      // ensure initial state is inactive
  digitalWrite( heatingPin, LOW );
  windowStartTime = millis();
  Setpoint = temps[index];

  //tell the PID to range between 0 and the full window size
  myPID.SetOutputLimits(0, WindowSize);
  //turn the PID on
  myPID.SetMode(AUTOMATIC);
  setDirection(temps[index], getCurrentTemperature());
}
void loop(){
  Input = getCurrentTemperature();
  if((Input - Setpoint)*direction>0 && duration == 0){  //You have reached the setpoint
    duration = times[index];
    setPointStartTime = millis();
    //setDirection(SetPoint, RoomTemp);
  }else if(duration > 0 && (millis()-setPointStartTime)>duration){
    duration = 0;
    if(index<2){
      index++;
      Setpoint = temps[index];
      //setDirection(SetPoint, temps[index - 1]);
    }
  }
  setDirection(Setpoint, Input);  
  myPID.Compute();
  unsigned long now = millis();
  if(now - windowStartTime>WindowSize)
  { //time to shift the Relay Window
    windowStartTime += WindowSize;
  }
  if(Output > now - windowStartTime){
    digitalWrite(RelayPin,HIGH);
  }else{
    digitalWrite(RelayPin,LOW);
  }
}
void setDirection(double tempA, double tempB){
  if(tempA>tempB){
    if(direction < 1){
      digitalWrite(coolingPin,LOW);  //Turn of Cooling
      direction = 1;
      RelayPin = heatingPin;
      myPID.SetControllerDirection(DIRECT);
      myPID.SetTunings(heatingPID[0] , heatingPID[1] , heatingPID[2]);
    }
  }else{
    if(direction > -1){
      digitalWrite(heatingPin,LOW);  //Turn of Heating
      direction = -1;
      RelayPin = coolingPin;
      myPID.SetControllerDirection(REVERSE);
      myPID.SetTunings(coolingPID[0] , coolingPID[1] , coolingPID[2]);
    }
  }
}
double getCurrentTemperature() {
  int highByteData, lowByteData, sensorReading;
  byte data[12];
  float temperature;
  
  ds.reset();
  ds.select(addr);
  ds.write(0x44,1);
  
  byte present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);

  for (int i = 0; i < 12; i++) {
    data[i] = ds.read();
  }
  
  lowByteData = data[0];
  highByteData = data[1];
  sensorReading = (highByteData << 8) + lowByteData;
  
  if (sensorReading & 0x8000) { // test the most significant bit for sign
    sensorReading = -sensorReading;
  }
  temperature = sensorReading * 0.0625; // minimum quantised reading from temp sensor DS18B20
  
  return (double)temperature;  
}
36  Using Arduino / Project Guidance / Re: Little problem with gobetwino on: April 03, 2013, 03:27:25 pm
I'm gonna take a stab at this.  The gobetwino is spitting out a '0' when the command is executed correctly and this is getting read into your serial string.  So what is getting sent to gobetwino is

"01"
"09"
"0134"
"05601"

Which ends up being interpreted as
"1"
"9"
"134"
"5601"

It would help if next time you just copied and pasted your output from the gobetwino here for one loop of commands in code tags.  So copy the status messages and command output for executing the matlab command and the reading of 3 lines and logging the three lines.
37  Using Arduino / Project Guidance / Re: Little problem with gobetwino on: April 03, 2013, 03:07:09 pm
This is his order of operations
  -arduino executes Matlab through gobetwino
  -Matlab writes output.txt
  -Arduino reads output.txt through gobetwino
  -Arduino ouputs logtest.txt through gobetwino
38  Using Arduino / Project Guidance / Re: Little problem with gobetwino on: April 03, 2013, 08:14:31 am
oh an make sure to initialize your index variable i to 0
Code:
int i = 0;

   while (!Serial.available()) {
      if (millis()-startTime >= timeOut) {
         return;
      }
   }
   while (Serial.available() && i < (serInLen-1) && (millis()-startTime) < timeOut) {  //Also check for timeout here, also leave space for '\0' at the end of your C-String
      strArray[i] = Serial.read();
      i++;
      delay(10); //slight delay to allow the buffer to fill up
   }
   strArray[i] = '/0'
39  Using Arduino / Project Guidance / Re: Trying to trigger an iPhone's touch screen electronically. on: April 03, 2013, 07:42:15 am
Just as a test I sandwiched a stylus inside a book, to give it weight and rigidity, and leaned the stylus onto my tablet.  The tablet had no problem registering a touch event.
40  Using Arduino / Project Guidance / Re: Little problem with gobetwino on: April 03, 2013, 07:39:43 am
Yeah sorry, apparently I missed to code tags, rookie move.  You wan't the character array value right after the last character to be zero.  It should be

Code:
while (Serial.available() && i < serInLen) {
      strArray[i] = Serial.read();
      i++;
   }
   strArray[i]='\0';
41  Using Arduino / Project Guidance / Re: heat and cool with programmable profiles. on: April 02, 2013, 05:29:14 pm
Ok I fixed it for you.  You just need to make sure you have the PID library in the right spot and add the getCurrentTemperature() function.

Code:
#include <PID_v1.h>
#define heatingPin 2
#define coolingPin 12
int RelayPin = 0;
const double RoomTemp = 22;
double heatingPID[] = {2,5,1};  //This needs to be tuned
double coolingPID[] = {3,6,4};  //This needs to be tuned
double Setpoint, Input, Output;
PID myPID(&Input, &Output, &Setpoint,heatingPID[0],heatingPID[1],heatingPID[2],DIRECT);
double temps[]={50,28,31};  //Set Point temperatures are 50C 28C and 31C
unsigned long times[]={120000,60000,120000};  //Durations are 2min, 1min, 2min
unsigned long setPointStartTime;
unsigned long duration = 0;
int direction = 0;
int index = 0;
int WindowSize = 5000;
unsigned long windowStartTime;
void setup(){
  pinMode( coolingPin, OUTPUT);
  pinMode( heatingPin, OUTPUT );
  digitalWrite( coolingPin, LOW );      // ensure initial state is inactive
  digitalWrite( heatingPin, LOW );
  windowStartTime = millis();
  Setpoint = temps[index];

  //tell the PID to range between 0 and the full window size
  myPID.SetOutputLimits(0, WindowSize);
  //turn the PID on
  myPID.SetMode(AUTOMATIC);
  setDirection(temps[index], getCurrentTemperature());
}
void loop(){
  Input = getCurrentTemperature();
  if((Input - Setpoint)*direction>0 && duration == 0){  //You have reached the setpoint
    duration = times[index];
    setPointStartTime = millis();
    //setDirection(SetPoint, RoomTemp);
  }else if(duration > 0 && (millis()-setPointStartTime)>duration){
    duration = 0;
    if(index<2){
      index++;
      Setpoint = temps[index];
      //setDirection(SetPoint, temps[index - 1]);
    }
  }
  setDirection(Setpoint, Input); 
  myPID.Compute();
  unsigned long now = millis();
  if(now - windowStartTime>WindowSize)
  { //time to shift the Relay Window
    windowStartTime += WindowSize;
  }
  if(Output > now - windowStartTime){
    digitalWrite(RelayPin,HIGH);
  }else{
    digitalWrite(RelayPin,LOW);
  }
}
void setDirection(double tempA, double tempB){
  if(tempA>tempB){
    if(direction < 1){
      digitalWrite(coolingPin,LOW);  //Turn of Cooling
      direction = 1;
      RelayPin = heatingPin;
      myPID.SetControllerDirection(DIRECT);
      myPID.SetTunings(heatingPID[0] , heatingPID[1] , heatingPID[2]);
    }
  }else{
    if(direction > -1){
      digitalWrite(heatingPin,LOW);  //Turn of Heating
      direction = -1;
      RelayPin = coolingPin;
      myPID.SetControllerDirection(REVERSE);
      myPID.SetTunings(coolingPID[0] , coolingPID[1] , coolingPID[2]);
    }
  }
}
42  Using Arduino / Project Guidance / Re: heat and cool with programmable profiles. on: April 02, 2013, 05:19:50 pm
I didn't actually check the code in a compiler so there may be errors to fix.  I tried to make sure it was as consistent as possible.
43  Using Arduino / Project Guidance / Re: Little problem with gobetwino on: April 02, 2013, 05:00:49 pm
Lets first debug that you are getting the correct data from the ".txt" file first.  Then we can know if it is the character to integer conversion that is fudging things up.  Also make sure to null terminate your string otherwise it isn't a real C-String.


Code:
int serInLen = 25;
int ledGreen = 8; // Groene led op pin 8
int ledRed = 4;  // Rode led op pin 4
char serInString[25];

void setup()                   
{
  pinMode(ledGreen, OUTPUT); 
  pinMode(ledRed, OUTPUT);
  Serial.begin(9600);
  Serial.println("#S|OPENCAMERA|[]#");
  delay(5000);
}

void loop()     
 
{
  char buffer[5];  // long data type has 11 characters (including the minus sign) and a terminating null
  int nrOfBlinks;
  int lineNr;
 
       
      /*
       Your buffer must be large enough to hold the maximum number of characters in the string.
       For 16-bit base 10 (decimal) integers, that is seven characters (five digits, a possible minus sign, and a terminating 0 that always signifies the end of a string);
       32-bit long integers need 12 character buffers (10 digits, the minus sign, and the terminating 0).
       No warning is given if you exceed the buffer size; this is a bug that can cause all kinds of strange symptoms,
       because the overflow will corrupt some other part of memory that may be used by your program.
       The easiest way to handle this is to always use a 12-character buffer and always use ltoa because this will work on both 16-bit and 32-bit values.

      */
        Serial.println("#S|OPNMATLAB|[]#");
        delay(15000);
       
        for(lineNr=1;lineNr<=3;lineNr++) {
        Serial.print("#S|LEESWITHE|[");           //Leest de waarden binnen van matlab
        Serial.print(itoa((lineNr), buffer, 10)); //Converteert de integer naar een string en steekt het resultaat in een array
        Serial.println("]#");                     //Want er kunnen enkel string-waarden doorgegeven worden naar Gobetwino
        readSerialString (serInString, 10000);   
        nrOfBlinks = atoi(serInString);    //Converteert een sting naar een integer
         
        delay(2000);
        Serial.begin(9600);
        Serial.print("#S|LOGTEST|[");
        //Serial.print(itoa((nrOfBlinks), buffer, 10));
        Serial.print(serInString);
        Serial.println("]#");
     
         
         //if (nrOfBlinks >= 50)
        //{
        //digitalWrite(ledGreen, HIGH);
        //digitalWrite(ledRed, LOW);     
        //}
       
        //else if (nrOfBlinks < 50)
        //{
        //digitalWrite(ledGreen, LOW);
        //digitalWrite(ledRed, HIGH); 
        /}
       }
       delay(15000);
}


void readSerialString (char *strArray,long timeOut) {
  // Wait up to timeOut miliseconds for data to arrive at the serial port, then read the data and put it in the char array strArray
   long startTime=millis();
   int i;

   while (!Serial.available()) {
      if (millis()-startTime >= timeOut) {
         return;
      }
   }
   while (Serial.available() && i < serInLen) {
      strArray[i] = Serial.read();
      i++;
   }
   strArray[i]='\0';
}
44  Using Arduino / Project Guidance / Re: Spectrum Analyzer - new to hardware - on: April 02, 2013, 03:39:20 pm
Start here for FTT information
http://www.arduinoos.com/2010/10/fast-fourier-transform-fft/

I wouldn't worry about the display of the data just yet.  Start with just getting the FFT to work and spitting it out over serial.  Also note that most of the articles that you read will be dealing with static data.  Your project is going to be working with real time data.

So you'll need to play around with how you are going to deal with that. 
Method One:
  -Fill array with completely new data
  -Perform FFT on array
  -repeat

Method Two:
  -Push one new data point into your array an pop off the oldest data point
  -Perform FFT on the array

Note that most FFT algorithms need a data set that is a power of 2 in size.
45  Using Arduino / Project Guidance / Re: Trying to trigger an iPhone's touch screen electronically. on: April 02, 2013, 03:14:39 pm
I am envisioning having the stylus ground completely separated from the electronics ground.  It could even be connected to the iphone case, or even better a large metalic cradle that is holding the iphone.  It's a completely different concept than what he has here, but might be much easier to implement and get working.
Pages: 1 2 [3] 4 5 ... 13