/*
* Al-Zohra n'est pas née en un jour
* Zohra was not born in a day
*
* Artistic installation from Randa Mirza
*
* Code : Jérôme Abel
* Date : 09/08/2016
* License : GNU/GPL v3
* Website : http://reso-nance.org/wiki/projets/zohrawasnotborninaday/accueil
*
* Electronic setup :
* + Arduino UNO
* + FabScan Shield
* + SilentStepStick 5V (Trinamic TMC2100 Stepper Motor Driver) with 20V-28V power supply
* + SilentStepStick Protector
* + Motor stepper Nema 17 (1704HS168A)
* + TinkerKit MOSFET (for the light)
*
* Wiring setup :
* SilentStepStick (pins 2,3,4)
* Mosfet (pin 11, 5V, GND)
*
* Main principle :
* An Arduino controls a light with a mosfet and a motor stepper with a quiet driver (for inside installation)
*
*/
// Pins Stepper Driver (TMC2100 SilentStick 5V)
#define EN_PIN 2
#define STEP_PIN 3
#define DIR_PIN 4
#define MS_PIN A5 // HIGH for 16 microstepping, LOW for no microstepping
#define STEPS 200 // 200 pas pour un tour
#define MICROSTEPS 16 // microstepping 1/16
// Pins Mosfets Lamp
#define LAMP_PIN 11
// ---------------- Variables du scénario ---------------- //
int wait = 29000; // temps d'attente de départ
int lamp_fadein = 29000; // temps de fade in
int lamp_intensity = 200; // intensité de la lampe (entre 0 et 255)
int lamp_fadeout = 5000; // temps de fade out
int motor_turns = 5 ;// nombre de tours en montéé
int motor_turns_ms = 20000; // temps par tour en millisecondes
int motor_break = 7000 ; // temps d'attente en position haute
//---------------- Démarrage ---------------- //
void setup() {
// Setup Stepper Driver
pinMode(EN_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
//pinMode(MS_PIN, OUTPUT);
digitalWrite(EN_PIN, HIGH); // Désactiver le driver
digitalWrite(DIR_PIN, LOW); // LOW or HIGH
digitalWrite(STEP_PIN, LOW);
//digitalWrite(MS_PIN, LOW); // No microstepping
// Allumer la lampe au démarrage
pinMode(LAMP_PIN, OUTPUT);
analogWrite(LAMP_PIN, 0);
delay(2000); // Délai de sécurité
digitalWrite(EN_PIN, LOW); // Activer le driver
}
//---------------- Boucle du scénario - programme ---------------- //
void loop() {
delay(wait);
fadein(LAMP_PIN, lamp_intensity, lamp_fadein); // pin (lamp), intensity max, temps total
motorON();
motorGo(motor_turns, motor_turns_ms); // nombre de tours, millisecondes par tour (minimum 7000 !)
delay(motor_break);
fadeout(LAMP_PIN, lamp_intensity, lamp_fadeout); // pin (lamp), intensity max, temps total
motorOFF();
}
//---------------- Fonctions utiles ---------------- //
void motorON() {
digitalWrite(EN_PIN, LOW);
}
void motorOFF() {
digitalWrite(EN_PIN, HIGH);
}
void motorGo(int _tour, int _tourms){
// Delai entre chaque pas (vitesse en millisecondes)
int _delay = (int) (_tourms / (STEPS * MICROSTEPS) ) * 1000 / 2;
// Exécuter le nombre de tours
for(int x = 0; x < (_tour * STEPS * MICROSTEPS); x++) {
digitalWrite(STEP_PIN,LOW);
delayMicroseconds(_delay);
digitalWrite(STEP_PIN,HIGH);
delayMicroseconds(_delay);
}
}
void fadeout (int _pin, int _intensity, int _time){
int new_time = (int) (_time / 255);
for (int i = 0; i <= 255; i++) {
int lampBright = 255 - i;
analogWrite(_pin, map(lampBright, 0, 255, 0, _intensity));
delay(new_time);
}
}
void fadein (int _pin, int _intensity, int _time){
int new_time = (int) (_time / 255);
for (int i = 0; i <= 255; i++) {
analogWrite(_pin, map(i, 0, 255, 0, _intensity));
delay(new_time);
}
}
// Include the Stepper Library
#include <Stepper.h>
// VARAIABLES INTERESSANTES
int leds_up = 0; //
int wait = 29000; // blablablabla
int lamp_up = 29000; //
int motor_up = 7 ; // = 7 giris = 42 sec if speed = 5
int motor_break = 7000 ;// after leds down
int lamp_down = 5000;
int motor_down = 7 ; // = 7 giris = sec if speed = 40
// ============================= //
const int leds = 6;
const int lamp = 5;
// Map our pins to constants to make things easier to keep track of
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;
// The amount of steps for a full revolution of your motor.
// 360 / stepAngle
const int STEPS = 200;
// Initialize the Stepper class
Stepper myStepper(STEPS, dirA, dirB);
void setup() {
// Set the RPM of the motor
myStepper.setSpeed(60);
// Turn on pulse width modulation
pinMode(pwmA, OUTPUT);
pinMode(pwmB, OUTPUT);
digitalWrite(pwmA, HIGH);
digitalWrite(pwmB, HIGH);
// Turn off the brakes
pinMode(brakeA, OUTPUT);
pinMode(brakeB, OUTPUT);
digitalWrite(brakeA, LOW);
digitalWrite(brakeB, LOW);
// Log some shit
Serial.begin(9600);
// LEDS variano da 0 (spento) a 255 (acceso full)
analogWrite(leds, 10);
// LAMP varia da 0 a 255
analogWrite(lamp, 0);
}
void motorON() {
digitalWrite(pwmA, HIGH);
digitalWrite(pwmB, HIGH);
}
void motorOFF() {
digitalWrite(pwmA, LOW);
digitalWrite(pwmB, LOW);
}
void fadeout (int _pin, int _intensity, int _time){
int time = (int) (_time / 255);
for (int i = 0; i <= 255; i++) {
int lampBright = 255 - i;
analogWrite(_pin, map(lampBright, 0, 255, 0, _intensity));
delay(time);
}
}
void fadein (int _pin, int _intensity, int _time){
int time = (int) (_time / 255);
for (int i = 0; i <= 255; i++) {
analogWrite(_pin, map(i, 0, 255, 0, _intensity));
delay(time);
}
}
void loop() {
delay(wait);
fadein(lamp, 200, lamp_up); // pin (leds or lamp), intensity max, temps total
motorON();
myStepper.setSpeed(2); // 5 giri al minuto ==> 1 giri = 3 sec.
myStepper.step(STEPS * motor_up);
delay(motor_break);
fadeout(lamp, 200, lamp_down);
motorOFF();
}