Skip to content

Instantly share code, notes, and snippets.

View Marzogh's full-sized avatar
🏔️
Off the grid - as much as possible

Marzogh Marzogh

🏔️
Off the grid - as much as possible
View GitHub Profile
@Marzogh
Marzogh / LED color functions
Created March 15, 2015 13:11
LED color codes for Arduino and other Microprocessors
// The ledIO() function turns on or off an LED (in a particular color - if RGB) based on the mode passed to it. The mode can be the color of LED - in lower case - if RGB
//or simply on or off if a single color LED
void ledIO(uint8_t mode)
{
switch (mode) {
case 1: //RED
digitalWrite(RLED, HIGH);
digitalWrite(GLED, LOW);
digitalWrite(BLED, LOW);
break;
@Marzogh
Marzogh / Code to add Date and Time to files written to SD cards.ino
Last active March 8, 2024 19:40
This code allows you to add the date and time to your SD card log files generated by an Arduino.
#include <SD.h>
File file;
const uint8_t CS = 10; // SD chip select
// YOUR SKETCH SHOULD UPDATE THESE SIX VALUES
// EACH TIME BEFORE CREATING YOUR SD CARD FILE
unsigned int year = 2015;
byte month = 6;
byte day = 18;
Datatypes
1. void --> Returns no information
2. boolean --> 8 bit (0 or 1)
3. char --> signed 8-bit (-128 to 127)
4. Unsigned char/byte --> unsigned 8-bit (0 to 255)
5. int/short --> signed 16-bit (-32,768 to 32,767)
Due --> signed 32-bit (-2,147,483,648 to 2,147,483,647)
6. unsigned int/word --> unsigned 16-bit (0 to 65,535)
Due --> unsigned 32-bit (0 to 4,294,967,295)
int calcNoOfDays(int Month, int Year)
{
int noOfDays[] =
{ 0, 31, 0, 31, 30, 31, 30, 31,
31, 30, 31, 30, 31
};
if (Month == 2) {
if (Year % 4 == 0 || Year % 400 == 0)
noOfDays[2] = 29;
else if (Year % 4 != 0 || Year % 100 == 0)
#include <TinyGPS++.h> // For gps
#include <SoftwareSerial.h> // For communicating with the gps
struct dateTime { // Stores date and time for DateTime calculations
int Year;
int Month;
int Date;
};
dateTime current;
// This custom version of delay() ensures that the gps object
// is being "fed".
static void smartDelay(unsigned long ms)
{
unsigned long start = millis();
do
{
while (ss.available())
gps.encode(ss.read());
} while (millis() - start < ms);
@Marzogh
Marzogh / CharlieplexingTiny85.c
Created August 20, 2015 12:46
Charlieplexing 6 LEDs on ATTiny85
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
void setLED(uint8_t LED);
uint8_t currentLED;
uint8_t times = 0;
/*
@Marzogh
Marzogh / File size converter.c
Created August 28, 2015 00:48
Easy format to use while converting between different storage sizes
knownUnits = new Dictionary<string, long>
{
{ "", 1L }, // no unit is same as unit B(yte)
{ "B", 1L },
{ "KB", 1024L },
{ "MB", 1024L * 1024L},
{ "GB", 1024L * 1024L * 1024L},
{ "TB", 1024L * 1024L * 1024L * 1024L}
// fill rest as needed
};
@Marzogh
Marzogh / arduinoMemory.cpp
Last active October 10, 2016 00:01
This function places the current value of the heap and stack pointers in the variables. You can call it from any place in your code and save the data for outputting or displaying later. This allows you to check at different parts of your program flow. The stack pointer starts at the top of RAM and grows downwards. The heap pointer starts just ab…
//This function returns the amount of RAM currently in available
uint8_t * heapptr, * stackptr;
void check_mem() {
stackptr = (uint8_t *)malloc(4); // use stackptr temporarily
heapptr = stackptr; // save value of heap pointer
free(stackptr); // free up the memory again (sets stackptr to 0)
stackptr = (uint8_t *)(SP); // save value of stack pointer
Serial.print(F("Heap: "));
Serial.println(*heapptr);
@Marzogh
Marzogh / Smooth_analog_values.ino
Last active May 23, 2016 23:24
Function to smooth out values from analog sensors
int sampleData(int analogPin, int samples = 8, int sampleInterval = 50) {
int sampleData[samples];
int val = 0;
for (int i = 0; i<samples; i++) {
sampleData[i] = analogRead(analogPin);
val = val + sampleData[i];
delay(sampleInterval);
}
val = (val / samples);
return map(val, 550, 1023, 100, 0);