Created
October 23, 2016 06:13
-
-
Save ArduinoBasics/03e193b2b4d4993cf76f41a68d55b7a9 to your computer and use it in GitHub Desktop.
This sketch will control various Magzor MIC boards using the Magzor I2C Arduino Shield on an Arduino MEGA to create a Stroboscopic animation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ================================================================================================================================================== | |
Project: ArduinoBasics: Stroboscopic Animation using Arduino MEGA | |
Author: Scott C | |
Created: 24th Apr 2016 | |
Arduino IDE: 1.6.4 | |
Website: http://arduinobasics.blogspot.com/p/arduino-basics-projects-page.html | |
Description: This sketch will control various Magzor MIC boards using the Magzor I2C Arduino Shield on an Arduino MEGA to | |
create a Stroboscopic animation. The Arduino will listen for a Hall effect sensor to be triggered by a magnet, | |
which will cause an LED to flash rapidly (like a strobe light), allowing some pictures rotating on a disk to | |
appear motionless. We will string 4 of these pictures together to create an animation. | |
Library required: It requires the Arduino MagzorI2C library (http://magzor.com/downloads), and the Wire library | |
NOTE: Some of this source code was AUTOMATICALLY generated by the Magzor MDP process: | |
//Format: Arduino, Build ID: b52016f6-c3a6-4ae0-92f9-892972f1f729 | |
================================================================================================================================================== */ | |
#include <Wire.h> | |
#include <MagzorI2C.h> | |
//Global Declarations | |
//MIC board address constants | |
static const uint8_t GPIO_BOARD_1_ADDRESS = 0x10; | |
static const uint8_t MOTOR_BOARD_1_ADDRESS = 0x30; | |
//MIC objects | |
I2CDevice_IOBoard io_board1(GPIO_BOARD_1_ADDRESS); | |
I2CDevice_MotorBoard motor_board1(MOTOR_BOARD_1_ADDRESS); | |
//device objects | |
// connector 0 on Magzor MIC: 10x GPIO board #1 | |
XPin led1 = io_board1.get_XPin(0); | |
// connector 1 on Magzor MIC: 10x GPIO board #1 | |
XPin hall_effect1 = io_board1.get_XPin(1); | |
// connector 1 on Magzor MIC: 2x DC Motor board #1 | |
XMotor motor1 = motor_board1.get_XMotor(1); | |
const int points =25; | |
uint8_t reset_pin = 0; | |
uint8_t interrupt_pin = 2; | |
unsigned long TimeBetweenEachFrame[points]; | |
int counter=0; | |
unsigned long diff = 0; | |
unsigned long current = 0; | |
unsigned long previous = 0; | |
void setup() { | |
// wait for MICs to initialize | |
delay(1500); | |
Wire.begin(); | |
Serial.begin(9600); | |
//register reset and interrupt pin | |
MagzorI2C::setup(reset_pin, interrupt_pin); | |
//register MIC boards | |
MagzorI2C::register_i2c_device(io_board1); | |
MagzorI2C::register_i2c_device(motor_board1); | |
motor1.set_direction(1); //set the direction of the motor | |
motor1.set_speed(250); //ramp up the motor | |
delay(100); | |
motor1.set_speed(130); //set the speed of the motor to 130 (Note : the maximum is 255) | |
previous=millis(); //initialise the "previous" variable | |
} | |
void loop() { | |
//Hall effect sensor will trigger LOW when it encounters the South side of a magnet | |
if(hall_effect1.digitalRead()==LOW){ | |
//Get the current time | |
current=millis(); | |
/*Calculate the time difference between each frame. | |
The time for one revolution of the disk is calculated by taking the previously triggered timestamp and subtracting it from the currently triggered timestamp. | |
We can then divide that time by 4 to get a rough estimate of the time between each frame. */ | |
diff = (current-previous)/4; | |
//Save the time between each frame in an array, so that we can transmit it to the Serial monitor later on | |
TimeBetweenEachFrame[counter] = diff; | |
//Save the current time for the next round of calculations | |
previous=current; | |
//The first 5 rounds, show frame #2. I have subtracted 2 to allow for processing time | |
if(counter<5){ | |
delay((diff*1)-2); | |
} | |
//The second 5 rounds, show frame #3. | |
if(counter>4&&counter<10){ | |
delay((diff*2)-2); | |
} | |
//The third 5 rounds, show frame #4. | |
if(counter>9&&counter<15){ | |
delay((diff*3)-2); | |
} | |
//The last 5 rounds, show frame #1, | |
if(counter>14&&counter<19){ | |
delay((diff*4)-2); | |
} | |
//When we reach the last round, reset the counter. Otherwise increment the counter. | |
if(counter>18){ | |
delay((diff*4)-2); | |
counter=0; | |
//dataDump(); //Uncomment if you want to know the speed of the disk - print the "Time between each frame" to serial monitor.. | |
} else { | |
counter++; | |
} | |
//Flash the LED for a fraction of a second. | |
led1.digitalWrite(HIGH); | |
led1.digitalWrite(LOW); | |
} | |
} | |
//Check the precision of rotation. | |
//Print the amount of milliseconds between each frame | |
//TimeBetweenEachFrame = Time for one complete rotation divided by 4. | |
void dataDump(){ | |
for(int i=0; i<points; i++){ | |
Serial.println(TimeBetweenEachFrame[i]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment