problem with loop in my program

Hello everybody,

i'm using arduino UNO to develop a sensor, i begin by a very simple code to try the sensor, but after a few time the program stops working and i can't use it.I must every time upload the program to my arduino in order to continue to use it. I want my program runs continusely. Here the code :

// code simple alcoometre

unsigned int val = 0;
unsigned char PIN = 0;
void setup() {
    Serial.begin(9600);
    pinMode(PIN,INPUT);
    }
void loop() {
    val = analogRead(PIN);
    Serial.println(val);
    delay(500);
    }

Thanks for help.

Why is PIN declared as a char ? What type of sensor is it ? Is the sensor connected to pin 0 or pin A0 ? What sort of values do you get when you run the program ?

unsigned char PIN = 0;
void setup() {
    Serial.begin(9600);

I don't see an unsigned char as being a problem, but I do see a problem with it being zero.

// code simple alcoometre

const unsigned char PIN = A0;  // just to be clear
void setup() 
{
    Serial.begin(9600);
}
void loop() 
{
    int val = analogRead(PIN);
    Serial.println(val);
    delay(500);
}

Perhaps show your wiring?

thanks for reply. For my trial, i use MQ-3 sensor which is connected to the pin A0. when it runs, the program send number 0,1,3,4,5,6,7.......

Do you have the heater hooked up?

The output of the MQ-3 is resistive, what is the value of the dropping resistor and supply voltage?

hello,

resistance load 500 ohm tension +5 V

thanks

Hello,

I'm using Mq3 sensor with resistance load 500 ohm and 5 v . i'm looking to transform the output of the sensor to a % (0-5 %). the reply of the mq3 it's string very long number and it's difficult to make measurement. To calibrate the sensor i use a 10k potentiometer

thanks

Where's the code?

AWOL: Where's the code?

In his other thread.

Arrch:

AWOL:
Where’s the code?

In his other thread.

Does someone has a link to that other thread?

Topics merged - it didn't seem to me worthwhile starting a new programming thread without any code

The reference (documentation) for analogRead(), seems to indicate the pin specification as a number, not a string like "a0", but a number such as 0, or 3. Will it accept either way? or just ad document indicates?

int analogPin = 3;     // potentiometer wiper (middle terminal) connected to analog pin 3
                       // outside leads to ground and +5V
int val = 0;           // variable to store the value read

void setup()
{
  Serial.begin(9600);          //  setup serial
}

void loop()
{
  val = analogRead(analogPin);    // read the input pin
  Serial.println(val);             // debug value
}

jackwp: The reference (documentation) for analogRead(), seems to indicate the pin specification as a number, not a string like "a0", but a number such as 0, or 3.

A0 isn't a string.

Does this help? From pins_arduino.h:

static const uint8_t A0 = 14;

Ok, thanks, I see now it is ok either way. Jack

hello,

the code is below:

#include "arduino.h"
#include <LiquidCrystal.h>
#include <sstream>
#include <string>
#include <iostream>

using namespace std;

float concentration(float x) {
  const float A[] = { 
    2.71494E+02, -3.10999E+02, 6.85051E+02, -3.47587E+02, 7.47499E+01            }; 
  float result;
  float B[4];
  B[0] = x*x;
  B[1] = B[0]*x;
  B[2] = B[1]*x;
  B[3] = B[2]*x;

  result = A[0]*x+A[1]*B[0]+A[2]*B[1]+A[3]*B[2]+A[4]*B[3];
  return result;
}

char messages[5][16] = {
  "Heating sensor", "64 seconds", "Autozero", "Ready!", "ppm"};

int L;
float x;
float x0[5];
float x_initial;

LiquidCrystal lcd(12, 11,5, 4, 3, 2); // Wiring microcontroller - LCD:

const int pushbutton = 8; // Pushbutton (normally open). Stops initial heating time when it is pressed. .
const int analogPin = A0; // Reads sensor voltage as a float int the interval (0-1) corresponding to (0 - 3.3V).
float sensor_value_AnalogIn = 7; // Analog output to a multimeter or datalogger (1V = 1000 ppm).

void text_screen( char messages[],  int colum, int row) {
  lcd.setCursor( colum, row);
  lcd.print(messages);
}



void setup() {
  lcd.begin(16,2);
  Serial.begin(9600);
  lcd.clear(); 
  text_screen(messages[0], 0, 0);
  text_screen(messages[1], 0, 1);
  delay(2000);

  for (int j = 0; j<4; j++)
  {
    lcd.clear();  // Heating sensor 4x16 = 64 seconds
    text_screen(messages[0], 0, 0);


    for (int i = 0; i<16; i++){  
      if (pushbutton == 1){   // Pressing pushbutton stops initial heating and enters in measuring mode.
        break;
      }

      lcd.setCursor(i, 1);
      lcd.write(62);
      delay(1000);
    }
  }


  lcd.clear();
  text_screen(messages[2], 0, 0);
  delay(1000);


  //float sensor_value_AnalogIn = 7; // Analog output to a multimeter or datalogger (1V = 1000 ppm).
 
  for (int i=0; i<5; i++)
  {
   x0[i]= analogRead(analogPin);
    delay(500);

  }
  x_initial = (x0[0]+x0[1]+x0[2]+x0[3]+x0[4])/5.0; // Autozero. Average of 5 initial measures.

  lcd.clear();
  text_screen(messages[3], 0, 0);
  delay(2000);

  
}

void loop()  {
  x = analogRead(analogPin);
  x = (x-x_initial)*3.3; // Calculate real voltage. 
  float sensor_value_AnalogIn = x/3000;  // if analog reading do not match digital readings you can adjust this value (1000 ppm =1V).

  if(x<0)
    x = 0;

  x = concentration(x);
  
//Serial.println(x); 
  delayMicroseconds(100);

  // Float to string conversion.
 char buffer[5];     

  dtostrf(x, 5, 3, buffer);

  lcd.clear();

  for (int i=0; i<strlen(buffer); i+=1)
  {
    lcd.setCursor(i,0);
    lcd.write(buffer[i]); 
   text_screen(messages[4], 10, 0); 
   
    Serial.println(buffer);
  }
  delay(2000);
 
}

the resistance load 500 ohm
voltage +5V