#include <SPI.h>
const int STCPPinL = 4; // пин, управляющий выходной защёлкой
const int STCPPinR = 3;
int speed = 2000;
int count = 0;
int lastSpeed = LOW;
int curSpeed = LOW;
void setup()
{
SPI.begin();
pinMode(STCPPinL, OUTPUT);
digitalWrite(STCPPinL, LOW);
pinMode(STCPPinR, OUTPUT);
digitalWrite(STCPPinR, LOW);
SPI.transfer(0); //сброс всех выходов в ноль
digitalWrite(STCPPinL, HIGH); //формирование фронта на выходную защелку
digitalWrite(STCPPinL, LOW);
digitalWrite(STCPPinR, HIGH); //формирование фронта на выходную защелку
digitalWrite(STCPPinR, LOW);
pinMode(2, INPUT_PULLUP);
attachInterrupt(0, Speed, FALLING);
}
// Кольцевой сдвиг
void rotateLeft(uint8_t &bits)
{
uint8_t high_bit = bits & (1 << 7) ? 1 : 0;
bits = (bits << 1) | high_bit;
}
void loop()
{
if(count < 8){
static uint8_t nomad = 1; //активен только один светодиод
SPI.transfer(nomad);
digitalWrite(STCPPinR, HIGH); //формирование фронта на выходную защелку
digitalWrite(STCPPinR, LOW);
rotateLeft(nomad); //сдвиг бита на один разрд
count++;
delay(speed);
} else if(count >= 8 && count < 16){
SPI.transfer(0);
digitalWrite(STCPPinR, HIGH);
static uint8_t nomad = 1; //активен только один светодиод
SPI.transfer(nomad);
digitalWrite(STCPPinL, HIGH); //формирование фронта на выходную защелку
digitalWrite(STCPPinL, LOW);
rotateLeft(nomad); //сдвиг бита на один разрд
count++;
delay(speed);
} else{
count = 0;
SPI.transfer(0);
digitalWrite(STCPPinL, HIGH);
}
}
void Speed(){
curSpeed = debounce(lastSpeed);
if(lastSpeed == HIGH && curSpeed == LOW){
if(speed > 200) {
speed-=200;
} else {speed = 2000;}
}
lastSpeed = curSpeed;
}
int debounce(int last){
int current = digitalRead(2);
if(last != current){
delay(5);
current = digitalRead(2);
}
return current;
}