Go Down

Topic: Combining code datalogger and monstermux 32 (Read 780 times) previous topic - next topic


Oct 05, 2012, 03:58 pm Last Edit: Nov 14, 2012, 11:18 am by rjbtd1 Reason: 1
Hello all,

I'm having trouble to put together 2 codes from 2 different shields... I have an arduino Duemilanove, a datalogger from adafruit and a 32 analogue shield from critical velocity.

the question is how to combine the two standard code in one!
I'm a beginner in arduino but know my electronics!  Coding is not my best part but eager to learn from anyone!

here is the  part from the "monstermux 32ch analogue shield";

Code: [Select]
[color=red]/* MonsterMux32 Demo Code
  Critical Velocity 2009
  This code selects an analog channel and prints the value to the serial port.
  For more information, visit www.criticalvelocity.com/mmux32

int sensorValue;
char muxch = 23;  // change this to the channel number you wish to read.

void setup() {
 // These 6 pins control which analog channel is connected to the Analog Input 0 on the Arduino.
 pinMode(8, OUTPUT);  
 pinMode(9, OUTPUT);  
 pinMode(10, OUTPUT);  
 pinMode(11, OUTPUT);  
 pinMode(12, OUTPUT);  
 pinMode(13, OUTPUT);

void loop() {
 // read the value from the sensor:
 // Analog 0 is now connected to input set by muxch.
 sensorValue = analogRead(0);    

// The following function selects the channel to be read.
// using PORTB directly is not really recommended for the Arduino environment.
// Feel free to modify the code to avoid direct writes to PORTB.

void set_mux(char channel) {
 // sets mux to read selected channel.
 if (channel < 32) {
   // valid channels are 0-31
   PORTB = (PORTB & 0xC0) | channel;

and here the code from the datalogger;

Code: [Select]
[color=green]#include <SD.h>
#include <Wire.h>
#include "RTClib.h"

// A simple data logger for the Arduino analog pins

// how many milliseconds between grabbing data and logging it. 1000 ms is once a second
#define LOG_INTERVAL  1000 // mills between entries (reduce to take more/faster data)

// how many milliseconds before writing the logged data permanently to disk
// set it to the LOG_INTERVAL to write each time (safest)
// set it to 10*LOG_INTERVAL to write all data every 10 datareads, you could lose up to
// the last 10 reads if power is lost but it uses less power and is much faster!
#define SYNC_INTERVAL 1000 // mills between calls to flush() - to write data to the card
uint32_t syncTime = 0; // time of last sync()

#define ECHO_TO_SERIAL   1 // echo data to serial port
#define WAIT_TO_START    0 // Wait for serial input in setup()

// the digital pins that connect to the LEDs
#define redLEDpin 2
#define greenLEDpin 3

// The analog pins that connect to the sensors
#define photocellPin 0           // analog 0
#define tempPin 1                // analog 1
#define BANDGAPREF 14            // special indicator that we want to measure the bandgap

#define aref_voltage 3.3         // we tie 3.3V to ARef and measure it with a multimeter!
#define bandgap_voltage 1.1      // this is not super guaranteed but its not -too- off

RTC_DS1307 RTC; // define the Real Time Clock object

// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 10;

// the logging file
File logfile;

void error(char *str)
 Serial.print("error: ");
 // red LED indicates error
 digitalWrite(redLEDpin, HIGH);


void setup(void)
 // use debugging LEDs
 pinMode(redLEDpin, OUTPUT);
 pinMode(greenLEDpin, OUTPUT);
 Serial.println("Type any character to start");
 while (!Serial.available());
#endif //WAIT_TO_START

 // initialize the SD card
 Serial.print("Initializing SD card...");
 // make sure that the default chip select pin is set to
 // output, even if you don't use it:
 pinMode(10, OUTPUT);
 // see if the card is present and can be initialized:
 if (!SD.begin(chipSelect)) {
   error("Card failed, or not present");
 Serial.println("card initialized.");
 // create a new file
 char filename[] = "LOGGER00.CSV";
 for (uint8_t i = 0; i < 100; i++) {
   filename[6] = i/10 + '0';
   filename[7] = i%10 + '0';
   if (! SD.exists(filename)) {
     // only open a new file if it doesn't exist
     logfile = SD.open(filename, FILE_WRITE);
     break;  // leave the loop!
 if (! logfile) {
   error("couldnt create file");
 Serial.print("Logging to: ");

 // connect to RTC
 if (!RTC.begin()) {
   logfile.println("RTC failed");
   Serial.println("RTC failed");
#endif  //ECHO_TO_SERIAL


 // If you want to set the aref to something other than 5v

void loop(void)
 DateTime now;

 // delay for the amount of time we want between readings
 delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
 digitalWrite(greenLEDpin, HIGH);
 // log milliseconds since starting
 uint32_t m = millis();
 logfile.print(m);           // milliseconds since start
 logfile.print(", ");    
 Serial.print(m);         // milliseconds since start
 Serial.print(", ");  

 // fetch the time
 now = RTC.now();
 // log time
 logfile.print(now.unixtime()); // seconds since 1/1/1970
 logfile.print(", ");
 logfile.print(now.year(), DEC);
 logfile.print(now.month(), DEC);
 logfile.print(now.day(), DEC);
 logfile.print(" ");
 logfile.print(now.hour(), DEC);
 logfile.print(now.minute(), DEC);
 logfile.print(now.second(), DEC);
 Serial.print(now.unixtime()); // seconds since 1/1/1970
 Serial.print(", ");
 Serial.print(now.year(), DEC);
 Serial.print(now.month(), DEC);
 Serial.print(now.day(), DEC);
 Serial.print(" ");
 Serial.print(now.hour(), DEC);
 Serial.print(now.minute(), DEC);
 Serial.print(now.second(), DEC);

 int photocellReading = analogRead(photocellPin);  
 int tempReading = analogRead(tempPin);    
 // converting that reading to voltage, for 3.3v arduino use 3.3, for 5.0, use 5.0
 float voltage = tempReading * aref_voltage / 1024;  
 float temperatureC = (voltage - 0.5) * 100 ;
 float temperatureF = (temperatureC * 9 / 5) + 32;
 logfile.print(", ");    
 logfile.print(", ");    
 Serial.print(", ");  
 Serial.print(", ");    

 // Log the estimated 'VCC' voltage by measuring the internal 1.1v ref
 int refReading = analogRead(BANDGAPREF);
 float supplyvoltage = (bandgap_voltage * 1024) / refReading;
 logfile.print(", ");
 Serial.print(", ");  
#endif // ECHO_TO_SERIAL

#endif // ECHO_TO_SERIAL

 digitalWrite(greenLEDpin, LOW);

 // Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
 // which uses a bunch of power and takes time
 if ((millis() - syncTime) < SYNC_INTERVAL) return;
 syncTime = millis();
 // blink LED to show we are syncing data to the card & updating FAT!
 digitalWrite(redLEDpin, HIGH);
 digitalWrite(redLEDpin, LOW);

I tried a few things like combining the void setup  and  replacing the two void loop by naming them void loop_code 1(); and void loop_code2();
this was not working for me and many errors appeared!

by the way: the code for the light sensor and temperature does not have tobe in there, I want to have a blank shield where I can just put my analogue inputs into it.

My goal is to use this datalogger for monitoring many K-type thermocouples using the AD595 thermocouple chip!

Please, if there's anyone who can help it's very much appreciated!




In my experience, as a hack programmer, the only way to really combine two pieces of code like that is to completely understand how each piece works. Once you fully understand what's happening it will start to become apparent which pieces you can keep and what you need to rewrite. It isn't so much about bashing two sketches together as combining two concepts. Just read them over and over and make experimental changes until they make sense. You don't need some of the code on the logger so work on eliminating that as a means to understanding how logging works.

One little tip. I notice that the logging sketch uses delay() for timing when to log. That won't be acceptable when you combine the two. So now there's another sketch that you will need to digest - blink without delay. The concept behind it will be needed.



Hello Jimmy60

Thanks a lot for the rapid reply!!
I also think i have to invest in learning to code and really understand the code that is used in the two sketches. (no better way to learn then just tinkering about)

I have all made the hardware for using my little project and just can't wait to see it work! the good thing is is that it's a hobby and hope to learn a lot from this!

Again, thanks for your tip and fast reply!


I also think i have to invest in learning to code and really understand the code that is used in the two sketches. (no better way to learn then just tinkering about)

You can think about it all day long, but you won't actually learn anything until you start making your own mistakes, figuring out that they are mistakes, why they are mistakes, and correcting them.


Hey Paul... You Promised you wouldn't share my little secret to success...

--> WA7EMS <--
"The solution of every problem is another problem." -Johann Wolfgang von Goethe
I do answer technical questions PM'd to me with whatever is in my clipboard

Go Up