Skip to content

Instantly share code, notes, and snippets.

Nadav Matalon nadavmatalon

  • London, UK
Block or report user

Report or block nadavmatalon

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View MCP3221_BASIC.ino
#include "MCP3221.h"
const byte DEV_ADDR = 0x4D; // I2C address of the MCP3221 (Change if needed)
MCP3221 mcp3221(DEV_ADDR); // constructs a new MCP3221 object with the relevant I2C address
void setup() {
Serial.begin(9600); // initiallizes the Serial Communications Port (at 9600bd)
Wire.begin(); // initiallizes the I2C Communications bus
while(!Serial); // waits for Serial Port to initialize
@nadavmatalon
nadavmatalon / debounced_switch_interrupt.ino
Last active Nov 13, 2016
Arduino: Debounced Switch (ISRs only, no polling)
View debounced_switch_interrupt.ino
/*
HOOK-UP: NORMALLY OFF
---------------------
Connect one pin of the push-button or toggle switch to pin D5.
Connect a 10K pull-down resistor between the above pin and GND.
Connect the other pin of the push-button or toggle switch to 5V.
HOOK-UP: NORMALLY ON
--------------------
Connect one pin of the push-button or toggle switch to pin D5.
@nadavmatalon
nadavmatalon / union_struct.ino
Created Nov 8, 2016
Union & Struct Example
View union_struct.ino
typedef struct Scr_t {
union {
u16 data;
struct {
u16 tile : 10;
u16 hflip : 1;
u16 vflip : 1;
u16 pal : 4;
};
};
View struct.ino
struct hours_byte {
unsigned int format: 1; // range: 0-1, 0=24h 1=12h
unsigned int ampm: 1; // range: 0-1, 0=AM 1=PM
unsigned int tenshours: 2; // range: 0-2
unsigned int hours: 4; // range: 0-15 (guessing it never goes over 9)
};
void pHour(hours_byte when) {
Serial.print(when.tenshours, DEC);
Serial.print(when.hours, DEC);
@nadavmatalon
nadavmatalon / ptrAddr.ino
Created Nov 8, 2016
Pointers & Addresses
View ptrAddr.ino
int i = 42;
int *p = &i;
// '*' means 'points to' and '&' means 'address of' ('p' is a pointer to the address of integer 'i')
@nadavmatalon
nadavmatalon / bitCode.ino
Created Nov 7, 2016
bitSet & bitClear: Expanded Macros
View bitCode.ino
// Reference: http://stackoverflow.com/questions/16683146/can-macros-be-overloaded-by-number-of-arguments
#ifdef bitSet
#undef bitSet
#endif
#ifdef bitClear
#undef bitClear
#endif
@nadavmatalon
nadavmatalon / GPIOR.cpp
Last active Nov 1, 2016
AVR: GPIOR (General Purpose I/O Register)
View GPIOR.cpp
/*
GPIOR
-----
Three general purpose I/O registers that can be used for storing any information (GPIOR0, GPIOR1 and GPIOR2)
These registers are particularly useful for storing global variables and status flags, since they are accessible
to bit-specific instructions such as SBI, CBI, SBIC, SBIS, SBRC, and SBRS.
Note that only GPIOR0 is bit-addressable
References:
@nadavmatalon
nadavmatalon / port_manipulation.cpp
Last active Aug 23, 2019
Arduino: Port Manipulation
View port_manipulation.cpp
// DDRD (R/W) pin direction (0 = INPUT / 1 = OUTPUT)
// PORTD (R/W) pin state (INPUT: 0 = LOW / 1 = HIGH | OUTPUT: 0 = PULL-UP DIACTIVATED / 1 = PULL-UP ACTIVATED)
// PIND (R) pin state (INPUT ONLY: 0 = LOW / 1 = HIGH)
// bit(n) // calculates value of n-th bit (returns: 0 / 1)
// bitRead(byteName, n) // gets value of n-th bit of byte (returns: 0 / 1)
// bitSet(byteName, n) // sets value of n-th bit of byte to 1
// bitClear(byteName, n) // sets value of n-th bit of byte to 0
// bitWrite(byteName, n, val) // sets value of n-th bit of byte to 0 or 1
@nadavmatalon
nadavmatalon / bitmath.cpp
Last active Oct 25, 2016
Bitmath Operations
View bitmath.cpp
// Source & much additional info:
// http://playground.arduino.cc/Code/BitMath
y = (x >> n) & 1; // n=0..7. stores nth bit of x in y. y becomes 0 or 1.
x &= ~(1 << n); // forces nth bit of x to be 0. all other bits left alone.
x &= (1<<(n+1))-1; // leaves alone the lowest n bits of x; all higher bits set to 0.
x |= (1 << n); // forces nth bit of x to be 1. all other bits left alone.
x ^= (1 << n); // toggles nth bit of x. all other bits left alone.
x = ~x; // toggles ALL the bits in x.
@nadavmatalon
nadavmatalon / .gitattributes
Created Oct 24, 2016
Github: Exclude Folder from Repo's Language Statistics
View .gitattributes
extras/* linguist-documentation
You can’t perform that action at this time.