Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not reading more then two analog pins #23

Closed
fizroy opened this issue Jan 15, 2019 · 4 comments
Closed

Not reading more then two analog pins #23

fizroy opened this issue Jan 15, 2019 · 4 comments

Comments

@fizroy
Copy link

fizroy commented Jan 15, 2019

First of all great lib, but i have a problem reading more then two pins at the same time.
Is there something else to add to the code? here is my code right now only 3 pins are read
<
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <ResponsiveAnalogRead.h>

//const int pin0 = A0;
const int pin1 = A1;
const int pin2 = A2;
const int pin3 = A3;
const int pin4 = A4;
const int pin5 = A5;
const int pin6 = A6;
const int pin7 = A7;
const int pin8 = A8;
const int pin9 = A9;
const int pin10 = A10;
const int pin11 = A11;
const int pin12 = A12;
const int pin13 = A13;
const int pin14 = A14;
const int pin15 = A15;

ResponsiveAnalogRead analogZero(A0, true);
ResponsiveAnalogRead analog1(pin1, true);
ResponsiveAnalogRead analog2(pin2, true);
ResponsiveAnalogRead analog3(pin3, true);
ResponsiveAnalogRead analog4(pin4, true);
ResponsiveAnalogRead analog5(pin5, true);
ResponsiveAnalogRead analog6(pin6, true);
ResponsiveAnalogRead analog7(pin7, true);
ResponsiveAnalogRead analog8(pin8, true);
ResponsiveAnalogRead analog9(pin9, true);
ResponsiveAnalogRead analog10(pin10, true);
ResponsiveAnalogRead analog11(pin11, true);
ResponsiveAnalogRead analog12(pin12, true);
ResponsiveAnalogRead analog13(pin13, true);
ResponsiveAnalogRead analog14(pin14, true);
ResponsiveAnalogRead analog15(pin15, true);

#define PIN_SWITCH_1 22
#define PIN_SWITCH_2 23
#define PIN_SWITCH_3 24
#define PIN_SWITCH_4 25
#define PIN_SWITCH_5 26
#define PIN_SWITCH_6 27
#define PIN_SWITCH_7 28
#define PIN_SWITCH_8 29
#define PIN_SWITCH_9 30
#define PIN_SWITCH_10 31
#define PIN_SWITCH_11 32
#define PIN_SWITCH_12 33
#define PIN_SWITCH_13 34
#define PIN_SWITCH_14 35
#define PIN_SWITCH_15 36
#define PIN_SWITCH_16 37

/* the first measured value of the first point /
#define SENS_1_VAL 543.85
#define SENS_1_TMP 27.87
/
the second measured value of the second point */
#define SENS_2_VAL 544.25
#define SENS_2_TMP 27.91

// Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xEF, 0xED };
IPAddress ip(192, 168, 1, 74);
IPAddress server(192, 168, 1, 37);

void callback(char* topic, byte* payload, unsigned int length)
{

for (int i = 0; i < length; i++) {
char receivedChar = (char)payload[i];

if (strcmp(topic, "arduino01/pin22") == 0)
  if (receivedChar == '0')
    digitalWrite(22, HIGH);
if (strcmp(topic, "arduino01/pin22") == 0)
  if (receivedChar == '1')
    digitalWrite(22, LOW);

if (strcmp(topic, "arduino01/pin23") == 0)
  if (receivedChar == '0')
    digitalWrite(23, HIGH);
if (strcmp(topic, "arduino01/pin23") == 0)
  if (receivedChar == '1')
    digitalWrite(23, LOW);

if (strcmp(topic, "arduino01/pin24") == 0)
  if (receivedChar == '0')
    digitalWrite(24, HIGH);
if (strcmp(topic, "arduino01/pin24") == 0)
  if (receivedChar == '1')
    digitalWrite(24, LOW);

if (strcmp(topic, "arduino01/pin25") == 0)
  if (receivedChar == '0')
    digitalWrite(25, HIGH);
if (strcmp(topic, "arduino01/pin25") == 0)
  if (receivedChar == '1')
    digitalWrite(25, LOW);

if (strcmp(topic, "arduino01/pin26") == 0)
  if (receivedChar == '0')
    digitalWrite(26, HIGH);
if (strcmp(topic, "arduino01/pin26") == 0)
  if (receivedChar == '1')
    digitalWrite(26, LOW);

if (strcmp(topic, "arduino01/pin27") == 0)
  if (receivedChar == '0')
    digitalWrite(27, HIGH);
if (strcmp(topic, "arduino01/pin27") == 0)
  if (receivedChar == '1')
    digitalWrite(27, LOW);

if (strcmp(topic, "arduino01/pin28") == 0)
  if (receivedChar == '0')
    digitalWrite(28, HIGH);
if (strcmp(topic, "arduino01/pin28") == 0)
  if (receivedChar == '1')
    digitalWrite(28, LOW);

if (strcmp(topic, "arduino01/pin29") == 0)
  if (receivedChar == '0')
    digitalWrite(29, HIGH);
if (strcmp(topic, "arduino01/pin29") == 0)
  if (receivedChar == '1')
    digitalWrite(29, LOW);

if (strcmp(topic, "arduino01/pin30") == 0)
  if (receivedChar == '0')
    digitalWrite(30, HIGH);
if (strcmp(topic, "arduino01/pin30") == 0)
  if (receivedChar == '1')
    digitalWrite(30, LOW);

if (strcmp(topic, "arduino01/pin31") == 0)
  if (receivedChar == '0')
    digitalWrite(31, HIGH);
if (strcmp(topic, "arduino01/pin31") == 0)
  if (receivedChar == '1')
    digitalWrite(31, LOW);

if (strcmp(topic, "arduino01/pin32") == 0)
  if (receivedChar == '0')
    digitalWrite(32, HIGH);
if (strcmp(topic, "arduino01/pin32") == 0)
  if (receivedChar == '1')
    digitalWrite(32, LOW);

if (strcmp(topic, "arduino01/pin33") == 0)
  if (receivedChar == '0')
    digitalWrite(33, HIGH);
if (strcmp(topic, "arduino01/pin33") == 0)
  if (receivedChar == '1')
    digitalWrite(33, LOW);

if (strcmp(topic, "arduino01/pin34") == 0)
  if (receivedChar == '0')
    digitalWrite(34, HIGH);
if (strcmp(topic, "arduino01/pin34") == 0)
  if (receivedChar == '1')
    digitalWrite(34, LOW);

if (strcmp(topic, "arduino01/pin35") == 0)
  if (receivedChar == '0')
    digitalWrite(35, HIGH);
if (strcmp(topic, "arduino01/pin35") == 0)
  if (receivedChar == '1')
    digitalWrite(35, LOW);

if (strcmp(topic, "arduino01/pin36") == 0)
  if (receivedChar == '0')
    digitalWrite(36, HIGH);
if (strcmp(topic, "arduino01/pin36") == 0)
  if (receivedChar == '1')
    digitalWrite(36, LOW);
    
if (strcmp(topic, "arduino01/pin37") == 0)
  if (receivedChar == '0')
    digitalWrite(37, HIGH);
if (strcmp(topic, "arduino01/pin37") == 0)
  if (receivedChar == '1')
    digitalWrite(37, LOW);    

}
}

EthernetClient arduino01;
PubSubClient client(arduino01);

unsigned long lastMillis = 0;
long lastReconnectAttempt = 0;

boolean reconnect() {
if (client.connect("arduino01")) {
client.subscribe("arduino01/pin22");
client.subscribe("arduino01/pin23");
client.subscribe("arduino01/pin24");
client.subscribe("arduino01/pin25");
client.subscribe("arduino01/pin26");
client.subscribe("arduino01/pin27");
client.subscribe("arduino01/pin28");
client.subscribe("arduino01/pin29");
client.subscribe("arduino01/pin30");
client.subscribe("arduino01/pin31");
client.subscribe("arduino01/pin32");
client.subscribe("arduino01/pin33");
client.subscribe("arduino01/pin34");
client.subscribe("arduino01/pin35");
client.subscribe("arduino01/pin36");
client.subscribe("arduino01/pin37");
}

return client.connected();
}

void setup()
{
pinMode (PIN_SWITCH_1, OUTPUT);
pinMode (PIN_SWITCH_2, OUTPUT);
pinMode (PIN_SWITCH_3, OUTPUT);
pinMode (PIN_SWITCH_4, OUTPUT);
pinMode (PIN_SWITCH_5, OUTPUT);
pinMode (PIN_SWITCH_6, OUTPUT);
pinMode (PIN_SWITCH_7, OUTPUT);
pinMode (PIN_SWITCH_8, OUTPUT);
pinMode (PIN_SWITCH_9, OUTPUT);
pinMode (PIN_SWITCH_10, OUTPUT);
pinMode (PIN_SWITCH_11, OUTPUT);
pinMode (PIN_SWITCH_12, OUTPUT);
pinMode (PIN_SWITCH_13, OUTPUT);
pinMode (PIN_SWITCH_14, OUTPUT);
pinMode (PIN_SWITCH_15, OUTPUT);
pinMode (PIN_SWITCH_16, OUTPUT);
digitalWrite(22, HIGH);
digitalWrite(23, HIGH);
digitalWrite(24, HIGH);
digitalWrite(25, HIGH);
digitalWrite(26, HIGH);
digitalWrite(27, HIGH);
digitalWrite(28, HIGH);
digitalWrite(29, HIGH);
digitalWrite(30, HIGH);
digitalWrite(31, HIGH);
digitalWrite(32, HIGH);
digitalWrite(33, HIGH);
digitalWrite(34, HIGH);
digitalWrite(35, HIGH);
digitalWrite(36, HIGH);
digitalWrite(37, HIGH);
Serial.begin(9600);
client.setServer(server, 1883);
client.setCallback(callback);
Ethernet.begin(mac, ip);
delay(2000);
lastReconnectAttempt = 0;
}

void loop() {

if (!client.connected()) {reconnect();}

analogZero.update();
analog1.update();
analog2.update();
analog3.update();
analog4.update();
analog5.update();
analog6.update();
analog7.update();
analog8.update();
analog9.update();
analog10.update();
analog11.update();
analog12.update();
analog13.update();
analog14.update();
analog15.update();

int sensorValueZero = analogZero.getValue();
char temperatureZero[3];
float tempZero = (SENS_1_TMP + (SENS_2_TMP - SENS_1_TMP) / (SENS_2_VAL - SENS_1_VAL) * (sensorValueZero - SENS_1_VAL))-2;
dtostrf(tempZero, 0, 1, temperatureZero);

int sensorValue1 = analog1.getValue();
char temperature1[3];
float temp1 = (SENS_1_TMP + (SENS_2_TMP - SENS_1_TMP) / (SENS_2_VAL - SENS_1_VAL) * (sensorValue1 - SENS_1_VAL))-2;
dtostrf(temp1, 0, 1, temperature1);

int sensorValue2 = analog2.getValue();
char temperature2[3];
float temp2 = (SENS_1_TMP + (SENS_2_TMP - SENS_1_TMP) / (SENS_2_VAL - SENS_1_VAL) * (sensorValue2 - SENS_1_VAL))-2;
dtostrf(temp2, 0, 1, temperature2);

/*
int sensorValue3 = analog3.getValue();
char temperature3[3];
float temp3 = (SENS_1_TMP + (SENS_2_TMP - SENS_1_TMP) / (SENS_2_VAL - SENS_1_VAL) * (sensorValue3 - SENS_1_VAL))-2;
dtostrf(temp3, 0, 1, temperature3);

int sensorValue4 = analog4.getValue();
char temperature4[3];
float temp4 = (SENS_1_TMP + (SENS_2_TMP - SENS_1_TMP) / (SENS_2_VAL - SENS_1_VAL) * (sensorValue4 - SENS_1_VAL))-2;
dtostrf(temp4, 0, 1, temperature4);

int sensorValue5 = analog5.getValue();
char temperature5[3];
float temp5 = (SENS_1_TMP + (SENS_2_TMP - SENS_1_TMP) / (SENS_2_VAL - SENS_1_VAL) * (sensorValue5 - SENS_1_VAL))-2;
dtostrf(temp5, 0, 1, temperature5);

int sensorValue6 = analog6.getValue();
char temperature6[3];
float temp6 = (SENS_1_TMP + (SENS_2_TMP - SENS_1_TMP) / (SENS_2_VAL - SENS_1_VAL) * (sensorValue6 - SENS_1_VAL))-2;
dtostrf(temp6, 0, 1, temperature6);

int sensorValue7 = analog7.getValue();
char temperature7[3];
float temp7 = (SENS_1_TMP + (SENS_2_TMP - SENS_1_TMP) / (SENS_2_VAL - SENS_1_VAL) * (sensorValue7 - SENS_1_VAL))-2;
dtostrf(temp7, 0, 1, temperature7);

int sensorValue8 = analog8.getValue();
char temperature8[3];
float temp8 = (SENS_1_TMP + (SENS_2_TMP - SENS_1_TMP) / (SENS_2_VAL - SENS_1_VAL) * (sensorValue8 - SENS_1_VAL))-2;
dtostrf(temp8, 0, 1, temperature8);

int sensorValue9 = analog9.getValue();
char temperature9[3];
float temp9 = (SENS_1_TMP + (SENS_2_TMP - SENS_1_TMP) / (SENS_2_VAL - SENS_1_VAL) * (sensorValue9 - SENS_1_VAL))-2;
dtostrf(temp9, 0, 1, temperature9);

int sensorValue10 = analog10.getValue();
char temperature10[3];
float temp10 = (SENS_1_TMP + (SENS_2_TMP - SENS_1_TMP) / (SENS_2_VAL - SENS_1_VAL) * (sensorValue10 - SENS_1_VAL))-2;
dtostrf(temp10, 0, 1, temperature10);

int sensorValue11 = analog11.getValue();
char temperature11[3];
float temp11 = (SENS_1_TMP + (SENS_2_TMP - SENS_1_TMP) / (SENS_2_VAL - SENS_1_VAL) * (sensorValue11 - SENS_1_VAL))-2;
dtostrf(temp11, 0, 1, temperature11);

int sensorValue12 = analog12.getValue();
char temperature12[3];
float temp12 = (SENS_1_TMP + (SENS_2_TMP - SENS_1_TMP) / (SENS_2_VAL - SENS_1_VAL) * (sensorValue12 - SENS_1_VAL))-2;
dtostrf(temp12, 0, 1, temperature12);

int sensorValue13 = analog13.getValue();
char temperature13[3];
float temp13 = (SENS_1_TMP + (SENS_2_TMP - SENS_1_TMP) / (SENS_2_VAL - SENS_1_VAL) * (sensorValue13 - SENS_1_VAL))-2;
dtostrf(temp13, 0, 1, temperature13);

int sensorValue14 = analog14.getValue();
char temperature14[3];
float temp14 = (SENS_1_TMP + (SENS_2_TMP - SENS_1_TMP) / (SENS_2_VAL - SENS_1_VAL) * (sensorValue14 - SENS_1_VAL))-2;
dtostrf(temp14, 0, 1, temperature14);

int sensorValue15 = analog15.getValue();
char temperature15[3];
float temp15 = (SENS_1_TMP + (SENS_2_TMP - SENS_1_TMP) / (SENS_2_VAL - SENS_1_VAL) * (sensorValue15 - SENS_1_VAL))-2;
dtostrf(temp15, 0, 1, temperature15);
*/

if (millis() - lastMillis > 5000){
client.publish("arduino01/pin0", temperatureZero);
client.publish("arduino01/pin1", temperature1);
client.publish("arduino01/pin2", temperature2);
//client.publish("arduino01/pin3", temperature3);
//client.publish("arduino01/pin4", temperature4);
//client.publish("arduino01/pin5", temperature5);
//client.publish("arduino01/pin6", temperature6);
//client.publish("arduino01/pin7", temperature7);
//client.publish("arduino01/pin8", temperature8);
//client.publish("arduino01/pin9", temperature9);
//client.publish("arduino01/pin10", temperature10);
//client.publish("arduino01/pin11", temperature11);
//client.publish("arduino01/pin12", temperature12);
//client.publish("arduino01/pin13", temperature13);
//client.publish("arduino01/pin14", temperature14);
//client.publish("arduino01/pin15", temperature15);
Serial.println(temperatureZero);
Serial.println(temperature1);
Serial.println(temperature2);
//Serial.println(temperature3);
//Serial.println(temperature4);
//Serial.println(temperature5);
//Serial.println(temperature6);
//Serial.println(temperature7);
//Serial.println(temperature8);
//Serial.println(temperature9);
//Serial.println(temperature10);
//Serial.println(temperature11);
//Serial.println(temperature12);
//Serial.println(temperature13);
//Serial.println(temperature14);
//Serial.println(temperature15);
lastMillis = millis();
}

client.loop();

}

@ristomatti
Copy link

@fizroy There seems to be quite a lot going on in your code. It could be a side effect of something else why no more than 2 work at the same time. Have you tried reducing your code to just reading 3 analog pins and printing them to serial? If it works, then start adding more pins. If all the pins are read correctly, then start adding the networking code.

Disclaimer: I know nothing about this library, I just found this repository.

@fizroy
Copy link
Author

fizroy commented Jan 17, 2019

It's ok i think i got it...arduino mega can't read more then 2 analog pins at one because of the mux.

and it works with just one because the other reading was leaking from the first pin.

@fizroy fizroy closed this as completed Jan 17, 2019
@ristomatti
Copy link

@fizroy Doesn't Mega have enough inputs for the code you pasted on itself? :) Anyway good if you figured out what the issue is.

@jsiddall
Copy link

I am also seeing strange behavior if I try to update() more than 2 inputs. 2 works great, 3 seems to hang the arduino entirely. It doesn't matter which of the 3 updates I comment, as long as I only do 2 updates in the loop. Any thoughts before I dig into this more?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants