1

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.

closed as off-topic by Chris Stratton, user31481, SDsolar, MatsK, gre_gor Nov 20 '17 at 16:39

  • This question does not appear to be about Arduino, within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • My guess is that whatever is attached to pin 4 causes the chip to overload? – Majenko Jul 21 '17 at 10:17
  • thanks for the quick reply! I removed the sensor, should return floating analogue value now, still nothing, SoftwareSerial returns complete jibberish whenever I use the function. – Thijs Jul 21 '17 at 10:20
  • @Majenko It might, if he'd actually turned it on. But there are just bunch of pinMode calls and no digitalWrite. – KIIV Jul 21 '17 at 10:21
  • 2
    DO you think it fails over my mistake of using pinMode where it should read digitalWrite? – Thijs Jul 21 '17 at 10:24
  • 5
    I'm voting to close this old abandoned question as incomplete and unanswerable because the asker has posted that their prime suspicion is now with code which is not included in the question but only linked at an external site, which is not in keeping with the rules of this site which require questions to stand on their own without relying on external references for the key parts. – Chris Stratton Nov 18 '17 at 19:28
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.

  • 3
    Offcourse my thought process is wrong, otherwise I would file a bug at ATMEL or Arduino :D I am not here to find a problem in the tech, I am here to identify the mistake I am making myself. Yes I am the weakest link. This is nice to know but it does not help regarding the question at hand. – Thijs Jul 21 '17 at 15:28
0

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.

-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
}
  • if I replace the function measure() by '17' it works. But the function still does not produce a valid repsonse. The int from analogRead goes to uint16_t for the 'tickle' struct and then goes over the air as uint8_t? Maybe it gets lost in this type jungle? – Thijs Jul 21 '17 at 12:57
  • It really sounds like you are just over the maximum memory you can use. Remove myserial and call and it should work. There doesn't appear to be anything wrong with your code. – Code Gorilla Jul 21 '17 at 13:02
  • it hangs on analogRead(), I can see it not hitting the sleep routine by watching my multimeter. something must be in the way of analogRead() – Thijs Jul 21 '17 at 13:17
  • Put a delay of 10ms after the analogRead() call. – Code Gorilla Jul 21 '17 at 13:21
  • 1
    @CodeGorilla Theres no thumb of rule to use global variables or local variables one over the other. Regardless of the language here we are writing to a device whose memory is limited(few kbits). In these cases more likely so many global variables when accessed from several routines may lead to unexpected side effects. I just suspected if it was a a such issue because I didn't check his napper library for memory leaks. OP makes sure it hangs in the particular routing, So it was just a suggestion from me for testing. – danial weaber Jul 24 '17 at 8:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.