Snippets

Peter Scargill Nano peripheral

Updated by Peter Scargill

File snippet.txt Modified

  • Ignore whitespace
  • Hide word diff
-// So this is a relatively simple,cheap peripheral using a £1.50 NANO board from China. 
+// This update May 2018 Peter Scargill
+//
+// So this is a relatively simple,cheap peripheral using a £1.50 NANO board from China.
 // It is supported by my ESP8266 software but as it is an I2c slave you could run it
-// from anything able to handle I2c. For example I have found that some of the NanoPi units 
+// from anything able to handle I2c. For example I have found that some of the NanoPi SBCs
 // are not too keen on even a bright LED on their IO pins and from an operating
-// system like Linux, getting PWM on several pins is just not on... so - plug in this device 
-// (default ID 9) with pullups (always needed for I2c) and you can gain PWM, ADC 
-// and general IO for very little money. 
+// system like Linux, getting PWM on several pins is just not on... so - plug in this device
+// (default ID 9) with pullups (always needed for I2c) and you can gain PWM, ADC
+// and general IO for very little money or better, use with ESP-GO to add veryu inexpensive funtionality to the ESP8266.
+//
 //
-// So as a guide you could use 3,5,6 and 9, 10 and 11 for PWM (unless you use these pins for general IO)
-// you can use 2, 4, 7, 8, 12 and 13 as input/output (I tried using 0 and 1 - no go 0 flashes 
-// on power up - 1 has pullup - best just avoid for GPIO 0 - use for serial IO).
+// As a guide you could use 3,5,6 and 9, 10 and 11 for PWM (unless you use these pins for general IO)
+// you can use 2, 4, 7, 8, 12 and 13 as input/output (I tried using 0 and 1 for GPIO - no go - 0 flashes
+// on power up - 1 has pullup - best just avoid using these two for general IO - use for serial IO).
 // remember 13 probably has a LED attached on the board so best used for output.
-// You could use A0 (14), A1 (15), A2 (16) and A3 (17) as analog in - possibly 
-// A6 (20) and A7 (21) if available. Set to 1.1v full scale.
+//
+// You could use A0 (14), A1 (15), A2 (16) and A3 (17) as analog or digital inputs - possibly
+// A6 (20) and A7 (21) if available on your board. Set to 1.1v full scale.
 // A4 and A5 are used for the I2c where A4 is SDA and A5 is SCL.
-// On the blog at https://tech.scargill.net you'll see several examples of using I2c with various boards.
+//
+// On the blog at https://tech.scargill.net you'll see several examples of using I2c.
 //
 // Late addition - servos - any of the pins 2-13 can be a servo. command is 11 - so device, command, pin, value
 // Send value 255 to disconnect a servo and note if ANY pin is set up as a servo you lose PWM options on pins 9 and 10.
-// Just disconnect all individually to get the PWM back (normally all disconnected at power up). 
+// Just disconnect all individually to get the PWM back (normally all disconnected at power up).
 // Values 0-180 but this varies with different servos. Mine buzzed at 0 !! See Arduino Servo library
 //
-// A simple i2c SLAVE - default device number 9 - reads instructions from
+// The board becomes a simple i2c SLAVE - default (programmable) device number 9 - reads instructions from
 // master and either sets outputs or returns inputs accordingly.
 //
-// Not yet used but check out the enable interrupt library here
-// https://github.com/GreyGnome/EnableInterrupt
-// could be used for edge-triggered interrupts on any pin for pulse counting
-//
 // There is also now a soft fade option for PWM, a tone generator and Dallas temperature chip support for up to 2 chips.
 // Here I use a simplified version of my DS18B20 code from years back. This starts the conversion at the END
 // of the code - so the first value is rubbish - read the blog as this is hidden - and there are no delays. On the assumption of one chip
 // per pin, no need for search either!
+
+// This version returns 6 bytes - the LAST one is a status byte - 1 if busy. I'm using serial for debug right now - so can't use 
+// serial command - simply scrap that if you want to use serial out.
+
 //
 #include <EEPROM.h>
 #include <Wire.h>
    default: break;  
     }
     busy=0;
-}
+}
Updated by Peter Scargill

File snippet.txt Modified

  • Ignore whitespace
  • Hide word diff
 // You could use A0 (14), A1 (15), A2 (16) and A3 (17) as analog in - possibly 
 // A6 (20) and A7 (21) if available. Set to 1.1v full scale.
 // A4 and A5 are used for the I2c where A4 is SDA and A5 is SCL.
-// On the blog at http://tech.scargill.net you'll see several examples of using I2c from various boards.
+// On the blog at https://tech.scargill.net you'll see several examples of using I2c with various boards.
 //
 // Late addition - servos - any of the pins 2-13 can be a servo. command is 11 - so device, command, pin, value
 // Send value 255 to disconnect a servo and note if ANY pin is set up as a servo you lose PWM options on pins 9 and 10.
 // https://github.com/GreyGnome/EnableInterrupt
 // could be used for edge-triggered interrupts on any pin for pulse counting
 //
-// There is also now a soft fade option for PWM, a tone generator and Dallas temperatur chip support for up to 2 chips.
+// There is also now a soft fade option for PWM, a tone generator and Dallas temperature chip support for up to 2 chips.
 // Here I use a simplified version of my DS18B20 code from years back. This starts the conversion at the END
 // of the code - so the first value is rubbish - read the blog as this is hidden - and there are no delays. On the assumption of one chip
 // per pin, no need for search either!
 #define NOTONE 14
 #define DALLAS1 15
 #define DALLAS2 16
+#define SETSERIAL 20 // 0 means turn serial off, by default on. Other values- 1=300, 2=1200, 3=2400, 4=9600, 5=28800, 6=57600, 7=115200 baud
 
 #define STRUCTBASE 0
 
           if (params[1]!=stored.t2) { stored.t2=params[1]; EEPROM.put(STRUCTBASE,stored); }   // no delay hence first value crap
           retparams[1]=tr2&255; retparams[0]=tr2>>8;
           break;  
+
+    case SETSERIAL:
+          switch (params[1]) {
+            case 0 : Serial.end(); break;
+            case 1 : Serial.begin(300); break;
+            case 2 : Serial.begin(1200); break;
+            case 3 : Serial.begin(2400); break;
+            case 4 : Serial.begin(9600); break;
+            case 5 : Serial.begin(28800); break;
+            case 6 : Serial.begin(57600); break;
+            case 7 : Serial.begin(115200); break;
+            default: break;            
+          }
+          break;
                                  
    default: break;  
     }
Updated by Peter Scargill

File snippet.txt Modified

  • Ignore whitespace
  • Hide word diff
 byte fade[12][3];
 Servo myservos[14]; // just for ease - so use any pin from 3 to 13... bit of waste but so what.
 
-// Here's the Dallas code
+// Here's the Dallas code - end user need to spot negative values...see https://datasheets.maximintegrated.com/en/ds/DS18B20.pdf
 int16_t dallas (int x)
 {
   OneWire ds(x);
       ds.write(0xBE);
       for (i=0;i<2; i++) data[i]=ds.read();
       result=(data[1]<<8)|data[0];
-      //result>>=4; if (data[1]&128)  result |=0xf000;
-      //if (data[0]&8) ++result;
+ 
       ds.reset();
       ds.write(0xCC);
       ds.write(0x44,1);
Updated by Peter Scargill

File snippet.txt Modified

  • Ignore whitespace
  • Hide word diff
 // are not too keen on even a bright LED on their IO pins and from an operating
 // system like Linux, getting PWM on several pins is just not on... so - plug in this device 
 // (default ID 9) with pullups (always needed for I2c) and you can gain PWM, ADC 
-// and general IO for very little money. I originally put an SPI display on here but that 
-// ate into too many otherwise useful pins.
-// indeed now (July 2017) we have GPIO and I2c running on the NEO type devices and I've blogged
-// setting this peripheral to run (easily) with these boards.
-
+// and general IO for very little money. 
+//
 // So as a guide you could use 3,5,6 and 9, 10 and 11 for PWM (unless you use these pins for general IO)
 // you can use 2, 4, 7, 8, 12 and 13 as input/output (I tried using 0 and 1 - no go 0 flashes 
 // on power up - 1 has pullup - best just avoid for GPIO 0 - use for serial IO).
 // https://github.com/GreyGnome/EnableInterrupt
 // could be used for edge-triggered interrupts on any pin for pulse counting
 //
+// There is also now a soft fade option for PWM, a tone generator and Dallas temperatur chip support for up to 2 chips.
 // Here I use a simplified version of my DS18B20 code from years back. This starts the conversion at the END
-// of the code - so the first value is rubbish - but then there are no delays. On the assumption of one chip
+// of the code - so the first value is rubbish - read the blog as this is hidden - and there are no delays. On the assumption of one chip
 // per pin, no need for search either!
 //
 #include <EEPROM.h>
Updated by Peter Scargill

File snippet.txt Modified

  • Ignore whitespace
  • Hide word diff
 // https://github.com/GreyGnome/EnableInterrupt
 // could be used for edge-triggered interrupts on any pin for pulse counting
 //
+// Here I use a simplified version of my DS18B20 code from years back. This starts the conversion at the END
+// of the code - so the first value is rubbish - but then there are no delays. On the assumption of one chip
+// per pin, no need for search either!
 //
 #include <EEPROM.h>
 #include <Wire.h>
 #include <Servo.h> /// note that if you use ANY servo, you lose PWM on pins 9 and 10.
 #include <avr/pgmspace.h>
-
+#include <OneWire.h>
 #define MAXPORTS 21
 
-#define EEPROM_CHSM 0
-#define EEPROM_ID 1
-#define EEPROM_USE_QTECH 2
-
 #define SET_OUTPUT  1
 #define READ_INPUT  2
 #define READ_INPUT_PULLUP 3
 #define FADE 12
 #define TONE 13
 #define NOTONE 14
+#define DALLAS1 15
+#define DALLAS2 16
+
+#define STRUCTBASE 0
+
+byte busy=0;
+struct STORAGE{
+byte chsm;
+byte device;
+byte t1;
+byte t2;
+};
+
+int tr1=255;
+int tr2=255;
+
+STORAGE stored;
 
 byte ports[MAXPORTS];
 byte params[128];
-byte retparams[2];
+byte retparams[3];
 byte paramp;
-byte bigcount;
-byte device;
 
 long mymillis;
 
+
 const PROGMEM  uint8_t ledTable[256] = // Nano is so pathetically short of RAM I have to do this!
 {
   0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4,
 
 byte fade[12][3];
 Servo myservos[14]; // just for ease - so use any pin from 3 to 13... bit of waste but so what.
-  
+
+// Here's the Dallas code
+int16_t dallas (int x)
+{
+  OneWire ds(x);
+  byte i;
+  byte data[2];
+  int16_t result;
+      ds.reset();
+      ds.write(0xCC);
+      ds.write(0xBE);
+      for (i=0;i<2; i++) data[i]=ds.read();
+      result=(data[1]<<8)|data[0];
+      //result>>=4; if (data[1]&128)  result |=0xf000;
+      //if (data[0]&8) ++result;
+      ds.reset();
+      ds.write(0xCC);
+      ds.write(0x44,1);
+      return result;
+}
+
+
 void setup(void) {
   // NO serial if using using 0-7 as port expansion (I'm not)
   // If you want serial - set the speed in the setup routine, if not, comment out
   byte eeprom1,eeprom2;
   analogReference(INTERNAL);  // 1.1v
   Serial.begin(115200);
-  
+ // get info out of EEPROM
+ EEPROM.get(STRUCTBASE,stored);
+ 
  // first check if EEPROM info is valid?
- if (EEPROM.read(EEPROM_CHSM)!=0x3b)
+ if (stored.chsm!=0x3d)
    {
-    for (a = 0 ; a < EEPROM.length() ; a++) EEPROM.write(a, 0);
-    EEPROM.write(EEPROM_CHSM,0x3b);
-    EEPROM.write(EEPROM_ID,9);      // default I2c address 9
-    EEPROM.write(EEPROM_USE_QTECH,1); /// default ON for QTECH
+    stored.chsm=0x3d;
+    stored.device=9;
+    stored.t1=255;
+    stored.t2=155;
+    EEPROM.put(STRUCTBASE,stored);
    }
-  device=EEPROM.read(EEPROM_ID); // programmable address
-  bigcount=0;
-  Wire.begin(device);           // join i2c bus with address #9 by default
+
+  for (a=0;a<MAXPORTS;a++) ports[a]=0; // all inputs
+  Wire.begin(stored.device);           // join i2c bus with address #9 by default
   Wire.onReceive(receiveEvent);
   Wire.onRequest(requestEvent); 
-  for (a=0;a<MAXPORTS;a++) ports[a]=0;
+  
   paramp=0;
   Serial.begin(115200);
   mymillis=0;
   for (a=0;a<12;a++){ fade[a][0]=0; fade[a][2]=0; }
   for (a=0;a<128;a++) params[a]=0;
+
+  delay(100);
   
+  if (stored.t1!=255) tr1=dallas(stored.t1);
+  if (stored.t2!=255) tr2=dallas(stored.t2);  
+
+  tr1=85*16;
+  tr2=85*16;
+
 }
 
 void loop() {
             }
         }
     }  
+
 }
 
 // function that executes whenever data is requested by master
 // this function is registered as an event, see setup()
 void requestEvent() {
-  Wire.write(retparams,2);
+      retparams[2]=busy;
+      Wire.write(retparams,3); 
 }
 
 // function that executes whenever data is requested by master
 // this function is registered as an event, see setup()
 void receiveEvent(int count) {
+busy=1;
 int a;
 int tcount;
 tcount=count;
-paramp=0; 
+paramp=0;
 for (a=0;a<6;a++) params[a]=0; 
   // Nothing time consuming or visual debugging in here if a RETURN VALUE is expected or the routine to send a byte back could be missed.
   while ((tcount--)&&(paramp<128))
           uint16_t anback; anback=analogRead(params[1]); retparams[0]=anback>>8; retparams[1]=anback&255;
           break;    
     case SET_ADDRESS:
-          EEPROM.update(EEPROM_ID,params[1]);
+          stored.device=params[1]; EEPROM.put(STRUCTBASE,stored);
           // update address - will take effect on next powerup of the device as you 
           // can only call "begin" once
           break;
                  myservos[params[1]].write(params[2]);
                  break; 
     case FADE:
-          if (ports[params[1]]!=4) { ports[params[1]]=4; pinMode(params[1],OUTPUT); } 
+          if (ports[params[1]]!=4) { ports[params[1]]=4; pinMode(params[1],OUTPUT);  } 
           fade[params[1]][0]=1; fade[params[1]][2]=params[2];
           break;  
 
           if ((params[4]|params[5])==0) tone(params[1],(params[2]<<8)+params[3]); else tone(params[1],(params[2]<<8)+params[3],(params[4]<<8)+params[5]); 
           ports[params[1]]=0;
           break;
-          
-          
+             
     case NOTONE:  // can't do PWM on pins 3 and 11 while doing TONE...
           noTone(params[1]); ports[params[1]]=0; 
           break;
-                             
+
+    case DALLAS1:
+          tr1=dallas(params[1]); 
+          if (params[1]!=stored.t1) { stored.t1=params[1];  EEPROM.put(STRUCTBASE,stored); } // no delay hence first value crap
+          retparams[1]=tr1&255; retparams[0]=tr1>>8; 
+          break;
+
+    case DALLAS2:
+          tr2=dallas(params[1]); 
+          if (params[1]!=stored.t2) { stored.t2=params[1]; EEPROM.put(STRUCTBASE,stored); }   // no delay hence first value crap
+          retparams[1]=tr2&255; retparams[0]=tr2>>8;
+          break;  
+                                 
    default: break;  
     }
-}
+    busy=0;
+}
  1. 1
  2. 2
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.