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, 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;
};
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>
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
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.