Pages: [1]   Go Down
Author Topic: analogRead not working..  (Read 1038 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi everyone,

I'm making a datalogger with an Arduino UNO board, and the data acquisition part was working properly, but the data were too fast for the Arduino to read it.
I read the suggestions on this post http://arduino.cc/forum/index.php/topic,6549.0.html and try it myself, so the analogRead would go at a higher frequency. After this, the program wouldn't work, none of the values were read (random symbols were printed instead), and now it just stops when it arrives to the analogRead instruction.

Can someone help me to fix this? Thank you!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46091
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So, you had some code we've never seen. You made some changes to it, and now it doesn't work. But, you want us to help you fix it. Something's missing here...
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, here's my code, the thing is, I uploaded the earlier version of it (without changing the prescale) and it wouldn't work either.

EDIT: Now I've posted the complete code, thank you for your patience!

Code:
#include <Wire.h>
#include <avr/io.h>
#include <SD.h>
#include <avr/power.h>
#include <stdio.h>
#include <stdlib.h>
#include "RTClib.h"

 
RTC_DS1307 RTC;

#define porcentaje 0.7

#define FASTADC 1

// defines for setting and clearing register bits
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif

int rly1Pin = 2;
int rly2Pin = 3;
int rly3Pin = 4;
int voltPin = A2;
int corrPin = A3;
int valor = 0;
const float incremento = (5/1024); //valor de cambio de voltaje para incremento de ADC
float voltVec[100]={0}; //vector que guardara todos los puntos para V
float corrVec[100]={0}; //vector que guardara todos los puntos para I
//Para la tarjeta SD de memoria
int CS_pin = 10;
int pow_pin = 8;



void setup () {
    Serial.begin(9600);
    Wire.begin();
    RTC.begin();
 
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");}
    
//RTC.adjust(DateTime(__DATE__, __TIME__));
  pinMode(CS_pin, OUTPUT);
  pinMode(pow_pin, OUTPUT);
  pinMode(rly1Pin, OUTPUT);
  pinMode(rly2Pin, OUTPUT);
  pinMode(rly3Pin, OUTPUT);
  Desactivar_Relay();
  SwitchOff_Carga();
  digitalWrite(rly3Pin, LOW);
  digitalWrite(pow_pin, HIGH);
  
  if (!SD.begin(CS_pin))
  {
    Serial.println("Card Failure");
    return;
  }
#if FASTADC
  // set prescale to 16
  sbi(ADCSRA,ADPS2) ;
  cbi(ADCSRA,ADPS1) ;
  cbi(ADCSRA,ADPS0) ;
#endif

}


void loop(){  
  
//  pregunta:
  DateTime now = RTC.now();
  int hora = now.hour();
  int minuto = now.minute(); //saber hora y minutos del dia para iniciar la secuencia


  if (hora<7)
{
Dormir();
}

  if (hora>22)
{
Dormir();
}
  
    
  else
{
   if (minuto==32)
     {
     //goto pregunta;
     Despierta();
     Activar_Relay();
     int voc = 0;
    
     voc = analogRead(voltPin);

     int maximo = voc*porcentaje;
     int delta1 = maximo/5;
     int delta2 = (voc-maximo)/95;
     Serial.print("VOC = ");
     Serial.println(voc,DEC);
     SwitchOn_Carga();
     Medicion_Datos_IV(delta1,1,6);
     Medicion_Datos_IV(delta2,6,101);
     Imprimir_Serial();
     Desactivar_Relay();
     SwitchOff_Carga();
     Switch_Descarga();
     Nada();
 }
  
  
  else
  {
     Desactivar_Relay();
   }
}
  
}    



void Activar_Relay (){
     digitalWrite(rly1Pin, HIGH);
}

void Desactivar_Relay(){
    digitalWrite(rly1Pin, LOW);
}

void Medicion_Datos_IV (int delta, int lim_inf, int lim_sup){
  
  int compara = 0;
  int i = 0;
  
  while(i<lim_sup)
  {
  compara = compara+delta;
  int volt = analogRead(voltPin);    
  int corr = analogRead(corrPin);
  if (volt>=compara)
  {voltVec[i] = volt;//(volt*incremento);
  corrVec[i] = corr;//(corr*incremento);
  i=i+1;}
  else{}
  }
  
  
}

void Imprimir_Serial(){
  for(int i=1; i<=101;i++)
  {
  Serial.println(voltVec[i], DEC);
  }
  Serial.println("/");
  for(int i=1; i<=101;i++)
  {
  Serial.println(corrVec[i], DEC);
  }
}

void Nada (){
  Serial.println("Fin de toma de datos");
}

void Despierta(){
//power_spi_enable();
power_usart0_enable();
//power_usart1_enable();
power_timer0_enable();
power_timer1_enable();
power_timer2_enable();
//power_twi_enable();
}
void Dormir(){

Serial.println("No molestar, estoy durmiendo");
//power_spi_disable();
power_usart0_disable();
//power_usart1_disable();
power_timer0_disable();
power_timer1_disable();
power_timer2_disable();
//power_twi_disable();

}  

void SwitchOn_Carga(){
  digitalWrite(rly2Pin, HIGH);
  Serial.println("Conectando carga");
}

void SwitchOff_Carga(){
  digitalWrite(rly2Pin, LOW);
  Serial.println("Desconectando carga");
}

void Switch_Descarga(){    
  int medida = analogRead(voltPin);
  while (medida >= 1){
  digitalWrite(rly3Pin, HIGH);
  medida = analogRead(voltPin);
  delay(5);
  }
  digitalWrite(rly3Pin, LOW);
}  

  
  
« Last Edit: October 17, 2012, 05:11:44 pm by Faye_V » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46091
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  if (hora<7){Dormir();}
  if (hora>22){Dormir();}
No code following a {.
No code on same line as }.

Code:
  else {
What does this else go with? Why do you want to do the rest of this before 22:00 hours only?

If you are doing something (what isn't clear because you didn't post all of your code) if it's before 7:00 or after 22:00, the else seems misplaced.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for your observations, I'll try not to be so messy with my codes... What I pretend to do is an IV curve tracer, so I have to take measures every hour from 7 am to 7 pm (10 pm on the sketch for testing), first I get a direct value from my voltage source (a PV), afterwards I connect a load to the PV, and take data again.

I've uploaded my sketch (the one without the prescaler change) into another UNO board already, and it doesn't work either! I'm collapsing! Could it be the serial port? It gives me this:

Desconectando carga
Card Failure
VOC = Desconectando carga
Card Failure
VOC = Desconectando carga
Card Failure
VOC = Desconectando carga
Card Failure
VOC = Desconectando carga
Card Failure
VOC = Desconectando carga
Card Failure
VOC = Desconectando carga
Card Failure

Anf before I got voltage values (from 0 to 1023). What happened?
« Last Edit: October 17, 2012, 05:19:07 pm by Faye_V » Logged

Offline Offline
Edison Member
*
Karma: 18
Posts: 1170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Does it work if you undefine FASTADC?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'll try that.

 I think I've solved it! Without using the FASTADC though, it seems to be a problem with the SD card setup, without it, it runs and takes the data (random values, different from those on the voltmeter) so I'll see if there's another way to get the correct data.

Thanks!
Logged

Offline Offline
Edison Member
*
Karma: 18
Posts: 1170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Also, incremento will be 0, since 5/1024 will be done in integer math before being converted to a float. try 5.0/1024.0
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks a lot, that explains it! Now it gives the value I want smiley

The problem I had wasn't with the SD card code, it was an RTC issue, it reseted itself, so it was 1am for my circuit, and the program entered into the "dormir" routine, putting low the USART0.. Could it be that the FASTADC caused this to the RTC?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm still having trouble with the analogRead in my program, I'm no longer using the FASTADC routine, because I've changed my load so the voltage increase will become slower.This way the 100µs per sample are enough, since I have 20ms from 0 to the top voltage, this allows me to have 200 samples and I only require 100.

BUT, the analogRead gives me random values! I keep getting 5V (1023) when I actually have 3.7V maximum.. What can be wrong? I'll appreciate any thoughts on this! Thanks!
Logged

Pages: [1]   Go Up
Jump to: