Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //arduino nixie tube controller
- //port B register has 8 pins,
- //port B register ranging from bit 7 to bit 0
- //PORT B:
- //7 6 5 4 3 2 1 0
- //PB4 to PB7 control the active tube, 7 being the one most left, 4 being the outer right tube
- //PB0 to PB3 control the displayed number
- int version = 1;
- int lastNumber;
- int NumArr[4] = {1, 3, 3, 7};
- int rotations = 0;
- bool on = false;
- int numCount = 0;
- int numCountMax = 9000;
- int numCountOp = 0;
- int cycleCount = 0;
- int nA0 = 0, nA1 = 0, nA2 = 0, nA3 = 0;
- int rndFnc = 0;
- int led = 13;
- //----ports for numbers
- int pb0 = 40;
- int pb1 = 41;
- int pb2 = 42;
- int pb3 = 43;
- // ----------
- // ports for the selected tube
- int pb4 = 44;
- int pb5 = 45;
- int pb6 = 46;
- int pb7 = 47;
- //----------
- void setup() {
- Serial.begin(9600);
- attachInterrupt(4, rpmcount, FALLING);
- pinMode(led, OUTPUT);
- pinMode(pb0, OUTPUT);
- pinMode(pb1, OUTPUT);
- pinMode(pb2, OUTPUT);
- pinMode(pb3, OUTPUT);
- pinMode(pb4, OUTPUT);
- pinMode(pb5, OUTPUT);
- pinMode(pb6, OUTPUT);
- pinMode(pb7, OUTPUT);
- //nr 0
- digitalWrite(pb0, LOW); // turn the LED on (HIGH is the voltage level)
- digitalWrite(pb1, HIGH); // turn the LED on (HIGH is the voltage level)
- digitalWrite(pb2, LOW); // turn the LED on (HIGH is the voltage level)
- digitalWrite(pb3, HIGH); // turn the LED on (HIGH is the voltage level)
- // all tubes
- digitalWrite(pb4, HIGH); // turn the LED on (HIGH is the voltage level)
- digitalWrite(pb5, HIGH); // turn the LED on (HIGH is the voltage level)
- digitalWrite(pb6, HIGH); // turn the LED on (HIGH is the voltage level)
- digitalWrite(pb7, HIGH); // turn the LED on (HIGH is the voltage level)
- delay(1000);
- }
- bool x = true;
- void blinkLed()
- {
- if (x == true)
- {
- digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
- x = false;
- }
- else
- {
- digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
- x = true;
- }
- }
- void processNumbers(int value)
- {
- NumArr[0] = value / 1000;
- NumArr[1] = (value % 1000)/100;
- NumArr[2] = ((value % 1000)%100)/10;
- NumArr[3] = ((value % 1000)%100)%10;
- }
- void rpmcount() //interrupt service routine ISR
- {
- rotations += 1;
- }
- void readSpeed()
- {
- if (rotations > 1)
- {
- on = true;
- }
- else
- {
- on = false;
- }
- processNumbers(map(rotations, 31, 82, 870, 2300));
- //Serial.println(rotations);
- rotations = 0;
- blinkLed();
- }
- void cycleNumber(int funct)
- {
- //funct 1 cycle initial values
- //funct 2 display random unmbers from 0 to 9999
- //funct 0 count from 0 to max and back
- //funct 3 shift initial val
- if (funct == 2)
- {
- int rnd = random(0,9999);
- processNumbers(rnd);
- }
- if (funct == 1)
- {
- int nArr1 = NumArr[0];
- NumArr[0] = NumArr[1];
- NumArr[1] = NumArr[2];
- NumArr[2] = NumArr[3];
- NumArr[3] = nArr1;
- }
- if (funct == 3)
- {
- if (!cycleCount)
- {
- nA0 = NumArr[0];
- nA1 = NumArr[1];
- nA2 = NumArr[2];
- nA3 = NumArr[3];
- }
- switch (cycleCount)
- {
- case 0:
- NumArr[3] = 0;
- NumArr[2] = nA3;
- NumArr[1] = nA2;
- NumArr[0] = nA1;
- cycleCount++;
- break;
- case 1:
- NumArr[2] = 0;
- NumArr[1] = nA3;
- NumArr[0] = nA2;
- cycleCount++;
- break;
- case 2:
- NumArr[1] = 0;
- NumArr[0] = nA3;
- cycleCount++;
- break;
- case 3:
- NumArr[0] = 0;
- cycleCount++;
- break;
- case 4:
- NumArr[1] = 0;
- NumArr[0] = nA3;
- cycleCount++;
- break;
- case 5:
- NumArr[2] = 0;
- NumArr[1] = nA3;
- NumArr[0] = nA2;
- cycleCount++;
- break;
- case 6:
- NumArr[3] = 0;
- NumArr[2] = nA3;
- NumArr[1] = nA2;
- NumArr[0] = nA1;
- cycleCount++;
- break;
- case 7:
- NumArr[3] = nA3;
- NumArr[2] = nA2;
- NumArr[1] = nA1;
- NumArr[0] = nA0;
- cycleCount = 0;
- break;
- }
- }
- if (funct == 0)
- {
- if (numCountOp)
- {
- numCount--;
- }
- else
- {
- numCount++;
- }
- if(numCount > numCountMax)
- {
- numCountOp = 1;
- }
- if(numCount <= 0)
- {
- numCountOp = 0;
- }
- processNumbers(numCount);
- }
- }
- int counter = 0;
- void setNumber(int tubeArr[4])
- {
- for (int i = 0; i < 4; i++)
- {
- delay(1);
- counter++;
- if (counter == 1000) //if one second has passed
- {
- readSpeed();
- //rndFnc = random(0,3);
- counter = 0;
- }
- if (!(counter%100))
- {
- //cycleNumber(rndFnc);
- // blinkLed();
- }
- if (on)
- {
- switch (i+1){
- case 1: //all the way on the left
- digitalWrite(pb4, LOW);
- digitalWrite(pb5, LOW);
- digitalWrite(pb6, LOW);
- digitalWrite(pb7, HIGH);
- break;
- case 2: //2nd from left
- digitalWrite(pb4, LOW);
- digitalWrite(pb5, LOW);
- digitalWrite(pb6, HIGH);
- digitalWrite(pb7, LOW);
- break;
- case 3:
- //3rd from left
- digitalWrite(pb4, LOW);
- digitalWrite(pb5, HIGH);
- digitalWrite(pb6, LOW);
- digitalWrite(pb7, LOW);
- break;
- case 4:
- // 4th from left
- digitalWrite(pb4, HIGH);
- digitalWrite(pb5, LOW);
- digitalWrite(pb6, LOW);
- digitalWrite(pb7, LOW);
- break;
- }
- switch (NumArr[i])
- {
- case 0:
- digitalWrite(pb0, LOW);
- digitalWrite(pb1, HIGH);
- digitalWrite(pb2, LOW);
- digitalWrite(pb3, HIGH);
- break;
- case 1:
- digitalWrite(pb0, HIGH);
- digitalWrite(pb1, LOW);
- digitalWrite(pb2, LOW);
- digitalWrite(pb3, LOW);
- break;
- case 2:
- digitalWrite(pb0, HIGH);
- digitalWrite(pb1, HIGH);
- digitalWrite(pb2, LOW);
- digitalWrite(pb3, LOW);
- break;
- case 3:
- digitalWrite(pb0, HIGH);
- digitalWrite(pb1, LOW);
- digitalWrite(pb2, LOW);
- digitalWrite(pb3, HIGH);
- break;
- case 4:
- digitalWrite(pb0, LOW);
- digitalWrite(pb1, HIGH);
- digitalWrite(pb2, LOW);
- digitalWrite(pb3, LOW);
- break;
- case 5:
- digitalWrite(pb0, LOW);
- digitalWrite(pb1, LOW);
- digitalWrite(pb2, LOW);
- digitalWrite(pb3, HIGH);
- break;
- case 6:
- digitalWrite(pb0, LOW);
- digitalWrite(pb1, LOW);
- digitalWrite(pb2, HIGH);
- digitalWrite(pb3, HIGH);
- break;
- case 7:
- digitalWrite(pb0, LOW);
- digitalWrite(pb1, LOW);
- digitalWrite(pb2, LOW);
- digitalWrite(pb3, LOW);
- break;
- case 8:
- digitalWrite(pb0, LOW);
- digitalWrite(pb1, LOW);
- digitalWrite(pb2, HIGH);
- digitalWrite(pb3, LOW);
- break;
- case 9:
- digitalWrite(pb0, HIGH);
- digitalWrite(pb1, HIGH);
- digitalWrite(pb2, LOW);
- digitalWrite(pb3, HIGH);
- break;
- }
- }
- else
- {
- digitalWrite(pb4, LOW);
- digitalWrite(pb5, LOW);
- digitalWrite(pb6, LOW);
- digitalWrite(pb7, LOW);
- }
- }
- }
- void loop()
- {
- setNumber(NumArr);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement