Skip to content

Instantly share code, notes, and snippets.

View nadavmatalon's full-sized avatar

Nadav Matalon nadavmatalon

View GitHub Profile
@nadavmatalon
nadavmatalon / port_manipulation.cpp
Last active February 22, 2024 04:24
Arduino: Port Manipulation
// 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 / .gitattributes
Created October 24, 2016 23:08
Github: Exclude Folder from Repo's Language Statistics
extras/* linguist-documentation
@nadavmatalon
nadavmatalon / mapf.ino
Created October 12, 2016 15:13
ARDUINO: MAP FLOAT FUNCTION
double mapf(double val, double in_min, double in_max, double out_min, double out_max) {
return (val - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
@nadavmatalon
nadavmatalon / GPIOR.cpp
Last active February 21, 2021 21:39
AVR: GPIOR (General Purpose I/O Register)
/*
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:
// http://playground.arduino.cc/Main/I2cScanner
// -------------
// i2c_scanner
// -------------
#include <Wire.h>
void setup() {
Wire.begin();
@nadavmatalon
nadavmatalon / MCP3221_BASIC.ino
Created December 2, 2016 12:33
MCP3221 Basic Usage
#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 November 13, 2016 04:31
Arduino: Debounced Switch (ISRs only, no polling)
/*
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 November 8, 2016 16:47
Union & Struct Example
typedef struct Scr_t {
union {
u16 data;
struct {
u16 tile : 10;
u16 hflip : 1;
u16 vflip : 1;
u16 pal : 4;
};
};
@nadavmatalon
nadavmatalon / struct.ino
Created November 8, 2016 16:46
Struct Example
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 November 8, 2016 16:40
Pointers & Addresses
int i = 42;
int *p = &i;
// '*' means 'points to' and '&' means 'address of' ('p' is a pointer to the address of integer 'i')