Show Posts
Pages: 1 2 [3] 4 5 ... 16
31  Using Arduino / Programming Questions / Re: How do I stop an Arduino sketch? on: October 29, 2012, 10:33:23 am
you can do what you want in the "Setup" if you want to do it only once.
32  Using Arduino / Programming Questions / 1 wire and 4 wire communications confilict on: October 29, 2012, 09:09:02 am
Do anyone knows if there is a conflict using 1wire and 4wire communications at the same time? I have a program that tries to use an SD card to write on a sensor value taken from the dht22 that works with the 1 wire protocol. I haven't conflicts between the sd pins an the sensor pin. But after the "SD.begin" the sensor does not answer to arduino (dht22 start condition 1 not met). Without SD begin everything is ok.

This is the code:
Code:
#include "dht22.h"
#include <SD.h>


dht22 mioTwigTempHum(A2);

void setup()
{
  Serial.begin(9600);
 
 
     //inizializzo la parte della scheda SD                SETUP->SD
   Serial.println("Initializing SD card...");
   // disable w5100 SPI while setting up SD
  pinMode(10,OUTPUT);
  digitalWrite(10, HIGH);//per arduino mega http://arduino.cc/forum/index.php/topic,46976.0.html
 
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
   pinMode(53, OUTPUT);
   digitalWrite(53, HIGH);
   
  if (!SD.begin(4)) {
    Serial.println("non potra' salvare su SD!");
   
  }
}


void loop()
{
  mioTwigTempHum.aggiornaValori();
  Serial.println(mioTwigTempHum.umidita );
  Serial.println(mioTwigTempHum.temperatura );
  delay(1000); // wait for one second
}

and this is the sensor library
.H
Code:
#ifndef dht22_h
#define dht22_h


class dht22
{
private:
int analogInArduino;     //pin arduino dove e' connesso il sensore
unsigned char read_dht22_dat();

public:
dht22(int analogInArduino); //costruttore   
~dht22( ); //distruttore

    bool aggiornaValori(); //aggiorna le variabili umidita e temperatura
    float umidita;
    float temperatura;
};




#endif


.CPP
Code:

/******************************************************************************
 * Includes
 ******************************************************************************/

#include "dht22.h"
  #if defined(ARDUINO) && ARDUINO >= 100
  #include "Arduino.h"
  #else
  #include "WProgram.h"
  #endif //Arduino includes

/******************************************************************************
 * Definitions
 ******************************************************************************/

/******************************************************************************
 * Constructors
 ******************************************************************************/

dht22::dht22(int analogInArduino)
{
   this->analogInArduino=analogInArduino;
   pinMode(analogInArduino,OUTPUT);
}


dht22::~dht22( )
{
}


/******************************************************************************
 * User API
 ******************************************************************************/



bool dht22::aggiornaValori()
{
byte dht22_dat[5];
byte i;


  // start condition
  // 1. pull-down i/o pin from 18ms
  digitalWrite(analogInArduino,LOW);
  delay(18);
 
  digitalWrite(analogInArduino,HIGH);
  delayMicroseconds(40);

      pinMode(analogInArduino,INPUT);


  if(digitalRead(analogInArduino)){
    Serial.println("dht22 start condition 1 not met");
    return(false);
  }
  delayMicroseconds(80);



  if(!digitalRead(analogInArduino)){
    Serial.println("dht22 start condition 2 not met");
    return(false);
  }
  delayMicroseconds(80);
  // now ready for data reception
  for (i=0; i<5; i++)
    dht22_dat[i] = read_dht22_dat();

  pinMode(analogInArduino,OUTPUT);
  digitalWrite(analogInArduino,HIGH);

  byte dht22_check_sum = dht22_dat[0]+dht22_dat[1]+dht22_dat[2]+dht22_dat[3];
  // check check_sum
  if(dht22_dat[4]!= dht22_check_sum)
  {
  Serial.println("DHT22 checksum error");
return(false);
  }
  umidita=((float)(dht22_dat[0]*256+dht22_dat[1]))/10;
  temperatura=((float)(dht22_dat[2]*256+dht22_dat[3]))/10;
  return(true);
}


unsigned char dht22::read_dht22_dat()
{
  byte i = 0;
  byte result=0;
  for(i=0; i< 8; i++){
    while(!digitalRead(analogInArduino));  // wait for 50us
    delayMicroseconds(30);
 
    if(digitalRead(analogInArduino))
      result |=(1<<(7-i));
    while(digitalRead(analogInArduino));  // wait '1' finish
  }
  return result;
}


I have tested this on the arduino 2560 and the duemilanove. For the sd I have used the ethernet shield and the wireless sd shiled.
Do you have any idea of the reason of the conflicts considering that pins are not in conflicts?
Thanks in advance
33  Using Arduino / Programming Questions / Re: Seeedstudio temperature and humidity code on: October 26, 2012, 08:49:00 am
Ok, I but why? pin 14 for A0?
34  Using Arduino / Programming Questions / Seeedstudio temperature and humidity code on: October 26, 2012, 05:22:12 am
Hello. I have dowloaded this code from seeedstudio:
Quote
/*
 * Version 1.0 April, 2012
 * Copyright 2012 Joinj
 * http://www.seeedstudio.com
 */
#define DHT11_PIN 14      // ADC0
 
byte read_dht11_dat()
{
  byte i = 0;
  byte result=0;
  for(i=0; i< 8; i++){
    while(!digitalRead(DHT11_PIN));  // wait for 50us
    delayMicroseconds(30);
 
    if(digitalRead(DHT11_PIN))
      result |=(1<<(7-i));
    while(digitalRead(DHT11_PIN));  // wait '1' finish
  }
  return result;
}
void setup()
{
  pinMode(DHT11_PIN,OUTPUT);
  Serial.begin(9600);
  Serial.println("Ready");
}
 
void loop()
{
  byte dht11_dat[5];
  byte dht11_in;
  byte i;
  float humdity,temperature;
  // start condition
  // 1. pull-down i/o pin from 18ms
  digitalWrite(DHT11_PIN,LOW);
  delay(18);
 
  digitalWrite(DHT11_PIN,HIGH);
  delayMicroseconds(40);
 
  pinMode(DHT11_PIN,INPUT);
 
  while(digitalRead(DHT11_PIN)){
    Serial.println("dht11 start condition 1 not met");
  }
  delayMicroseconds(80);
 
  while(!digitalRead(DHT11_PIN)){
    Serial.println("dht11 start condition 2 not met");
  }
  delayMicroseconds(80);
  // now ready for data reception
  for (i=0; i<5; i++)
    dht11_dat = read_dht11_dat();
 
  pinMode(DHT11_PIN,OUTPUT);
  digitalWrite(DHT11_PIN,HIGH);
 
  byte dht11_check_sum = dht11_dat[0]+dht11_dat[1]+dht11_dat[2]+dht11_dat[3];
  // check check_sum
  if(dht11_dat[4]!= dht11_check_sum)
  {
    Serial.println("DHT11 checksum error");
  }
  humdity=((float)(dht11_dat[0]*256+dht11_dat[1]))/10;
  temperature=((float)(dht11_dat[2]*256+dht11_dat[3]))/10;
  Serial.print("Current humdity = ");
  Serial.print(humdity,1);
  Serial.print("%  ");
  Serial.print("temperature = ");
  Serial.print(temperature,1);
  Serial.println("C  ");
  delay(2000);
}

And it works perfectly with my arduino duemilanove. My question is: whi they declare #define DHT11_PIN 14  for analog pin 0 and not #define DHT11_PIN 0?
The same code does not work on a mega. Is it possible that on the uno the pin A0 has many names?
35  Using Arduino / Networking, Protocols, and Devices / Re: mega 2560 and SPI on: October 24, 2012, 04:41:02 am
thanks! Do you know if there is a way with to use also the i2c with a shiled made for the UNO? (I know on the uno are pin 4 and 5 for the mega 20 and 21)
36  Using Arduino / Networking, Protocols, and Devices / mega 2560 and SPI on: October 24, 2012, 03:27:51 am
I am using th eethernet shiled on the mega without problems. But reading the documentation I get surprised that things work. If the SPI on the mega is on pins  50 (MISO), 51 (MOSI), 52 (SCK) and 53 (SS) how is possible that we can use one shield made for the UNO SPI: 10 (SS), 11 (MOSI), 12 (MISO), 13 (SCK)?
37  Using Arduino / Programming Questions / Re: programming error...Need Help!! on: October 24, 2012, 03:12:42 am
have you put libraries in the right place?
38  Using Arduino / Programming Questions / Re: UNO vs Mega2560 on: October 24, 2012, 02:36:36 am
If you are using an Arduino Mega, SDA is pin 20 and SCL is 21, so note that shields with I2C need to be specifically for the Mega (taken from http://tronixstuff.wordpress.com/2010/10/20/tutorial-arduino-and-the-i2c-bus/)
39  Using Arduino / Programming Questions / Re: SD.begin fails after the mircoSd is written & arduino resetted on: October 24, 2012, 02:34:26 am
exactly. The problem only happens if in order:

1)I write on the txt file
2)I Leave the file written (without canceling an recreating it empty)
3)the sd is not phisically removed
4)The arduino reset

After all this things in the new setup() the card is not recognisez.
In all other case (I don't reset, I phisically remove the sd, I cancel the file before resetting, ecc...) everything works without problems
40  Using Arduino / Programming Questions / Re: SD.begin fails after the mircoSd is written & arduino resetted on: October 24, 2012, 02:08:36 am
Quote
Please explain exactly what you mean by 'cancel'

With 'cancel the file' I mean "SD.remove("valori.txt");"
41  Using Arduino / Programming Questions / Re: SD.begin fails after the mircoSd is written & arduino resetted on: October 23, 2012, 09:08:28 am
Quote
2) I don't cancel the files
Do you mean that you don't close the file?

No, I really  mean cancel. I always close the file once I have written o read. If I cancel it an re-create before resetting it works. If I write on it and reset (leaving phisically the card inside the slot) the SD.begin in the setup fails
42  Using Arduino / Programming Questions / SD.begin fails after the mircoSd is written & arduino resetted on: October 23, 2012, 08:37:40 am
Hello. I have a problem with my microSD card used with an ethernet shiled and a mega 2560.
The problem is that SD.begin(pin) in the setup fails if:
1) I write the card 2) I don't cancel the files 3) I reset arduino
If these 3 things do not happen everything works fine (open, write, cancel, create ecc..) for example if i write on a file, cancel it and reset arduino the sdBegin succeed.
If I read from a computer the file written on the SD it always looks like to be correct.
This is how I initialize the sd:
Code:
  //initialize SD              SETUP->SD
   Serial1.println("Initializing SD card...");
   // disable w5100 SPI while setting up SD
   pinMode(10,OUTPUT);
  digitalWrite(10, HIGH);//for arduino mega http://arduino.cc/forum/index.php/topic,46976.0.html
  
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
   pinMode(53, OUTPUT);
   digitalWrite(53, HIGH);
  
  if (!SD.begin(4)) {
    Serial1.println("cant save on SD!");
    noSd=true;
  }
  else{
    Serial1.println(F("SD inizialized"));
    
    // open the file. note that only one file can be open at a time,
    // so you have to close this one before opening another.
    mioFile = SD.open("valori.txt", FILE_WRITE);  
    if (mioFile) {
      Serial1.print("file opened");
      if(mioFile.available()>0 )
        sdScritta=true;
      mioFile.close();
      Serial1.println("done.");
    } else {
      // if the file didn't open, print an error:
      Serial1.println(F("error opening valori.txt"));
    }

  }



this is where I write:

Code:
     mioFile = SD.open("valori.txt", FILE_WRITE);
      if (mioFile) {
        Serial1.println(perServer);    
        mioFile.println(perServer);
// close the file:
        mioFile.close();
        sdScritta=true;
        Serial1.println("done.");

and here I read one line

Code:
       while (mioFile.peek()!='\r' && mioFile.peek()!=EOF)
        {
          stringaTemp+=(char)(mioFile.read());
        }
        Serial1.write(mioFile.read());//cattura il carattere di a capo
        Serial1.write(mioFile.read());
        Serial1.println(stringaTemp);
        Serial1.println("fine di una riga");


and here I cancel the file

Code:

    if(mioFile.peek()==-1){
      Serial1.println("cancel old file");
      mioFile.close();
      SD.remove("valori.txt");
      mioFile = SD.open("valori.txt", FILE_WRITE);    //create new one
      sdScritta=false;
      nRiga=0;
  }

If I phisycally eject the SD,put it inside again with the same file written and only after I reset it works!
Do you know if anything is wrong?
Thanks in advance for the time you will spent
43  International / Software / Re: arduino si riavvia da solo ... poca ram? on: October 23, 2012, 07:42:01 am
per capire se davvero รจ la ram a mancare usaq questa funzione:

int freeRam () {
  extern int __heap_start, *__brkval;
  int v;
  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
44  Using Arduino / Programming Questions / Re: Program flow question on: October 22, 2012, 08:23:50 am
Quote
Should I be using a completley different approach?
yes you can. i suggest you to read http://www.google.it/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&ved=0CDYQFjAB&url=http%3A%2F%2Fciteseerx.ist.psu.edu%2Fviewdoc%2Fdownload%3Fdoi%3D10.1.1.88.9025%26rep%3Drep1%26type%3Dpdf&ei=kkiFULGPB4vMsgbN9YHIAQ&usg=AFQjCNHpg7FOkZg9-3JO1VNAFOSxIhcnOg&sig2=HwQhSKvap7BtM_SRr6AINg&cad=rja
45  Using Arduino / Programming Questions / Re: how ethernt library methods work on: October 22, 2012, 08:01:55 am
thank you SurferTim.
Pages: 1 2 [3] 4 5 ... 16