working on it ...

Filters

Explore Public Snippets

Sort by

Found 21k snippets

    public by RobertKnoester modified Jun 4, 2017  136  1  4  0

    comment_line

    comment_line
    //////////////////////////////////////////////////////////////////

    public by javieralejandroenc20 modified Feb 27, 2017  415  0  4  0

    Untitled

    c++
    // Enter here the actual content of the snippet.            

    public by axydus modified Jan 27, 2017  19897  1  4  0

    C++_SwitchCase(axyd)

    Simple switch...case for menu choice
    int choice= 0;
    cin>>choice;
    
    switch(choice){
    	case 1:
    		
    		break;
    	case 2:
    		
    		break;
    	case 3:
    		
    		break;
    	case 4:
    		
    		break;
    	default :
    		
    		break;		
    }

    public by arijitiit modified Sep 26, 2016  1586  2  4  0

    C++_1

    mACROS
    // Enter here the actual content of the snippet.            
    
    typedef long long ll;
    typedef pair<int, int> ii;
    typedef vector<ii> vii;
    typedef vector<int> vi;
    #define INF 1000000000

    public by tomsim modified Aug 21, 2016  1806  0  5  0

    Rotary Encoder State table

    Read Rotary Encoder using state table instead of interrupt
    /* Common 2 pins rotary encoder (ignore push button pin)
      Default pin output = (0,0)
      Read each pin and combine them into one value
      then use it to index the state table to advance to next state
      State sequence 00->01->11->10->00 return -1
          Next state  0   1   3   2   0
                     00->10->11->01->00 return +1
                      0   2   3   1   0
    */
    const int ENC_A = 2;
    const int ENC_B = 3;
    
    // State table
    static uint8_t stb[][4] =
    {
    //       0 1 2 3
            {8,2,1,8}, //0 start here v=0
            {0,8,1,3}, //1 0*2(-3-1-0 -1
            {0,8,2,4}, //2 0*1(-3-2-0
            {8,5,1,3}, //3 0-2*3(-1-0 -1
            {8,2,6,4}, //4 0-1*3(-2-0
            {7,5,8,3}, //5 0-2-3*1(-0 -1
            {9,8,6,4}, //6 0-1-3*2(-0
    };
    static int encRead( int pa, int pb)
    {
    	int8_t cx = 0; 			// initial state 0
    	int errCnt = 0;
    	while (errCnt < 1550)
    	{
    		uint8_t v = digitalRead(pa) | digitalRead(pb)<<1;
    
    		if (v < 4)
    		{
    			uint8_t nx = stb[cx][v];
    
    			if (nx > 6)
    			{
    		    if (nx == 8)
    		      errCnt++;
    				else
    					return (8 - (int)nx);
    			}
    			else
    			{
    				cx = nx;
    			}
    		}
    		else
    		{
    			cx = 0;
    			errCnt++;
    		}
    	}
    
    	return 0;
    }
    
    void setup()
    {
    	Serial.begin(9600);
    
    	pinMode( ENC_A, INPUT_PULLUP);
    	pinMode( ENC_B, INPUT_PULLUP);
    
    }
    
    const int MAX_VAL = 64*6 -1;
    
    int lastPos = 0;
    
    void loop() {
    
    	unsigned long 	sampleTime = millis();
    	int rv = encRead(ENC_A,ENC_B);
    	sampleTime = millis() - sampleTime;
    
    	if (rv == 0)
    	{
    		delay(50);
    	}
    	else
    	{
    		lastPos += rv;
    		if (sampleTime < 20)
    			sampleTime = 20;
    		// simulate acceleration
    		lastPos += rv*int(9000.0/(sampleTime*sampleTime));
    
    		if (lastPos < 0) lastPos = 0;
    		if (lastPos > MAX_VAL) lastPos = MAX_VAL;
    
    		Serial.print(lastPos); Serial.print(" "); Serial.println(sampleTime);
    
    	}
    
    }
    
    
    
    
    
                                        

    public by tomsim modified Aug 16, 2016  1673  1  5  0

    Dallas sensor to thingspeak

    /* Code snippet to use Dallas sensor with ThingSpeak
     * Use lib Arduino manager to install ThingSpeak and DallasTemperature libraries
     */
    
    #include "ThingSpeak.h"
    #include <ESP8266WiFi.h>
    #include <OneWire.h>
    #include <DallasTemperature.h>
    
    ...
    /*============ Dallas device setup ==================*/
    // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
    OneWire oneWire(ONE_WIRE_BUS);
    
    // Pass our oneWire reference to Dallas Temperature. 
    DallasTemperature tempSensor(&oneWire);
    
    const int MAX_DEVICES = 3;
    static int nDeviceCount = 0;
    static DeviceAddress tempDev[MAX_DEVICES];
    
    static void findDeviceAddr(void)
    {
    	tempSensor.begin();
    	nDeviceCount = tempSensor.getDeviceCount();
    
    	if (nDeviceCount > MAX_DEVICES)
    		nDeviceCount = MAX_DEVICES;
    
    	for (int i=0; i < nDeviceCount; i++)
    	{
      		uint8_t *devAddr = tempDev[i];
    		if (tempSensor.getAddress(devAddr,i))
    			Serial.println("getAddress ok");
    	}
    }
    static float getTempF( DeviceAddress deviceAddress)
    {
    	float tempF = DallasTemperature::toFahrenheit( tempSensor.getTempC(deviceAddress));
    	return tempF;
    }
    
    ...
    /*============ ThingSpeak setup ==================*/
    // Note:  Each channel has its own number and write API key
    // API key is what get used - wrong channel number doesn't matter
    
    static unsigned long myChannelNumber = YOUR_CHANNEL_NUMBER;	// Customize
    static const char 	*myWriteAPIKey = "Your API KEY";		// Customize
    ...
    
    /*== Global client for ThingSpeak */
    static WiFiClient  client;
    
    static void sampleTemperature()
    {
    	// request to all devices on the bus
    	tempSensor.requestTemperatures(); // Send the command to get temperatures
    
    	for (int i=0; i < nDeviceCount; i++)
    	{
    		float tempF = getTempF(tempDev[i]);
    		Serial.print(String(i+1)+": "+tempF+"F ");
    		ThingSpeak.setField( i+1, tempF);
    	}
    	Serial.println();
    	if (nDeviceCount > 0)
    	{
    		int rc = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
    		Serial.println(String("Post rc=")+rc);
    	}
    }
    
    
    void setup () 
    {
      Serial.begin(115200);
    
    	/* Must Do Wifi setup first... */
    ...
    	/* Wifi should be ready after this */
    ...
    	ThingSpeak.begin(client);	// initialize ThingSpeak lib
    ...
    	findDeviceAddr();  	// locate OneWire devices on the bus
    ...
    }
    
    void loop () 
    {
    ...
    			if (itsTimeToSample)
    				sampleTemperature();
    ...
    }            

    public by tomsim modified Aug 16, 2016  2619  34  5  0

    ESP8266 WiFi auto-config

    Use WiFiManager library to auto config SSID
    #include <EEPROM.h>
    #include <ESP8266WiFi.h>
    #include <WiFiManager.h>         //https://github.com/tzapu/WiFiManager
    ...
    
    /*====== WiFiManager support stuff ========*/
    void configModeCallback (WiFiManager *myWiFiManager) {
      Serial.println("In WiFi config mode");
      Serial.println(WiFi.softAPIP());
      //if you used auto generated SSID, print it
      Serial.println(myWiFiManager->getConfigPortalSSID());
    }
    
    void setup () 
    {
      Serial.begin(115200);
      delay(200);
    ...
    	/* WiFi setup */
    	WiFiManager wifiManager;
    	wifiManager.setDebugOutput(false);
    //wifiManager.resetSettings(); /* reset settings - for testing */
    	//set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode
    	wifiManager.setAPCallback(configModeCallback);
    
    	//fetches ssid and pass and tries to connect
    	//if it does not connect it starts an access point with the specified name
    	//here  "AutoConnectAP"
    	//and goes into a blocking loop awaiting configuration
      if (!wifiManager.autoConnect("AutoConnectAP"))
    	{
    		Serial.println("failed to connect and hit timeout");
    		//reset and try again, or maybe put it to deep sleep
    		ESP.reset();
    		delay(1000);
    	}
    
    	/*if you get here you have connected to the WiFi*/
    	Serial.println("");
    	Serial.println("WiFi connected");
    	Serial.println(WiFi.localIP());
    ...
    }
    
    void loop()
    {
    ...
    }
    
    
    
                

    public by tomsim modified Aug 15, 2016  1471  3  5  0

    C Array Initialization

    char tab[][4] =
    {
    // 	 0 1 2 3
    	{7,1,2,0}, //0 start here v=3
    	{3,1,7,0}, //1 3*1(-0-2-3 -1
    	{4,7,2,0}, //2 3*2(-0-1-3
    	{3,1,5,7}, //3 3-1*0(-2-3 -1
    	{4,6,2,7}, //4 3-2*0(-1-3
    	{3,7,5,8}, //5 3-1-0*2(-3 -1
    	{4,6,7,9}, //6 3-2-0*1(-3
    };

    public by fickrymuhammad modified Apr 19, 2016  2395  1  4  -2

    Keypad

    /*
    This example reads the date and time from DS1307 Real-Time Clock
    and send this information to your PC using USB/Serial.
    Make the connections below, upload the code and open Serial Monitor.
    
    Made by Álvaro Justen aka Turicas
    
    Pin connections on DS1307 module:
    
    [DS1307] <--> [Arduino]
    5V       <--> 5V
    GND      <--> GND 
    SQW      <--> (not connected)
    SCL      <--> Analog Input 5
    SDA      <--> Analog Input 4
    
    This software is free software.
    */
    
    #include <string.h>
    #include <Wire.h>
    #include <DS1307.h>
    
    char dateTime[20];
    int RTCValues[7];
    
    void setup() {
        Serial.begin(9600);
        Serial.println("Reading information from RTC...");
    
        DS1307.begin();
    }
    
    void loop() {
        DS1307.getDate(RTCValues);
        //Year: two-digit, from 00 to 99
        //Month: two-digit, from 01 to 12
        //Day of month, from 01 to 31
        //Day of week, from 0 (sunday) to 6 (saturday)
        //Hour: 24-hour format, from 0 to 23
        //Minute: from 0 to 59
        //Second: from 0 to 59
    
        sprintf(dateTime, "20%02d-%02d-%02d %02d:%02d:%02d", RTCValues[0],
                RTCValues[1], RTCValues[2], RTCValues[4], RTCValues[5],
                RTCValues[6]);
        Serial.print(dateTime);
        Serial.print(" - day of week: ");
        Serial.println(fromNumberToWeekDay(RTCValues[3]));
    
        delay(1000);
    }

    public by fickrymuhammad modified Apr 13, 2016  1886  0  2  0

    Kaaaaaaaaaaa

    c++
    // Enter here the actual content of the snippet.            /*
      Button
    
     Turns on and off a light emitting diode(LED) connected to digital
     pin 13, when pressing a pushbutton attached to pin 2.
    
    
     The circuit:
     * LED attached from pin 13 to ground
     * pushbutton attached to pin 2 from +5V
     * 10K resistor attached to pin 2 from ground
    
     * Note: on most Arduinos there is already an LED on the board
     attached to pin 13.
    
    
     created 2005
     by DojoDave <http://www.0j0.org>
     modified 30 Aug 2011
     by Tom Igoe
    
     This example code is in the public domain.
    
     http://www.arduino.cc/en/Tutorial/Button
     */
    
    // constants won't change. They're used here to
    // set pin numbers:
    const int buttonPin = 2;     // the number of the pushbutton pin
    const int ledPin =  13;      // the number of the LED pin
    
    // variables will change:
    int buttonState = 0;         // variable for reading the pushbutton status
    
    void setup() {
      // initialize the LED pin as an output:
      pinMode(ledPin, OUTPUT);
      // initialize the pushbutton pin as an input:
      pinMode(buttonPin, INPUT);
    }
    
    void loop() {
      // read the state of the pushbutton value:
      buttonState = digitalRead(buttonPin);
    
      // check if the pushbutton is pressed.
      // if it is, the buttonState is HIGH:
      if (buttonState == HIGH) {
        // turn LED on:
        digitalWrite(ledPin, HIGH);
      } else {
        // turn LED off:
        digitalWrite(ledPin, LOW);
      }
    }
    • Public Snippets
    • Channels Snippets