Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int bottomLeft = 3;
- const int bottom = 6;
- const int bottomRight = 2;
- const int topRight = 7;
- const int top = 1;
- const int topLeft = 4;
- const int middle = 5;
- void setup() {
- pinMode(bottomLeft, OUTPUT);
- pinMode(bottom, OUTPUT);
- pinMode(bottomRight, OUTPUT);
- pinMode(topRight, OUTPUT);
- pinMode(top, OUTPUT);
- pinMode(topLeft, OUTPUT);
- pinMode(middle, OUTPUT);
- pinMode(A0, INPUT);
- //debug
- //Serial.begin(9600);
- }
- int incomingByte = 0;
- int prevByte = 2;
- int currentByte = 0;
- int testing = 1;
- void loop() {
- if (testing == 1){
- test();
- //digitalWrite(6,HIGH); // test individual segments
- }else{
- int raw = 0;
- float val = 0;
- float avgVal = 0;
- for (int i=0; i <= 99; i++){
- raw = analogRead(A0); // read 0-5 volts from the GPS
- val = fmap(raw, 0, 1023, 0.0, 5.0); // volts are 10 bit (0-1023) so map it back to a proper volt number
- avgVal = avgVal+val;
- }
- avgVal = avgVal/100;
- //Serial.println(avgVal); //voltage
- //1st: 1.36V
- //2nd: 1.77V
- //3rd: 2.49V
- //4th: 3.23V
- //5th: 4.1V
- //6th: 4.55V
- //N: 5V
- if (avgVal <= 1.70){
- reset();
- first();
- }
- if (avgVal >= 1.71 && avgVal <= 2.50){
- second();
- }
- if (avgVal >= 2.51 && avgVal <= 3.1){
- third();
- }
- if (avgVal >= 3.11 && avgVal <= 4.00){
- fourth();
- }
- if (avgVal >= 4.01 && avgVal <= 4.40){
- fifth();
- }
- if (avgVal >= 4.41 && avgVal <= 4.80){
- sixth();
- }
- if (avgVal >= 4.81){
- neutral();
- }
- delay(1);
- }
- }
- void reset(){
- digitalWrite(bottomRight,HIGH);
- digitalWrite(bottom,HIGH);
- digitalWrite(bottomLeft,HIGH);
- digitalWrite(topRight,HIGH);
- digitalWrite(top,HIGH);
- digitalWrite(topLeft,HIGH);
- digitalWrite(middle,HIGH);
- }
- void first(){
- reset();
- digitalWrite(topRight,LOW);
- digitalWrite(bottomRight,LOW);
- }
- void second(){
- reset();
- digitalWrite(top,LOW);
- digitalWrite(topRight,LOW);
- digitalWrite(middle,LOW);
- digitalWrite(bottomLeft,LOW);
- digitalWrite(bottom,LOW);
- }
- void third(){
- reset();
- digitalWrite(top,LOW);
- digitalWrite(topRight,LOW);
- digitalWrite(middle,LOW);
- digitalWrite(bottomRight,LOW);
- digitalWrite(bottom,LOW);
- }
- void fourth(){
- reset();
- digitalWrite(topLeft,LOW);
- digitalWrite(middle,LOW);
- digitalWrite(topRight ,LOW);
- digitalWrite(bottomRight,LOW);
- }
- void fifth(){
- reset();
- digitalWrite(top,LOW);
- digitalWrite(topLeft,LOW);
- digitalWrite(middle,LOW);
- digitalWrite(bottomRight,LOW);
- digitalWrite(bottom,LOW);
- }
- void sixth(){
- reset();
- digitalWrite(top,LOW);
- digitalWrite(topLeft,LOW);
- digitalWrite(bottomLeft,LOW);
- digitalWrite(bottom,LOW);
- digitalWrite(bottomRight,LOW);
- digitalWrite(middle,LOW);
- }
- //animated neutral "spins"
- void neutral(){
- digitalWrite(bottomLeft,LOW);
- delay(100);
- reset();
- digitalWrite(bottom,LOW);
- delay(100);
- reset();
- digitalWrite(bottomRight,LOW);
- delay(100);
- reset();
- digitalWrite(topRight,LOW);
- delay(100);
- reset();
- digitalWrite(top,LOW);
- delay(100);
- reset();
- digitalWrite(topLeft,LOW);
- delay(100);
- reset();
- }
- void test(){
- first();
- delay(1000);
- second();
- delay(1000);
- third();
- delay(1000);
- fourth();
- delay(1000);
- fifth();
- delay(1000);
- sixth();
- delay(1000);
- }
- //Mapping function, used to convert 0-1023 to 0-5 volts
- float fmap(float x, float in_min, float in_max, float out_min, float out_max)
- {
- return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement