Skip to content

Instantly share code, notes, and snippets.

@mcgodfrey
mcgodfrey / homebrew_temperature_logger_log_file.c
Created Mar 26, 2016
Log to SD function from homebrew temperature logger
View homebrew_temperature_logger_log_file.c
/*
* Logs current temperature data to SD card.
* Note that the global variable "filename" must be initialised
* File format is:
* datestring,sensor0_name,temp0,sensor1_name,temp1,sensor2_name,temp2
* datestring,sensor0_name,temp0,sensor1_name,temp1,sensor2_name,temp2
*/
byte log_temps(char *date_str){
File f = SD.open(filename, FILE_WRITE);
if(f) {
@mcgodfrey
mcgodfrey / homebrew_temperature_logger_temperature.c
Created Mar 26, 2016
Code snippet for temperature measurement in homebrew temperature logger
View homebrew_temperature_logger_temperature.c
void measure_temps(){
DateTime t = rtc.now(); //get the current timestamp
datetime2str(t, date_str);
//trigger a temperature measurement
sensors.begin();
sensors.setWaitForConversion(false);
sensors.requestTemperatures();
conversionTimer.restart(); //Set up timer for temperature conversion to complete
return;
@mcgodfrey
mcgodfrey / homebrew_temperature_logger_buttons.c
Created Mar 26, 2016
Code snippet for button presses in homebrew temperature logger
View homebrew_temperature_logger_buttons.c
void loop() {
//...
//poll the buttons
button_up.poll();
button_down.poll();
button_select.poll();
@mcgodfrey
mcgodfrey / homebrew_temperature_logger_timers.c
Created Mar 26, 2016
Code snippet for timers in homebrew temperature logger
View homebrew_temperature_logger_timers.c
void loop() {
//poll the timers
//if any of these have expired then their callback will automatically run.
conversionTimer.run();
measTimer.run();
display_timeout.run();
//...
//reset the display timeout timer if a button was pressed
if(button_up.pushed() || button_down.pushed() || button_select.pushed()){
display_timeout.restart();
@mcgodfrey
mcgodfrey / mojo_top_servo_demo.v
Last active Mar 6, 2016
Example servo control module in Verilog. Sweeps 4 servo motors back and forth at different speeds.
View mojo_top_servo_demo.v
/*
Servo motor control demo module
Four servos swing back and forth with different speeds.
*/
module mojo_top(
input clk, // 50MHz clock input
//remaining default inputs/outputs omitted
output [3:0] servo_out //PWM output lines for each servo
);
//Usual setup code omitted (setting SPI lines, reset, etc.
@mcgodfrey
mcgodfrey / servo_controller.v
Last active Dec 12, 2021
Servo controller in verilog. Takes an 8-bit position and converts it into the pwm signal required by a servo motor.
View servo_controller.v
/*
servo controller
Based on code from Mojo tutorial
https://embeddedmicro.com/tutorials/mojo/servos
Takes an 8-bit position as an input
Output a single pwm signal with period of ~20ms
Pulse width = 1ms -> 2ms full scale. 1.5ms is center position
*/
module servo_controller (
@mcgodfrey
mcgodfrey / counter.v
Last active Apr 23, 2020
Up-down counter (8-bit) in verilog for servo control example
View counter.v
/*
8 bit up-down counter.
Based on code from Mojo tutorial
https://embeddedmicro.com/tutorials/mojo/pulse-width-modulation
/\ /\
/ \ / \
/ \/ \
The CRT_LEN determines the period of the resulting counter
*/
module counter #(parameter CTR_LEN = 27) (
@mcgodfrey
mcgodfrey / power_supply.c
Last active Jan 1, 2016
Code for power supply display
View power_supply.c
/*
* Voltage and current monitor for lab supply
*
* LCD and analog input pin assignments as shown below
*
* Matt Godfrey, 2015
*/
#include <LiquidCrystal.h>