2

With this code I try to send a measurement over 443MHz using RadioHead. All parts work but when combined they fail. This chip used is an ATtiny85.

The function 'measure' torpedoes the entire program. If it is used, it will compile but it will never run Setup or Loop. I know this because I use a Logic Analyzer and Software Serial. I can see the Serial Port, PB1, hanging high.

I would like to know what is so wrong about function measure that it undermines all code..

#include <Arduino.h>
#include <SoftwareSerial.h>
#include <RH_ASK.h>   // local libs at
#include <powernap.h> // /home/thijs/.platformio/lib

#define TICKLE_ID 192

#define SENSOR1_PIN 2
#define SENSOR1_PWR 4
#define SENSOR2_PIN -1
#define SENSOR2_PWR -1

#define SOFTWARE_SERIAL_TX_PIN 1
#define SOFTWARE_SERIAL_RX_PIN -1

#define RADIOHEAD_BAUD 2000
#define RADIOHEAD_TX_PIN 0
#define RADIOHEAD_RX_PIN -1

struct tickle {
  uint16_t id = TICKLE_ID;
  uint16_t value1;
  uint16_t value2;
};

Napper napper;
SoftwareSerial mySerial(SOFTWARE_SERIAL_RX_PIN, SOFTWARE_SERIAL_TX_PIN);
RH_ASK driver(RADIOHEAD_BAUD, RADIOHEAD_RX_PIN, RADIOHEAD_TX_PIN);

void senddata(int hygro) {
  mySerial.print("Senddata");
  struct tickle package; // make a Tickle package
  package.id = TICKLE_ID; // hard-coded device ID
  package.value1 = 0; // any positive int up to 2^16
  package.value2 = hygro; // any positive int up to 2^16
  driver.send((uint8_t *)&package, sizeof(package));
  driver.waitPacketSent(); // wait for it ~Barney
}

int measure() {
  mySerial.print("Measure");
  pinMode(SENSOR1_PWR, OUTPUT); // enable sensor
  pinMode(SENSOR1_PWR, HIGH); // turn on sensor
  delay(100); // wait for sensor to settle

  int i; // int for counter
  int data = 0; // data container

  for (i = 0; i < 5; i++){ // cumulate 5 measurements
    data = data + analogRead(SENSOR1_PIN);
  }

  data = data / 5; // avg of measurements
  pinMode(SENSOR1_PWR, LOW); // turn off sensor
  pinMode(SENSOR1_PWR, INPUT); // disable sensor
  mySerial.print(data);
  return data; // return avg measured value
}

void setup() {
  mySerial.begin(600); // 600 baud serial
  mySerial.print("Setup"); // logging the start
  driver.init(); // initialize the RadioHead library
  napper.setup_sleep(); // initialize the Napper library
}

void loop() {
  mySerial.print("Loop"); // start of loop
  int measurement = measure(); // measure data 
  senddata(measurement); // send data
  napper.napminutes(1); // deep sleep for 1 minute
}

[EDIT]
I thought the problem to be this, and then to be that. This is how I debug, I start somewhere and work my way back. Currently it looks like my own library PowerNap is the culprit, I have tested with other Sleep code and it works. I can only be sure after more tests and isolated code running.

Thijs
  • 432
  • 1
  • 6
  • 21

3 Answers3

1

Interesting question but nothing seems wrong in the measure() function unless the mistake pinMode is used instead of digitalWrite. I suggest in the for loop keep a slight interval between the measurements. lets say about 50ms. if the problem still presets lets start the debugging. set a serial message in every critical point and pin point the exact location the code hangs.

0

analogRead() hangs

you started this journey thinking a particular routine (measure()) is the problem and you now are blaming analogRead() to be the source of your ills.

it will serve you much better for you to step back and ask yourself if your thought process is at fault here.

while you haven't provided much information on what it is that's not working, you have provided more than enough information to suggest that the issue is not measure() nor analogRead().

you may find this hard to understand now but debugging is far less about convincing yourself where the wrongs are. instead, it is about convincing yourself what you think right is wrong.

dannyf
  • 2,813
  • 11
  • 13
-1

Personally I think its a memory size issue, but the only real changes you need to make to measure is change pinMode to digitalWrite (as you have spotted) and personally I think you should remove the other pinMode statements and leave the pin setup the whole time, unless you are using it for something else.

int measure() {
  mySerial.println("Measure");
  pinMode(SENSOR1_PWR, OUTPUT); // enable sensor
  digitalWrite(SENSOR1_PWR, HIGH); // turn on sensor
  delay(100); // wait for sensor to settle

  int data = 0; // data container

  for (int i = 0; i < 5; i++){ // cumulate 5 measurements
    data = data + analogRead(SENSOR1_PIN);
  }

  data = data / 5; // avg of measurements
  digitalWrite(SENSOR1_PWR, LOW); // turn off sensor
  // pinMode(SENSOR1_PWR, INPUT); // disable sensor - If you do this the "sensor" floats.
  mySerial.println(data);
  return data; // return avg measured value
}
Code Gorilla
  • 5,652
  • 1
  • 17
  • 31