// -----
// SimplePollRotator.ino - Example for the RotaryEncoder library.
// This class is implemented for use with the Arduino environment.
// Copyright (c) by Matthias Hertel, http://www.mathertel.de
// This work is licensed under a BSD style license. See http://www.mathertel.de/License.aspx
// More information on: http://www.mathertel.de/Arduino
// -----
// 18.01.2014 created by Matthias Hertel
// -----
#include <SPI.h>
// This example checks the state of the rotary encoder in the loop() function.
// The current position is printed on output when changed.
// Hardware setup:
// Attach a rotary encoder with output pins to A2 and A3.
// The common contact should be attached to ground.
#include <RotaryEncoder.h>
// Setup a RoraryEncoder for pins A2 and A3:
RotaryEncoder encoder(10, 11);
int input = 1;
int newInput = 1;
const int selectButton = 13;
int lastButtonState = 1;
int latchPin = 3;
int drain;
void setup()
{
Serial.begin(57600);
Serial.println("SimplePollRotator example for the RotaryEncoder library.");
pinMode(selectButton, INPUT);
digitalWrite(selectButton, HIGH);
pinMode(latchPin, OUTPUT); //set latch pin to output
SPI.begin();
} // setup()
// Read the current position of the encoder and print out when changed.
void loop()
{
static int pos = 0; //static
encoder.tick();
int newPos = encoder.getPosition();
if (pos != newPos) {
pos = newPos;
if (pos >= 0){
newInput = (pos % 8)+1;
}else{
newInput = ((pos+1) % 8)+8;
}
//pos = newPos;
} // if
if (input != newInput && (digitalRead(selectButton)== 0)) {
input = newInput;
changeInput();
Serial.print(newPos);
Serial.println();
}
} // loop ()
// The End
void changeInput()
{
switch(input){
case 1: //input0 on PCB
drain = 8;
case 2: //input1 on PCB
drain = 4;
break;
case 3: //input2 on PCB
drain = 2;
break;
case 4: //input3 on PCB
drain = 1;
break;
case 5: //input4 on PCB
drain = 16;
break;
case 6: //input5 on PCB
drain = 32;
break;
}
digitalWrite(latchPin, LOW);
delay(100);
SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));
SPI.transfer(drain);
digitalWrite(latchPin, HIGH);
SPI.endTransaction();
delay(250);
}