158 lines
3.4 KiB
C++
158 lines
3.4 KiB
C++
#include <TimerOne.h>
|
|
#define FACTOR 10
|
|
uint8_t pR[6] = {2, 4, 6, 8, 10, 12};
|
|
uint8_t pA[6] = {3, 5, 7, 9, 11, A5};
|
|
uint8_t mode = A2;
|
|
uint8_t iA = A0;
|
|
uint8_t iR = A1;
|
|
long unsigned int time_av[6] = {0,0,0,0,0,0};
|
|
long unsigned int time_rt[6] = {0,0,0,0,0,0};
|
|
long unsigned int time_av_prot[6] = {0,0,0,0,0,0};
|
|
long unsigned int time_rt_prot[6] = {0,0,0,0,0,0};
|
|
uint32_t time_pausa = 100;
|
|
|
|
void auto_mode()
|
|
{
|
|
//Serial.println(time_pausa);
|
|
avanza( 1, 1, 1, 1, 1, 1, time_pausa);
|
|
//avanza( -1, -1, -1, -1, -1, -1, time_pausa);
|
|
}
|
|
|
|
void manual_mode()
|
|
{
|
|
if (!digitalRead(iR))
|
|
{
|
|
for(int i=0; i<6; i++) avanza(i);
|
|
}
|
|
else if (!digitalRead(iA))
|
|
{
|
|
for(int i=0; i<6; i++) retrocede(i);
|
|
}
|
|
else
|
|
{
|
|
for(int i=0; i<6; i++) stop(i);
|
|
}
|
|
// delay(100);
|
|
}
|
|
|
|
// the setup function runs once when you press reset or power the board
|
|
void setup() {
|
|
// initialize digital pin LED_BUILTIN as an output.
|
|
Serial.begin(9600);
|
|
for(int i=0; i<6; i++)
|
|
{
|
|
pinMode(pA[i], OUTPUT);
|
|
pinMode(pR[i], OUTPUT);
|
|
digitalWrite(pA[i], LOW);
|
|
digitalWrite(pR[i], LOW);
|
|
}
|
|
pinMode(mode, INPUT);
|
|
pinMode(iA, INPUT);
|
|
pinMode(iR, INPUT);
|
|
digitalWrite(iR, HIGH);
|
|
digitalWrite(iA, HIGH);
|
|
digitalWrite(mode, HIGH);
|
|
Timer1.initialize(10000);
|
|
Timer1.attachInterrupt(stop_diapos); // blinkLED to run every 0.15 seconds
|
|
noInterrupts();
|
|
time_pausa = 600 + average(A4)*FACTOR;
|
|
interrupts();
|
|
}
|
|
|
|
// the loop function runs over and over again forever
|
|
void loop() {
|
|
if(!digitalRead(mode))
|
|
{
|
|
noInterrupts();
|
|
manual_mode();
|
|
}
|
|
else
|
|
{
|
|
interrupts();
|
|
auto_mode();
|
|
}
|
|
}
|
|
|
|
|
|
void avanza(uint8_t pin)
|
|
{
|
|
digitalWrite(pR[pin], LOW);
|
|
digitalWrite(pA[pin], HIGH);
|
|
}
|
|
|
|
void retrocede(uint8_t pin)
|
|
{
|
|
digitalWrite(pA[pin], LOW);
|
|
digitalWrite(pR[pin], HIGH);
|
|
}
|
|
|
|
void avanza(uint8_t pin, uint32_t time)
|
|
{
|
|
if(time>0)
|
|
{
|
|
noInterrupts();
|
|
time_av_prot[pin] = time;
|
|
time_av[pin] = millis();
|
|
digitalWrite(pR[pin], LOW);
|
|
digitalWrite(pA[pin], HIGH);
|
|
interrupts();
|
|
}
|
|
}
|
|
|
|
void retrocede(uint8_t pin, uint32_t time)
|
|
{
|
|
if(time>0)
|
|
{
|
|
noInterrupts();
|
|
time_rt_prot[pin] = time;
|
|
time_rt[pin] = millis();
|
|
digitalWrite(pA[pin], LOW);
|
|
digitalWrite(pR[pin], HIGH);
|
|
interrupts();
|
|
}
|
|
}
|
|
|
|
void avanza(bool av0, bool av1, bool av2, bool av3, bool av4, bool av5, unsigned long int time)
|
|
{
|
|
bool av[6] = {av0, av1, av2, av3, av4, av5};
|
|
for(int i=0; i<6; i++)
|
|
{
|
|
if (av[i]>0) avanza(i, 500);
|
|
else if(av[i]<0) retrocede(i, 500);
|
|
}
|
|
delay(time);
|
|
noInterrupts();
|
|
time_pausa = 600 + average(A4)*FACTOR;
|
|
interrupts();
|
|
}
|
|
|
|
void stop(int8_t pin)
|
|
{
|
|
digitalWrite(pA[pin], LOW);
|
|
digitalWrite(pR[pin], LOW);
|
|
}
|
|
|
|
void stop_diapos(void)
|
|
{
|
|
|
|
for(int i=0; i<6; i++)
|
|
{
|
|
if ((millis()- time_av[i]) < time_av_prot[i])
|
|
break;
|
|
else if ((millis()- time_rt[i]) < time_rt_prot[i])
|
|
break;
|
|
else stop(i);
|
|
}
|
|
}
|
|
|
|
#define N 100
|
|
uint32_t average(int PIN)
|
|
{
|
|
uint32_t value = 0;
|
|
for(int i=0; i<N; i++)
|
|
{
|
|
value = analogRead(A4) + value;
|
|
}
|
|
return value/N;
|
|
}
|