December 06, 2024, 06:17:38 AM

Bug with external power

Started by jujulot, May 06, 2015, 12:00:43 PM

Previous topic - Next topic

jujulot

hello everybody ,

i have a olimexino 328 with the  Wireless SD shield (http://www.arduino.cc/en/Main/ArduinoWirelessShield).
on the shield  i use the SD card to log data that came from 4 max31850 ( https://learn.adafruit.com/adafruit-1-wire-thermocouple-amplifier-max31850k/wiring-and-test) and xbee to send data to my computer.

i have made my code with an Arduino Uno , i tested it with external powering and everything was good.

but when i replace my Uno with the olimex 328 i have a problem :
my program is not working and the led on the wireless shield that indicate Tx and RX are lighting at the same time which is not correct (i don't use receive ans send at the same time). i have to reset 2 or 3 times and then it's starting well and everything is allright.

this case is when i use external power (12 volts from good regulator ( i think so))

what i don't understand is when i use USB power everything is good at each time, the olimexino is starting well and my code is ok.

if someone have a idea of where is my mistake taht would be very great...

thank to all


/* Code arduino 2015
author : Julien Marro-Dauzat , Tommaso Passerin d'Entreves
systems(at)guimbal.com
algorithme acquisition thermocouples, transmission des datas vers systeme regulation labview:
  -acquisition 4 thermocouples via protocole 1-wire (pin d2)du max31850
  -record des datas thermocouples sur carte SD (pin d10 ,  d11, d12d, d13)
  -emission des datas thermocouples via xbee (rs-232) (pin d0)
  -recoit des commandes via xbee (rs-232)(pin d1)
  -commande numerique transistors vers relais chaudiere (pin a definir)
*/

//----lib----//
#include <SPI.h>
#include <SD.h>
#include <stdlib.h>
//-max31850_1-wire-//
#include <OneWire.h>
#include <DallasTemperature.h>

//----var_serial----//
const int SerialRate = 9600;//BaudRate serial 0
#define INPUT_SIZE 30 //maximum size of incoming serial for parsing
//----var_SD----//
const int ChipSelect = 4;//pin de selection SD
//----var_temp----//
long date = 0 ;//temps arduino
long last_date = 0 ;//temps arduino a n-1
float fdate = 0.0;//temps calcule arduino en seconde
long intdate = 0 ; //temps calcule arduino en seconde entiere
//----var_sensor----//
#define ONE_WIRE_BUS 2 // Data wire is plugged into port 2 on the Arduino
#define TEMPERATURE_PRECISION 9 // maximum bytes for max31850
int nbr_sensors= 0 ;//nombre de capteurs
float temp_all[4]={1, 2, 3, 4};
//----var_commande----//
int mySensVals[4] = {0, 0, 0, 0};
int pin_temp = 9;// the pin that the consigne Temperature huile is attached to
int pompe = 0;//etat de la pompe
int froid = 0;//etat du refroidissement
int temp = 0;//etat de la temperature cible huile
//----var_file----//
int new_fic = 1 ; //etat de creation d'un nouveau fichier de log  (start on)
String name_fic_log = "log";
int increment_fic_log = 1;//increment for each new file log
char name_fic[50];

//----init_sensors_by_1-wire-connection----//
OneWire oneWire(ONE_WIRE_BUS);// Setup a oneWire instance to communicate with any OneWire devices
DallasTemperature sensors(&oneWire);// Pass our oneWire reference to Dallas Temperature.
DeviceAddress Thermometer_1, Thermometer_2;// arrays to hold device addresses
DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address

//----send_log----//
void printlog ( String mylog) {
  Serial.print("log: ");//for labview logging error (diff from "dat:" for data)
  Serial.println(mylog);
  }

//----creation et entete_fichier_log----// 
void prepar_fichier()
{
  if (increment_fic_log >= 10) {
    String name_fic_destroy = "log" + String(increment_fic_log - 10);
    int str_len = name_fic_destroy.length() + 1;// Length (with one extra character for the null terminator)
    char fic_destroy[str_len];    // Prepare the character array (the buffer)
    name_fic_destroy.toCharArray(fic_destroy, str_len); // Copy it over
    printlog(name_fic_destroy + " destroy ");//labview log
    SD.remove(fic_destroy);//destroy old file
    delay(1000);//wait
    }
  //--test_existing_file--//
  if (SD.exists(name_fic)) {
    printlog(name_fic_log + "already exist. destroy it");
    SD.remove(name_fic);
    delay(5000);
  }
  //--create_new_file--//
  File fichier = SD.open(name_fic, FILE_WRITE);//name of the files for log
  fichier.println("record format:");//decription
  fichier.println("Time , Temp1, Temp2, Temp3, Temp4, Com1 , Com2 , Anal_out");//description
  fichier.close();
  printlog(name_fic_log + "created");
  new_fic=0;//new fic was created (return to 0)
}

void setup() {
  pinMode(pin_temp, OUTPUT);//defini la pin en output analogic
  pinMode(A0, OUTPUT);//defini la pin en output numeric
  pinMode(A1, OUTPUT);//defini la pin en output numeric
 
  /*digitalWrite(A0, HIGH);//test
  digitalWrite(A1, LOW);
  delay(1000);
  digitalWrite(A0, LOW);
  digitalWrite(A1, HIGH);
  delay(1000);*/
  digitalWrite(A0, LOW);
  digitalWrite(A1, LOW);

  Serial.begin(SerialRate);//init serial
  Serial.setTimeout(1);
  delay(1000);
  printlog("start");//labview starting log
 
  if (!SD.begin(ChipSelect)) //SD card test
    {
    printlog("SD absente");//to labview log
    }
  else
    {
    printlog("SD initialized");//to labview log
    }
  delay(1000);
  //----Init file for log
  name_fic_log = "log" + String(increment_fic_log);
  int str_len = name_fic_log.length() + 1;// Length (with one extra character for the null terminator)
  name_fic[str_len];// Prepare the character array (the buffer)
  name_fic_log.toCharArray(name_fic, str_len);// Copy it over
  prepar_fichier();  //init_writing_fic
 
  //--init_sensor--//
  sensors.begin();// Start up the max31850 library
  nbr_sensors = sensors.getDeviceCount();//locate devices on the bus
  printlog("found " + String(nbr_sensors) + " sensors");//to labview log
 
  //----Loop through each device, print out address
  for(int i=0;i<nbr_sensors; i++)
  {
    //-----Search the wire for address
    if(sensors.getAddress(tempDeviceAddress, i))
{
//---- set the resolution to TEMPERATURE_PRECISION bit
sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
/*Serial.print("Resolution actually set to: ");
Serial.print(sensors.getResolution(tempDeviceAddress), DEC);
Serial.println();*/
}else{
Serial.print("Found ghost device at ");
Serial.print(i, DEC);
Serial.print(" but could not detect address. Check power and cabling");
}
  }
  delay(3000);
}

//----function to return the temperature for a device
float printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  //Serial.println("dat:" + String(tempC) );
  return tempC;
}

void loop() {
  sensors.requestTemperatures();  // request to all devices on the bus
  //----Loop through each device, print out temperature data
  for(int i=0;i<nbr_sensors; i++)
  {
    //----Search the wire for address
    if(sensors.getAddress(tempDeviceAddress, i))
{
temp_all[i]=printTemperature(tempDeviceAddress); // Use a simple function to get the data
}
//else ghost device! Check your power requirements and cabling
  }
 
  Serial.println("dat:" + String(temp_all[0])
  + ", " + String(temp_all[1])
  + ", " + String(temp_all[2])
  + ", " + String(temp_all[3])
  );
  //----test time for file logging----
  date = millis();
  fdate = date / 1000;
  intdate = long (fdate);
  //-----file logging------
  if ( intdate != last_date ) {
    last_date = intdate ;
    File fichier = SD.open(name_fic, FILE_WRITE);//open
    fichier.println ( String(intdate) + //write
    ", " + String(temp_all[0])
    + ", " + String(temp_all[1])
    + ", " + String(temp_all[2])
    + ", " + String(temp_all[3])
    + ", " + String(pompe)
    + ", " + String(froid)
    + ", " + String(temp)
    + ", " + String(new_fic)
    );
   
    fichier.close();
    }

  while (Serial.available() > 0) {
    pompe = Serial.parseInt();// look for the next valid integer in the incoming serial stream:
    froid = Serial.parseInt();
    temp = Serial.parseInt();
    new_fic = Serial.parseInt();
    //----test data -----
      if (pompe >=2 || froid >= 2 || new_fic >= 2 ) {
        printlog ("error");//no good
        printlog (String (pompe) + String(froid) + String (temp) + String(new_fic));
        }
      else{//good data
        digitalWrite(A0, pompe);  // turn the LED on (HIGH is the voltage level)
        digitalWrite(A1, froid);  // turn the LED on (HIGH is the voltage level)
        analogWrite(pin_temp, temp);
        //printlog ("no error on incomiong data");
        //printlog (String (pompe) + String(froid) + String (temp) + String(new_fic));
        if (new_fic == 1 ){
          increment_fic_log +=1;//++
          name_fic_log = "log" + String(increment_fic_log);//increment the name
          printlog("log name is " + name_fic_log);//log the name
          int str_len = name_fic_log.length() + 1;// Length (with one extra character for the null terminator)
          name_fic[str_len];// Prepare the character array (the buffer)
          name_fic_log.toCharArray(name_fic, str_len);// Copy it over
          prepar_fichier();  //init_writing_fic
        }
      }
    }
}