Control 4 digits 7-segment LED interfacing MCU 8051 - microcontroller

I have some error while reading the voltage displayed on 7-segment LED. The PCB was designed by another person. They didn't assign the 7-segment LED pins to the same port on MCU.
Herein, I assume the MCU code is correct. But the LED displayed wrong voltage value. I have a question first. Can we connect 7-segment LED pins to 2 ports of MCU (microcontroller)? Such as segment a~d connect to MCU pins P1.0~P1.3, segment e~h connect to pins P2.0~P2.3. Or only connect to the same port, e.g. LED segment a~h connect to MCU pins P1.0~P1.7?
Here's the code of pin addressing and LED control:
// PORT 1
//--------------------------------------
sbit UP_BUTTON = P1^0;
sbit DOWN_BUTTON = P1^1;
sbit LEFT_BUTTON = P1^2;
sbit RIGHT_BUTTON = P1^3;
sbit ENTER_BUTTON = P1^4;
sbit _7SEG_COM5 = P1^5;
sbit _7SEG_COM4 = P1^6;
sbit _7SEG_COM3 = P1^7;
//--------------------------------------
// PORT 2
//--------------------------------------
sbit _7SEG_F = P2^0;
sbit _7SEG_G = P2^1;
sbit _7SEG_H = P2^2;
sbit _7SEG_COM0 = P2^3;
sbit _7SEG_COM1 = P2^4;
sbit _7SEG_COM2 = P2^5;
sbit B_RST = P2^6; //DATA B RST
sbit B_RSTSEL = P2^7; //DATA B RSTSEL
//--------------------------------------
// PORT 3
//--------------------------------------
sbit THREE_ZERO = P3^0;
sbit THREE_ONE = P3^1;
sbit READY = P3^2;
sbit _7SEG_A = P3^3;
sbit _7SEG_B = P3^4;
sbit _7SEG_C = P3^5;
sbit _7SEG_D = P3^6;
sbit _7SEG_E = P3^7;
sbit Sinitial_f = Flag0^1;
unsigned char idata SEG[6] _at_ 0x82;
// 7 segment display in addition,3rd number(SEG2) is dot always
//=======================================================================================
void SEGMENT_DISPLAY(void)
{
unsigned char SFRPAGE_BUF,seg_num,seg_value;
int cnt, i, num, temp;
SFRPAGE_BUF = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
if(!Sinitial_f)
{
SEG[0] = 0x0A;
SEG[1] = 0x0A;
SEG[2] = 0x0A;
SEG[3] = 0x0A;
SEG[4] = 0x0A;
SEG[5] = 0x0A;
Sinitial_f = 1;
}
for(seg_num=0x00;seg_num<=0x04;seg_num++)
{
seg_value = SEG[seg_num];
switch(seg_value)
{
case 0x00: _7SEG_A = 1;//0
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 0;
_7SEG_H = 0;
break;
case 0x01: _7SEG_A = 0;//1
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 0;
_7SEG_E = 0;
_7SEG_F = 0;
_7SEG_G = 0;
_7SEG_H = 0;
break;
case 0x02: _7SEG_A = 1;//2
_7SEG_B = 1;
_7SEG_C = 0;
_7SEG_D = 1;
_7SEG_E = 1;
_7SEG_F = 0;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x03: _7SEG_A = 1;//3
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 0;
_7SEG_F = 0;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x04: _7SEG_A = 0;//4
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 0;
_7SEG_E = 0;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x05: _7SEG_A = 1;//5
_7SEG_B = 0;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 0;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x06: _7SEG_A = 1;//6
_7SEG_B = 0;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x07: _7SEG_A = 1;//7
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 0;
_7SEG_E = 0;
_7SEG_F = 1;
_7SEG_G = 0;
_7SEG_H = 0;
break;
case 0x08: _7SEG_A = 1;//8
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x09: _7SEG_A = 1;//9
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 0;
_7SEG_E = 0;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x0A: _7SEG_A = 0;//.
_7SEG_B = 0;
_7SEG_C = 0;
_7SEG_D = 0;
_7SEG_E = 0;
_7SEG_F = 0;
_7SEG_G = 0;
_7SEG_H = 1;
break;
case 0x0B: _7SEG_A = 0;//-
_7SEG_B = 0;
_7SEG_C = 0;
_7SEG_D = 0;
_7SEG_E = 0;
_7SEG_F = 0;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x0C: _7SEG_A = 0;//Close
_7SEG_B = 0;
_7SEG_C = 0;
_7SEG_D = 0;
_7SEG_E = 0;
_7SEG_F = 0;
_7SEG_G = 0;
_7SEG_H = 0;
break;
case 0x0D: _7SEG_A = 1;//E
_7SEG_B = 0;
_7SEG_C = 0;
_7SEG_D = 1;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x0E: _7SEG_A = 1;//C
_7SEG_B = 0;
_7SEG_C = 0;
_7SEG_D = 1;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 0;
_7SEG_H = 0;
break;
case 0x0F: _7SEG_A = 1;//A
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 0;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x10: _7SEG_A = 0;//r
_7SEG_B = 0;
_7SEG_C = 0;
_7SEG_D = 0;
_7SEG_E = 1;
_7SEG_F = 0;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x11: _7SEG_A = 1;//S
_7SEG_B = 0;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 0;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x12: _7SEG_A = 1;//P
_7SEG_B = 1;
_7SEG_C = 0;
_7SEG_D = 0;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x13: _7SEG_A = 1;//C.
_7SEG_B = 0;
_7SEG_C = 0;
_7SEG_D = 1;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 0;
_7SEG_H = 1;
break;
case 0x14: _7SEG_A = 0;//d
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 1;
_7SEG_F = 0;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x15: _7SEG_A = 0;//n
_7SEG_B = 0;
_7SEG_C = 1;
_7SEG_D = 0;
_7SEG_E = 1;
_7SEG_F = 0;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x16: _7SEG_A = 0;//V
_7SEG_B = 1;
_7SEG_C = 0;
_7SEG_D = 0;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x17: _7SEG_A = 0;//h
_7SEG_B = 0;
_7SEG_C = 1;
_7SEG_D = 0;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x18: _7SEG_A = 0;//L
_7SEG_B = 0;
_7SEG_C = 0;
_7SEG_D = 1;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 0;
_7SEG_H = 0;
break;
case 0x19: _7SEG_A = 0;//U
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 0;
_7SEG_H = 0;
break;
case 0x1A: _7SEG_A = 0;//b
_7SEG_B = 0;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x1B: _7SEG_A = 1;//g
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 0;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 0;
break;
case 0x1C: _7SEG_A = 0;//V.
_7SEG_B = 1;
_7SEG_C = 0;
_7SEG_D = 0;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 1;
break;
case 0x1D: _7SEG_A = 0;//r.
_7SEG_B = 0;
_7SEG_C = 0;
_7SEG_D = 0;
_7SEG_E = 1;
_7SEG_F = 0;
_7SEG_G = 1;
_7SEG_H = 1;
break;
case 0x1E: _7SEG_A = 1;//g.
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 0;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 1;
break;
case 0x1F: _7SEG_A = 0;//b.
_7SEG_B = 0;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 1;
break;
//number + dot
case 0x20: _7SEG_A = 1;//0.
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 0;
_7SEG_H = 1;
break;
case 0x21: _7SEG_A = 0;//1.
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 0;
_7SEG_E = 0;
_7SEG_F = 0;
_7SEG_G = 0;
_7SEG_H = 1;
break;
case 0x22: _7SEG_A = 1;//2.
_7SEG_B = 1;
_7SEG_C = 0;
_7SEG_D = 1;
_7SEG_E = 1;
_7SEG_F = 0;
_7SEG_G = 1;
_7SEG_H = 1;
break;
case 0x23: _7SEG_A = 1;//3.
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 0;
_7SEG_F = 0;
_7SEG_G = 1;
_7SEG_H = 1;
break;
case 0x24: _7SEG_A = 0;//4.
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 0;
_7SEG_E = 0;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 1;
break;
case 0x25: _7SEG_A = 1;//5.
_7SEG_B = 0;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 0;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 1;
break;
case 0x26: _7SEG_A = 1;//6.
_7SEG_B = 0;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 1;
break;
case 0x27: _7SEG_A = 1;//7.
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 0;
_7SEG_E = 0;
_7SEG_F = 1;
_7SEG_G = 0;
_7SEG_H = 1;
break;
case 0x28: _7SEG_A = 1;//8.
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 1;
_7SEG_E = 1;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 1;
break;
case 0x29: _7SEG_A = 1;//9.
_7SEG_B = 1;
_7SEG_C = 1;
_7SEG_D = 0;
_7SEG_E = 0;
_7SEG_F = 1;
_7SEG_G = 1;
_7SEG_H = 1;
break;
default: break;
}
switch(seg_num)
{
case 0x00: _7SEG_COM5 = 0;
break;
case 0x01: _7SEG_COM4 = 0;
break;
case 0x02:
_7SEG_COM3 = 0;
break;
case 0x03: if(engineering_f)
_7SEG_H = 1;
_7SEG_COM2 = 0;
break;
case 0x04: _7SEG_COM1 = 0;
break;
default: break;
}
DELAY_X2us(100);
_7SEG_COM0 = 1;
_7SEG_COM1 = 1;
_7SEG_COM2 = 1;
_7SEG_COM3 = 1;
_7SEG_COM4 = 1;
_7SEG_COM5 = 1;
}
SFRPAGE = SFRPAGE_BUF;
}
void SEGMENT_REG_REFRESH(void)
{
//for display voltage (V)
unsigned long voltage;
unsigned char voltage_ten_thousand;
unsigned char voltage_thousand;
unsigned char voltage_hundred;
unsigned char voltage_ten;
unsigned char voltage_unit;
unsigned long DAC_buf =0x0000;
//for display pattern index
unsigned char hundred;
unsigned char ten;
unsigned char unit;
if(Keyboard ==1)
{
switch(Keyboard_Item_Index)
{
case 0: //VGH
voltage = VGH_VGL_A*0.05+ 3.2;
voltage_ten = voltage/10 ;
voltage = voltage%10;
SEG[5] = 0x16;
SEG[4] = 0x1B;
SEG[3] = 0x17;
if(voltage_ten!=0x00)
{
SEG[2] = voltage_ten;
SEG[1] = voltage_unit + 0x20; //add dot
SEG[0] = voltage_hundred;
}
else
{
SEG[2] = voltage_thousand + 0x20; //add dot
SEG[1] = voltage_hundred;
SEG[0] = voltage_ten;
}
break;
case 1: //VGL
voltage = VGH_VGL_B*0.05+ 1.2;
voltage_ten = voltage/10 ;
voltage = voltage%10;
SEG[5] = 0x16;
SEG[4] = 0x1B;
SEG[3] = 0x18;
if(voltage_ten!=0x00)
{
SEG[2] = voltage_ten;
SEG[1] = voltage_unit + 0x20; //add dot
SEG[0] = voltage_hundred;
}
else
{
SEG[2] = voltage_thousand + 0x20; //add dot
SEG[1] = voltage_hundred;
SEG[0] = voltage_ten;
}
break;
// Button Control to switch voltage for adjustment. Keyboard_Item_Index //corresponds each voltage VGH/VGL...
//=======================================================================================
void Button_Check()
{
if(!UP_BUTTON)
{
Debounce_Cnt++;
if(Debounce_Cnt > 5)
{
Debounce_Cnt = 0;
Up_Button_f = 1;
Action_f = 0;
Up_Hold_Cnt++;
if(Up_Hold_Cnt > 100)
{
Up_Hold_Cnt = 0;
Up_Button_Hold_f = 1;
if(Keyboard == 1)
{
BUZZER();
}
else
{
BUZZER();
DELAY_X2us(50000);
BUZZER();
DELAY_X2us(50000);
}
}
}
}
else if(!DOWN_BUTTON)
{
Debounce_Cnt++;
if(Debounce_Cnt > 5)
{
Debounce_Cnt = 0;
Down_Button_f = 1;
Action_f = 0;
Down_Hold_Cnt++;
if(Down_Hold_Cnt > 100)
{
Down_Hold_Cnt = 0;
Down_Button_Hold_f = 1;
if(Keyboard==1)
{
BUZZER();
}
}
}
}
else if(!LEFT_BUTTON)
{
Debounce_Cnt++;
if(Debounce_Cnt > 5)
{
Debounce_Cnt = 0;
Left_Button_f = 1;
Action_f = 0;
Left_Hold_Cnt++;
if(Keyboard == 1)
{
}
else
{
BUZZER();
DELAY_X2us(50000);
BUZZER();
DELAY_X2us(50000);
}
if(Left_Hold_Cnt > 100)
{
Left_Hold_Cnt = 0;
Left_Button_Hold_f = 1;
//BUZZER();
}
}
}
else if(!RIGHT_BUTTON)
{
Debounce_Cnt++;
if(Debounce_Cnt > 5)
{
Debounce_Cnt = 0;
Right_Button_f = 1;
Action_f = 0;
Right_Hold_Cnt++;
if(Keyboard == 1)
{
}
else
{
POWER_SLEEP();
//BUZZER();
DELAY_X2us(50000);
//BUZZER();
DELAY_X2us(50000);
}
if(Right_Hold_Cnt > 100)
{
Right_Hold_Cnt = 0;
Right_Button_Hold_f = 1;
//BUZZER();
}
}
}
else if(!ENTER_BUTTON)
{
Debounce_Cnt++;
if(Debounce_Cnt > 5)
{
Debounce_Cnt = 0;
Enter_Button_f = 1;
Action_f = 0;
Enter_Hold_Cnt++;
if(Enter_Hold_Cnt > 100)
{
Enter_Hold_Cnt = 0;
Enter_Button_Hold_f = 1;
BUZZER();
DELAY_X2us(50000);
BUZZER();
DELAY_X2us(50000);
}
}
}
else
{
Action_f = 1;
Debounce_Cnt = 0;
Up_Hold_Cnt = 0;
Down_Hold_Cnt = 0;
Left_Hold_Cnt = 0;
Right_Hold_Cnt = 0;
Enter_Hold_Cnt = 0;
}
}
void Button_Function()
{
//int i;
if(Up_Button_Hold_f)
{
Up_Button_f = 0;
Up_Button_Hold_f = 0;
if(Keyboard ==1)
{
Keyboard_Adj(Keyboard_Item_Index,3);
}
else
{
returnPatternZero();
}
//Keyboard = !Keyboard;
}
else if(Down_Button_Hold_f)
{
Down_Button_f = 0;
Down_Button_Hold_f = 0;
BUZZER();
check_ID();
// DELAY_X2us(5000);
// if(Keyboard ==1)
// {
//
// Keyboard_Adj(Keyboard_Item_Index,2);
//
// }
}
else if(Left_Button_Hold_f)
{
Left_Button_f = 0;
Left_Button_Hold_f = 0;
}
else if(Right_Button_Hold_f)
{
Right_Button_f = 0;
Right_Button_Hold_f = 0;
//DC Power wake up
POWER_INITIAL();
DELAY_X2us(5000);
returnPatternZero();
}
else if(Enter_Button_Hold_f)
{
Enter_Button_f = 0;
Enter_Button_Hold_f = 0;
Keyboard = !Keyboard;
}
else if(Up_Button_f)
{
Up_Button_f = 0;
if(Keyboard ==1)
{
Keyboard_Adj(Keyboard_Item_Index,1);
}
else
{
if(Pattern_Index<Total_Pattern-1)
{
Pattern_Index++;
}
else
{
Pattern_Index=0;
}
quickPattern();
}
}
else if(Down_Button_f)
{
Down_Button_f = 0;
if(Keyboard ==1)
{
Keyboard_Adj(Keyboard_Item_Index,0);
}
else
{
if(Pattern_Index>0)
{
Pattern_Index--;
}
else
{
Pattern_Index=Total_Pattern-1;
}
quickPattern();
}
}
else if(Left_Button_f)
{
Left_Button_f = 0;
if(Keyboard ==1)
{
if(Keyboard_Item_Index >0)
{
Keyboard_Item_Index-=1 ;
}
else
{
Keyboard_Item_Index = Total_Keyboard_Item_Index-1;
}
}
else
{
returnPatternZero();
}
}
else if(Right_Button_f)
{
Right_Button_f = 0;
if(Keyboard ==1)
{
if(Keyboard_Item_Index < Total_Keyboard_Item_Index-1)
{
Keyboard_Item_Index +=1 ;
}
else
{
Keyboard_Item_Index = 0;
}
}
else
{
//DC Power wake up
POWER_INITIAL();
DELAY_X2us(5000);
returnPatternZero();
}
}
else if(Enter_Button_f)
{
Enter_Button_f = 0;
}
}
void main(void)
{
while(1)
{
Button_Check();
if(Action_f)
{
Button_Function();
}
SEGMENT_REG_REFRESH();
SEGMENT_DISPLAY();
}
}

In you definition voltage is an int value:
unsigned long voltage;
You can't do things like this:
voltage = VGH_VGL_B*0.05+ 1.2;
with an integer.
I guess you should avoid float at all.

Related

Why is my serial monitor showing the binary value updating every other cycle where as the decimal is every cycle? - Arduino

Project Context:
This is code I wrote for my arduino to try and create a binary clock, where I create a 24 length array of boleans to define a binary clock.
the array is broken down like this:
00|0000|000|0000|000|0000
H1| H2 | M1| M2 | S1| S2
I intent to use 3 8 Bit shift registers to control the output LEDs, so this could be broken up 1 nibble per digit however I did not do that......not sure why.
TLDR:
I made this shit code, it is currently only incrememnting the binary value that's printed every other cycle and the decimal every cycle, would anyone be able to help me figure out why?
`
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
int h1 = 0;
int h2 = 0;
int m1 = 0;
int m2 = 0;
int s1 = 0;
int s2 = -1;
while(0 == 0)
{
// put your main code here, to run repeatedly:
s2 = s2 + 1;
if (s2 == 10)
{
s2 = 0;
s1 = s1 + 1;
if ( s1 == 6)
{
s1 = 0;
m2 = m2 + 1;
if ( m2 == 10)
{
m2 = 0;
m1 = m1 + 1;
if (m1 == 6)
{
m1 = 0;
h2 = h2 + 1;
if ( h2 == 10 )
{
h2 = 0;
h1 = h1 + 1;
}
else if ((h2 == 5) && (h1 == 2))
{
h1 = 0;
h2 = 0;
}
}
}
}
}
stringSetter(h1,h2,m1,m2,s1,s2);
delay(1000);
}
}
void stringSetter(int h1, int h2, int m1, int m2, int s1, int s2)
{
//h1 - converting to boolean array
boolean h1b[2];
if ( h1 == 0 )
{
h1b[0] = 0;
h1b[1] = 0;
}
else if ( h1 == 1 )
{
h1b[0] = 1;
h1b[1] = 0;
}
else if ( h1 == 2 )
{
h1b[0] = 0;
h1b[1] = 1;
}
//h2 - converting to boolean array
boolean h2b[4];
if ( h2 == 0 )
{
h2b[0] = 0;
h2b[1] = 0;
h2b[2] = 0;
h2b[3] = 0;
}
else if ( h2 == 1 )
{
h2b[0] = 1;
h2b[1] = 0;
h2b[2] = 0;
h2b[3] = 0;
}
else if ( h2 == 2)
{
h2b[0] = 0;
h2b[1] = 1;
h2b[2] = 0;
h2b[3] = 0;
}
else if ( h2 == 3 )
{
h2b[0] = 1;
h2b[1] = 1;
h2b[2] = 0;
h2b[3] = 0;
}
else if ( h2 == 4 )
{
h2b[0] = 0;
h2b[1] = 0;
h2b[2] = 1;
h2b[3] = 0;
}
else if ( h2 == 5 )
{
h2b[0] = 1;
h2b[1] = 0;
h2b[2] = 1;
h2b[3] = 0;
}
else if ( h2 == 6 )
{
h2b[0] = 0;
h2b[1] = 1;
h2b[2] = 1;
h2b[3] = 0;
}
else if ( h2 == 7 )
{
h2b[0] = 1;
h2b[1] = 1;
h2b[2] = 1;
h2b[3] = 0;
}
else if ( h2 == 8 )
{
h2b[0] = 0;
h2b[1] = 0;
h2b[2] = 0;
h2b[3] = 1;
}
else if ( h2 == 9 )
{
h2b[0] = 1;
h2b[1] = 0;
h2b[2] = 0;
h2b[3] = 1;
}
//m1 - converting to boolean array
boolean m1b[3];
if ( m1 == 0 )
{
m1b[0] = 0;
m1b[1] = 0;
m1b[2] = 0;
}
else if ( m1 == 1 )
{
m1b[0] = 1;
m1b[1] = 0;
m1b[2] = 0;
}
else if ( m1 == 2)
{
m1b[0] = 0;
m1b[1] = 1;
m1b[2] = 0;
}
else if ( m1 == 3 )
{
m1b[0] = 1;
m1b[1] = 1;
m1b[2] = 0;
}
else if ( m1 == 4 )
{
m1b[0] = 0;
m1b[1] = 0;
m1b[2] = 1;
}
else if ( m1 == 5 )
{
m1b[0] = 1;
m1b[1] = 0;
m1b[2] = 1;
}
//m2 - converting to boolean array
boolean m2b[4];
if ( m2 == 0 )
{
m2b[0] = 0;
m2b[1] = 0;
m2b[2] = 0;
m2b[3] = 0;
}
else if ( m2 == 1 )
{
m2b[0] = 1;
m2b[1] = 0;
m2b[2] = 0;
m2b[3] = 0;
}
else if ( m2 == 2)
{
m2b[0] = 0;
m2b[1] = 1;
m2b[2] = 0;
m2b[3] = 0;
}
else if ( m2 == 3 )
{
m2b[0] = 1;
m2b[1] = 1;
m2b[2] = 0;
m2b[3] = 0;
}
else if ( m2 == 4 )
{
m2b[0] = 0;
m2b[1] = 0;
m2b[2] = 1;
m2b[3] = 0;
}
else if ( m2 == 5 )
{
m2b[0] = 1;
m2b[1] = 0;
m2b[2] = 1;
m2b[3] = 0;
}
else if ( m2 == 6 )
{
m2b[0] = 0;
m2b[1] = 1;
m2b[2] = 1;
m2b[3] = 0;
}
else if ( m2 == 7 )
{
m2b[0] = 1;
m2b[1] = 1;
m2b[2] = 1;
m2b[3] = 0;
}
else if ( m2 == 8 )
{
m2b[0] = 0;
m2b[1] = 0;
m2b[2] = 0;
m2b[3] = 1;
}
else if ( m2 == 9 )
{
m2b[0] = 1;
m2b[1] = 0;
m2b[2] = 0;
m2b[3] = 1;
}
//s1 - converting to boolean array
boolean s1b[3];
if ( s1 == 0 )
{
s1b[0] = 0;
s1b[1] = 0;
s1b[2] = 0;
}
else if ( s1 == 1 )
{
s1b[0] = 1;
s1b[1] = 0;
s1b[2] = 0;
}
else if ( s1 == 2 )
{
s1b[0] = 0;
s1b[1] = 1;
s1b[2] = 0;
}
else if ( s1 == 3 )
{
s1b[0] = 1;
s1b[1] = 1;
s1b[2] = 0;
}
else if ( s1 == 4 )
{
s1b[0] = 0;
s1b[1] = 0;
s1b[2] = 1;
}
else if ( s1 == 5 )
{
s1b[0] = 1;
s1b[1] = 0;
s1b[2] = 1;
}
//s2 - converting to boolean array
boolean s2b[4];
if ( s2 == 0 )
{
s2b[0] = 0;
s2b[1] = 0;
s2b[2] = 0;
s2b[3] = 0;
}
else if ( s2 == 1 )
{
s2b[0] = 1;
s2b[1] = 0;
s2b[2] = 0;
s2b[3] = 0;
}
else if ( s2 == 2)
{
s2b[0] = 0;
s2b[1] = 1;
s2b[2] = 0;
s2b[3] = 0;
}
else if ( s2 == 3 )
{
s2b[0] = 1;
s2b[1] = 1;
s2b[2] = 0;
s2b[3] = 0;
}
else if ( s2 == 4 )
{
s2b[0] = 0;
s2b[1] = 0;
s2b[2] = 1;
s2b[3] = 0;
}
else if ( s2 == 5 )
{
s2b[0] = 1;
s2b[1] = 0;
s2b[2] = 1;
s2b[3] = 0;
}
else if ( s2 == 6 )
{
s2b[0] = 0;
s2b[1] = 1;
s2b[2] = 1;
s2b[3] = 0;
}
else if ( s2 == 7 )
{
s2b[0] = 1;
s2b[1] = 1;
s2b[2] = 1;
s2b[3] = 0;
}
else if ( s2 == 8 )
{
s2b[0] = 0;
s2b[1] = 0;
s2b[2] = 0;
s2b[3] = 1;
}
else if ( s2 == 9 )
{
s2b[0] = 1;
s2b[1] = 0;
s2b[2] = 0;
s2b[3] = 1;
}
Serial.print('H');
Serial.print(h1);
Serial.print(h2);
Serial.print('M');
Serial.print(m1);
Serial.print(m2);
Serial.print('S');
Serial.print(s1);
Serial.print(s2);
Serial.println();
// setting up pin booleans
boolean combiBi[24] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
combiBi[23] = 0;
combiBi[22] = 0;
combiBi[21] = 0;
combiBi[20] = 0;
combiBi[19] = h1b[1];
combiBi[18] = h1b[0];
combiBi[17] = h2b[3];
combiBi[16] = h2b[2];
combiBi[15] = h2b[1];
combiBi[14] = h2b[0];
combiBi[13] = m1b[2];
combiBi[12] = m1b[1];
combiBi[11] = m1b[0];
combiBi[10] = m2b[3];
combiBi[9] = m2b[2];
combiBi[8] = m2b[1];
combiBi[7] = m2b[0];
combiBi[6] = s1b[2];
combiBi[5] = s1b[1];
combiBi[4] = s1b[0];
combiBi[3] = s2b[3];
combiBi[2] = s2b[2];
combiBi[1] = s2b[1];
combiBi[0] = s2b[0];
// printing out binary for pins
for(int o = 23; o > 0; o--){
if ( o == 19 )
{
Serial.print('H');
}
else if (o == 13)
{
Serial.print('M');
}
else if (o == 6)
{
Serial.print('S');
}
Serial.print(combiBi[o]);
}
Serial.println();
}
`
Serial Monitor Output
I tried to get a decimal and binary equivilant output printed in the serial monitor, however the binary is only updated on every other cycle while the decimal performs as expect.
for(int o = 23; o > 0; o--)
In this loop o will never reach 0. Hence you'll never print your last bit which changes every second.
Change your run condition to o >= 0.
As already mentioned in my comments you should really learn more about digital numbers and their representation in memory. You don't have to explicitly implement every single bit combination by hand using arrays.
This is a computer, let it compute.

I'm trying to lip sync a 3d model using BabylonJS but not all lip positions are happening at the time their supposed to

I'm trying to lip sync a 3D model using babylonJS, morph targets, and audio obtained from Amazon Polly but the lip positions aren't happening at the exact time their supposed to (for example when the speech starts the lip positions start as well but not all the lip positions occur in sync with the sound or the speech ends before the lip positions end.) I tried creating a time counter variable called alpha and tried to increase the amount it's incremented by but there are times when it skips some of the lip position occurrences. The time (in milliseconds) that I want the lip positions to occur are shown in the variable jsonParsed. Does anyone know of a way to fix this. If you'd like to see my source code I've pasted it below.
var createScene = function () {
// This creates a basic Babylon Scene object (non-mesh)
var scene = new BABYLON.Scene(engine);
// This creates and positions a free camera (non-mesh)
var camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(0, 5, -10), scene);
// This creates a light, aiming 0,1,0 - to the sky (non-mesh)
var light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(0, 1, 0), scene);
// Default intensity is 1. Let's dim the light a small amount
light.intensity = 0.7;
url = 'https://raw.githubusercontent.com/chris45242/BabylonModel/main/';
fileName = "project.blend1.gltf";
const Casi = BABYLON.SceneLoader.Append(url, fileName, scene, function (s){
// Create a default arc rotate camera and light.
scene.createDefaultCameraOrLight(true, true, true);
scene.activeCamera.alpha += Math.PI;
scene.stopAllAnimations();
//scene.animationGroups[0].start(true);
scene.animationGroups[1].start(true);
var casiVisor = scene.getMeshByName("Casi's Visor");
casiVisor.setEnabled(true);
casiVisor.visibility = 0.7;
var casiBody = scene.getMeshByName("Casi's Body.001_primitive0");
var casiInnerMouth = scene.getMeshByName("Casi's Body.001_primitive2");
var casiTeeth = scene.getMeshByName("Casi's Teeth");
var primitive = scene.getMeshByName("Primitives.001");
var jsonParsed = //'[{"time":6,"type":"word","start":0,"end":4,"value":"it\'s"},'+
'[{"time":6,"type":"viseme","value":"i"},' +
'{"time":64,"type":"viseme","value":"t"},' +
'{"time":122,"type":"viseme","value":"s"},' +
//'{"time":190,"type":"word","start":5,"end":9,"value":"very"},' +
'{"time":190,"type":"viseme","value":"f"},' +
'{"time":241,"type":"viseme","value":"E"},' +
'{"time":282,"type":"viseme","value":"r"},' +
'{"time":376,"type":"viseme","value":"i"},' +
//'{"time":416,"type":"word","start":10,"end":14,"value":"good"},' +
'{"time":416,"type":"viseme","value":"k"},' +
'{"time":481,"type":"viseme","value":"u"},' +
'{"time":576,"type":"viseme","value":"t"},' +
//'{"time":627,"type":"word","start":15,"end":17,"value":"to"},' +
'{"time":627,"type":"viseme","value":"t"},' +
'{"time":674,"type":"viseme","value":"u"},' +
//'{"time":716,"type":"word","start":18,"end":22,"value":"meet"},' +
'{"time":716,"type":"viseme","value":"p"},' +
'{"time":801,"type":"viseme","value":"i"},' +
'{"time":923,"type":"viseme","value":"t"},' +
//'{"time":999,"type":"word","start":23,"end":26,"value":"you"},' +
'{"time":999,"type":"viseme","value":"i"},' +
'{"time":1135,"type":"viseme","value":"u"},' +
'{"time":1357,"type":"viseme","value":"sil"}]';
const obj = JSON.parse(jsonParsed);
let t = 0;
//const lipSync = 'a';
const lipSync = obj[t]['value'];
const vowelInput = {
value: lipSync,
}
// Create custom observable for the 'vowelInput'
vowelInput.onValueChange = new BABYLON.Observable();
// Create function to set current vowel
const setCurrentVowel = (value) => {
if (value === vowelInput.value) {
return value
} else {
vowelInput.value = value
console.log("test", value, vowelInput.value)
vowelInput.onValueChange.notifyObservers(vowelInput.value)
}
}
// Add callback function to the custom observable
vowelInput.onValueChange.add((value) => {
switch (value) {
case 'a': {
// Instead of changing position, you can all lip animations according to the vowel.
casiBody.morphTargetManager.getTarget(12).influence = 1;
casiInnerMouth.morphTargetManager.getTarget(12).influence = 1;
primitive.morphTargetManager.getTarget(3).influence = 1;
casiBody.morphTargetManager.getTarget(11).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(11).influence = 0;
casiBody.morphTargetManager.getTarget(13).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(13).influence = 0;
casiBody.morphTargetManager.getTarget(14).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(14).influence = 0;
casiTeeth.morphTargetManager.getTarget(0).influence = 0;
casiBody.morphTargetManager.getTarget(15).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(15).influence = 0;
casiBody.morphTargetManager.getTarget(16).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(16).influence = 0;
casiTeeth.morphTargetManager.getTarget(0).influence = 0;
casiTeeth.morphTargetManager.getTarget(1).influence = 0;
primitive.morphTargetManager.getTarget(2).influence = 0;
primitive.morphTargetManager.getTarget(4).influence = 0;
primitive.morphTargetManager.getTarget(5).influence = 0;
primitive.morphTargetManager.getTarget(6).influence = 0;
primitive.morphTargetManager.getTarget(7).influence = 0;
break
}
case 'e':
case 'E': {
casiBody.morphTargetManager.getTarget(14).influence = 1;
casiInnerMouth.morphTargetManager.getTarget(14).influence = 1;
casiTeeth.morphTargetManager.getTarget(0).influence = 1;
primitive.morphTargetManager.getTarget(5).influence = 1;
casiBody.morphTargetManager.getTarget(11).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(11).influence = 0;
casiBody.morphTargetManager.getTarget(12).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(12).influence = 0;
casiBody.morphTargetManager.getTarget(13).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(13).influence = 0;
casiBody.morphTargetManager.getTarget(15).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(15).influence = 0;
casiBody.morphTargetManager.getTarget(16).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(16).influence = 0;
casiTeeth.morphTargetManager.getTarget(1).influence = 0;
primitive.morphTargetManager.getTarget(2).influence = 0;
primitive.morphTargetManager.getTarget(3).influence = 0;
primitive.morphTargetManager.getTarget(4).influence = 0;
primitive.morphTargetManager.getTarget(6).influence = 0;
primitive.morphTargetManager.getTarget(7).influence = 0;
break
}
case 'o':
case 'r': {
casiBody.morphTargetManager.getTarget(11).influence = 1;
casiInnerMouth.morphTargetManager.getTarget(11).influence = 1;
primitive.morphTargetManager.getTarget(2).influence = 1;
casiBody.morphTargetManager.getTarget(12).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(12).influence = 0;
casiBody.morphTargetManager.getTarget(13).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(13).influence = 0;
casiBody.morphTargetManager.getTarget(14).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(14).influence = 0;
casiBody.morphTargetManager.getTarget(15).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(15).influence = 0;
casiBody.morphTargetManager.getTarget(16).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(16).influence = 0;
casiTeeth.morphTargetManager.getTarget(0).influence = 0;
casiTeeth.morphTargetManager.getTarget(1).influence = 0;
primitive.morphTargetManager.getTarget(3).influence = 0;
primitive.morphTargetManager.getTarget(4).influence = 0;
primitive.morphTargetManager.getTarget(5).influence = 0;
primitive.morphTargetManager.getTarget(6).influence = 0;
primitive.morphTargetManager.getTarget(7).influence = 0;
break
}
case 'u': {
casiBody.morphTargetManager.getTarget(13).influence = 1;
casiInnerMouth.morphTargetManager.getTarget(13).influence = 1;
primitive.morphTargetManager.getTarget(4).influence = 1;
casiBody.morphTargetManager.getTarget(11).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(11).influence = 0;
casiBody.morphTargetManager.getTarget(12).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(12).influence = 0;
casiBody.morphTargetManager.getTarget(14).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(14).influence = 0;
casiBody.morphTargetManager.getTarget(15).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(15).influence = 0;
casiBody.morphTargetManager.getTarget(16).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(16).influence = 0;
casiTeeth.morphTargetManager.getTarget(0).influence = 0;
casiTeeth.morphTargetManager.getTarget(1).influence = 0;
primitive.morphTargetManager.getTarget(2).influence = 0;
primitive.morphTargetManager.getTarget(3).influence = 0;
primitive.morphTargetManager.getTarget(5).influence = 0;
primitive.morphTargetManager.getTarget(6).influence = 0;
primitive.morphTargetManager.getTarget(7).influence = 0;
break
}
case 'i': {
casiBody.morphTargetManager.getTarget(15).influence = 1;
casiInnerMouth.morphTargetManager.getTarget(15).influence = 1;
primitive.morphTargetManager.getTarget(6).influence = 1;
casiBody.morphTargetManager.getTarget(11).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(11).influence = 0;
casiBody.morphTargetManager.getTarget(12).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(12).influence = 0;
casiBody.morphTargetManager.getTarget(13).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(13).influence = 0;
casiBody.morphTargetManager.getTarget(14).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(14).influence = 0;
casiBody.morphTargetManager.getTarget(16).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(16).influence = 0;
casiTeeth.morphTargetManager.getTarget(0).influence = 0;
casiTeeth.morphTargetManager.getTarget(1).influence = 0;
primitive.morphTargetManager.getTarget(2).influence = 0;
primitive.morphTargetManager.getTarget(3).influence = 0;
primitive.morphTargetManager.getTarget(4).influence = 0;
primitive.morphTargetManager.getTarget(5).influence = 0;
primitive.morphTargetManager.getTarget(7).influence = 0;
break
}
case 'f':
case 'v': {
casiBody.morphTargetManager.getTarget(16).influence = 1;
casiInnerMouth.morphTargetManager.getTarget(16).influence = 1;
casiTeeth.morphTargetManager.getTarget(1).influence = 1;
primitive.morphTargetManager.getTarget(7).influence = 1;
casiBody.morphTargetManager.getTarget(11).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(11).influence = 0;
casiBody.morphTargetManager.getTarget(12).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(12).influence = 0;
casiTeeth.morphTargetManager.getTarget(1).influence = 0;
casiBody.morphTargetManager.getTarget(13).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(13).influence = 0;
casiBody.morphTargetManager.getTarget(14).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(14).influence = 0;
casiBody.morphTargetManager.getTarget(15).influence = 0;
casiInnerMouth.morphTargetManager.getTarget(15).influence = 0;
casiTeeth.morphTargetManager.getTarget(0).influence = 0;
primitive.morphTargetManager.getTarget(2).influence = 0;
primitive.morphTargetManager.getTarget(3).influence = 0;
primitive.morphTargetManager.getTarget(4).influence = 0;
primitive.morphTargetManager.getTarget(5).influence = 0;
primitive.morphTargetManager.getTarget(6).influence = 0;
break
}
}
})
let idx = 0
const vowels = ['a', 'e', 'o', 'u', 'i', 'f']
var soundURL = "speech.mp3";
//alpha is the timer I use
let alpha = 0;
// Change current vowel repeatedly
var speech = new BABYLON.Sound("speech", url + soundURL, scene, function(){
setTimeout(speech.play(), 10000);
});
//const inter = setInterval(() => {
var intervalID = scene.onBeforeRenderObservable.add(function(){
alpha += 1;//continuously add 1 to the timer
if(alpha === obj[t]['time']){//if we've reached a time shown in jsonParsed array
console.log(alpha, t, obj[t]['time']);
setCurrentVowel(obj[t]['value']);
t = (t + 1) % obj.length;
console.log(alpha, t, obj[t]['time']);
}
/*setTimeout(() => {
setInterval(inter, obj[t]['time']);
});*/
//setCurrentVowel(obj[t]['value']);
//t = (t + 1) % obj.length;
//idx = (idx + 1) % 6
});
//}, 1000)
/*function inter(){
setCurrentVowel(obj[t]['value']);
}*/
scene.onDisposeObservable.add(() => {
clearInterval(inter)
})
});
// Our built-in 'sphere' shape.
//var sphere = BABYLON.MeshBuilder.CreateSphere("sphere", {diameter: 2, segments: 32}, scene);
// Move the sphere upward 1/2 its height
//sphere.position.y = 1;
// Our built-in 'ground' shape.
var ground = BABYLON.MeshBuilder.CreateGround("ground", {width: 6, height: 6}, scene);
return scene;
};

How can I remove the white-space around the numbers on a JS timer?

I have a JavaScript timer here, and I am trying to remove the white-space around the numbers so that the entire timer is green.
I have tried using CSS to remove the white-space but have had no luck. I am unsure whether CSS or JS has to be used to remove the space.
This is the code for the timer
An image can be found here
I just want to remove the white-space around the numbers. Thanks
var flagclock = 0;
var flagstop = 1;
var stoptime = 0;
var currenttime;
var splitdate = '';
var output;
var clock;
var thetable = document.getElementById("timerTable");
// Adjust limits and colors here
// Specify limits in seconds
var limit1 = 0;
color1 = "lightgreen";
var limit2 = 360;
color2 = "orange";
var limit3 = 600;
color3 = "red";
function startstop() {
var startdate = new Date();
var starttime = startdate.getTime();
if (flagclock == 0) {
startstop.value = 'Stop';
flagclock = 1;
counter(starttime);
} else {
startstop.value = 'Start';
flagclock = 0;
flagstop = 1;
splitdate = '';
}
}
function counter(starttime) {
output = document.getElementById('output');
clock = document.getElementById('clock');
currenttime = new Date();
var timediff = currenttime.getTime() - starttime;
if (flagstop == 1) {
timediff = timediff + stoptime
}
if (flagclock == 1) {
clock.value = formattime(timediff, '');
refresh = setTimeout('counter(' + starttime + ');', 100);
var secs = timediff / 1000;
var thecolor = "white";
if (secs > limit3) thecolor = color3;
else if (secs > limit2) thecolor = color2;
else if (secs > limit1) thecolor = color1;
thetable.style.backgroundColor = thecolor;
console.log(timediff / 1000)
} else {
window.clearTimeout(refresh);
stoptime = timediff;
}
}
function formattime(rawtime, roundtype) {
if (roundtype == 'round') {
var ds = Math.round(rawtime / 100) + '';
} else {
var ds = Math.floor(rawtime / 100) + '';
}
var sec = Math.floor(rawtime / 1000);
var min = Math.floor(rawtime / 60000);
ds = ds.charAt(ds.length - 1);
if (min >= 15) {
startstop();
}
sec = sec - 60 * min + '';
if (sec.charAt(sec.length - 2) != '') {
sec = sec.charAt(sec.length - 2) + sec.charAt(sec.length - 1);
} else {
sec = 0 + sec.charAt(sec.length - 1);
}
min = min + '';
if (min.charAt(min.length - 2) != '') {
min = min.charAt(min.length - 2) + min.charAt(min.length - 1);
} else {
min = 0 + min.charAt(min.length - 1);
}
return min + ':' + sec;
}
<body onload="startstop();">
<table id="timerTable">
<tr>
<td>
<input class="timerArea" id="clock" type="text" value="00:00" style="text-align:center;font-weight:bold;font-size:14pt;" readonly><br>
</td>
</tr>
</table>
</body>
In order to make the entire timer green, you can set the CSS property background-color of the <input> element to "transparent". I've also removed the element's default border.
.timerArea {
text-align: center;
font-weight: bold;
font-size: 14pt;
background-color: transparent;
border: none;
}
Here's a demonstration:
var flagclock = 0;
var flagstop = 1;
var stoptime = 0;
var currenttime;
var splitdate = '';
var output;
var clock;
var thetable = document.getElementById("timerTable");
// Adjust limits and colors here
// Specify limits in seconds
var limit1 = 0;
color1 = "lightgreen";
var limit2 = 360;
color2 = "orange";
var limit3 = 600;
color3 = "red";
function startstop() {
var startdate = new Date();
var starttime = startdate.getTime();
if (flagclock == 0) {
startstop.value = 'Stop';
flagclock = 1;
counter(starttime);
} else {
startstop.value = 'Start';
flagclock = 0;
flagstop = 1;
splitdate = '';
}
}
function counter(starttime) {
output = document.getElementById('output');
clock = document.getElementById('clock');
currenttime = new Date();
var timediff = currenttime.getTime() - starttime;
if (flagstop == 1) {
timediff = timediff + stoptime
}
if (flagclock == 1) {
clock.value = formattime(timediff, '');
refresh = setTimeout('counter(' + starttime + ');', 100);
var secs = timediff / 1000;
var thecolor = "white";
if (secs > limit3) thecolor = color3;
else if (secs > limit2) thecolor = color2;
else if (secs > limit1) thecolor = color1;
thetable.style.backgroundColor = thecolor;
console.log(timediff / 1000)
} else {
window.clearTimeout(refresh);
stoptime = timediff;
}
}
function formattime(rawtime, roundtype) {
if (roundtype == 'round') {
var ds = Math.round(rawtime / 100) + '';
} else {
var ds = Math.floor(rawtime / 100) + '';
}
var sec = Math.floor(rawtime / 1000);
var min = Math.floor(rawtime / 60000);
ds = ds.charAt(ds.length - 1);
if (min >= 15) {
startstop();
}
sec = sec - 60 * min + '';
if (sec.charAt(sec.length - 2) != '') {
sec = sec.charAt(sec.length - 2) + sec.charAt(sec.length - 1);
} else {
sec = 0 + sec.charAt(sec.length - 1);
}
min = min + '';
if (min.charAt(min.length - 2) != '') {
min = min.charAt(min.length - 2) + min.charAt(min.length - 1);
} else {
min = 0 + min.charAt(min.length - 1);
}
return min + ':' + sec;
}
startstop();
.timerArea {
text-align: center;
font-weight: bold;
font-size: 14pt;
background-color: transparent;
border: none;
}
<table id="timerTable">
<tr>
<td>
<input class="timerArea" id="clock" type="text" value="00:00" readonly><br>
</td>
</tr>
</table>

Semantic analyzer for checking type incompatibilities - Symbol table Implementation?

I got this code for Semanitic Analysis. Can't figure out the code for Symbol Table to make it run? Making a 2D array like this :
String[,] Symboltable = new String[20, 6];
Gives error of wrong number of indices inside, which I resolved but still wouldn't work. What is the other way I could do this?
Region Semantic Analyzer
void Semantic_Analysis(int k)
{
Regex variable_Reg = new Regex(#"^[A-Za-z|_][A-Za-z|0-9]*$");
if (finalArray[k].Equals("+"))
{
if (variable_Reg.Match(finalArray[k - 1] + "").Success && variable_Reg.Match(finalArray[k + 1] + "").Success)
{
String type = finalArray[k - 4] + "";
String left_side = finalArray[k - 3] + "";
int left_side_i = 0;
int left_side_j = 0;
String before = finalArray[k - 1] + "";
int before_i = 0;
int before_j = 0;
String after = finalArray[k + 1] + "";
int after_i = 0;
int after_j = 0;
for (int i = 0; i < Symboltable.Count; i++)
{
for (int j = 0; j < Symboltable[i].Count; j++)
{
if (Symboltable[i][j].Equals(left_side))
{ left_side_i = i; left_side_j = j; }
if (Symboltable[i][j].Equals(before))
{ before_i = i; before_j = j; }
if (Symboltable[i][j].Equals(after))
{ after_i = i; after_j = j; }
}
}
if (type.Equals(Symboltable[before_i][2]) && type.Equals(Symboltable[after_i][2]) && Symboltable[before_i][2].Equals(Symboltable[after_i][2]))
{
int Ans = Convert.ToInt32(Symboltable[before_i][3]) + Convert.ToInt32(Symboltable[after_i][3]);
Constants.Add(Ans);
}
if (Symboltable[left_side_i][2].Equals(Symboltable[before_i][2]) && Symboltable[left_side_i][2].Equals(Symboltable[after_i][2]) && Symboltable[before_i][2].Equals(Symboltable[after_i][2]))
{
int Ans = Convert.ToInt32(Symboltable[before_i][3]) + Convert.ToInt32(Symboltable[after_i][3]);
Constants.RemoveAt(Constants.Count - 1);
Constants.Add(Ans);
Symboltable[left_side_i][3] = Ans + "";
}
}
}
if (finalArray[k].Equals("-"))
{
if (variable_Reg.Match(finalArray[k - 1] + "").Success && variable_Reg.Match(finalArray[k + 1] + "").Success)
{
String type = finalArray[k - 4] + "";
String left_side = finalArray[k - 3] + "";
int left_side_i = 0;
int left_side_j = 0;
String before = finalArray[k - 1] + "";
int before_i = 0;
int before_j = 0;
String after = finalArray[k + 1] + "";
int after_i = 0;
int after_j = 0;
for (int i = 0; i < Symboltable.Count; i++)
{
for (int j = 0; j < Symboltable[i].Count; j++)
{
if (Symboltable[i][j].Equals(left_side))
{ left_side_i = i; left_side_j = j; }
if (Symboltable[i][j].Equals(before))
{ before_i = i; before_j = j; }
if (Symboltable[i][j].Equals(after))
{ after_i = i; after_j = j; }
}
}
if (type.Equals(Symboltable[before_i][2]) && type.Equals(Symboltable[after_i][2]) && Symboltable[before_i][2].Equals(Symboltable[after_i][2]))
{
int Ans = Convert.ToInt32(Symboltable[before_i][3]) - Convert.ToInt32(Symboltable[after_i][3]);
Constants.Add(Ans);
}
if (Symboltable[left_side_i][2].Equals(Symboltable[before_i][2]) && Symboltable[left_side_i][2].Equals(Symboltable[after_i][2]) && Symboltable[before_i][2].Equals(Symboltable[after_i][2]))
{
int Ans = Convert.ToInt32(Symboltable[before_i][3]) + Convert.ToInt32(Symboltable[after_i][3]);
Constants.RemoveAt(Constants.Count - 1);
Constants.Add(Ans);
Symboltable[left_side_i][3] = Ans + "";
}
}
}
if (finalArray[k].Equals(">"))
{
if (variable_Reg.Match(finalArray[k - 1] + "").Success && variable_Reg.Match(finalArray[k + 1] + "").Success)
{
String before = finalArray[k - 1] + "";
int before_i = 0;
int before_j = 0;
String after = finalArray[k + 1] + "";
int after_i = 0;
int after_j = 0;
for (int i = 0; i < Symboltable.Count; i++)
{
for (int j = 0; j < Symboltable[i].Count; j++)
{
if (Symboltable[i][j].Equals(before))
{ before_i = i; before_j = j; }
if (Symboltable[i][j].Equals(after))
{ after_i = i; after_j = j; }
}
}
if (Convert.ToInt32(Symboltable[before_i][3]) > Convert.ToInt32(Symboltable[after_i][3]))
{
int start_of_else = finalArray.IndexOf("else");
int end_of_else = finalArray.Count - 1;
for (int i = end_of_else; i >= start_of_else; i--)
{
if (finalArray[i].Equals("}"))
{
if (i < finalArray.Count - 2)
{ end_of_else = i; }
}
}
for (int i = start_of_else; i <= end_of_else; i++)
{ finalArray.RemoveAt(start_of_else); }
}
else
{
int start_of_if = finalArray.IndexOf("if");
int end_of_if = finalArray.IndexOf("}");
for (int i = start_of_if; i <= end_of_if; i++)
{ finalArray.RemoveAt(start_of_if); }
if_deleted = true;
}
}
}
}
#endregion

msp430f2618 fading LED using pwm

I'm trying to write code in C to fade an off-board LED using PWM and the MSP430f2618. I can get the LED to turn on but it stays at full intensity. I am trying to read in an array of frequency values and fade the LED based on the frequency values.
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
int array_size = 0, i = 0, delay = 0;
double frequency[50] = {0.0};
array_size = sizeof(frequency);
frequency [0] = 60.0;
for (i = 1; i < array_size; i++)
{
if (frequency[i - 1] < 61)
{
frequency[i] = frequency[i-1] + 0.1;
}
else
{
frequency[i] = 60.0;
}
}
P4OUT &= 0;
P4DIR |= (BIT1 + BIT2); //P4.1 and P4.2 output
P4SEL &= ~(BIT1 + BIT2); //P4.1 and P4.2 TBx options, timer select
TBCCR0 = 512-1;
TBCCTL1 = OUTMOD_7;
TBCCTL2 = OUTMOD_7;
for (i = 0; i < array_size; i++)
{
P4OUT &= 0;
if ((frequency[i] < 60.2) && (frequency[i] >=60.0))
{
//TBCCR1 = 3200;
TBCCR1 = 384;
}
else if ((frequency[i] < 60.4) && (frequency[i] >=60.2))
{
//TBCCR1 = 2560;
TBCCR1 = 256;
}
else if ((frequency[i] < 60.6) && (frequency[i] >=60.4))
{
//TBCCR1 = 1920;
TBCCR1 = 128;
}
else if ((frequency[i] < 60.8) && (frequency[i] >=60.6))
{
//TBCCR1 = 1280;
TBCCR1 = 64;
}
else if ((frequency[i] < 61) && (frequency[i] >=60.8))
{
//TBCCR1 = 640;
TBCCR1 = 32;
}
else
{
TBCCR2 = 512;
}
P4OUT ^= BIT1;
for (delay = 0; delay < 32000; delay++);
}
TBCTL = TBSSEL_2 + MC_1; // ACLK, up mode
__bis_SR_register(LPM0_bits); // Enter LPM3
return 0;
}
The timer is not running until you start it by setting the MC field. That initialization must be done at the beginning.

Resources