The 555 timer IC is an integrated circuit used in a variety of timer, delay, pulse generation, and oscillator applications. It is one of the most popular timing ICs due to its flexibility and price.
Prompt
Create a single file p5.js simulation of a 555 timer in monostable mode.
Add sliders for resistance and capacitance.
Make the canvas be 400x250.
Make the text size be 16.
Draw a waveform and an LED that flashes.
Place the sliders at the bottom of the canvas in the bottom 50 pixels.
Add labels and values to the left of the sliders.
Response
1
This code will create an interactive simulation where you can adjust the resistance and capacitance values using sliders and see the effect on the LED flash rate and the waveform displayed. The LED and waveform visually represent the timer's operation in monostable mode.
letresistanceSlider,capacitanceSlider;letledState=false;letlastTriggerTime=0;letwaveform=[];constcanvasWidth=400,canvasHeight=450;functionsetup(){createCanvas(canvasWidth,canvasHeight);textSize(12);// SlidersresistanceSlider=createSlider(1,100,50);resistanceSlider.position(20,canvasHeight-30);capacitanceSlider=createSlider(1,100,50);capacitanceSlider.position(220,canvasHeight-30);}functiondraw(){background(255);// Draw the waveformdrawWaveform();// Update and draw LEDupdateLED();drawLED();// Draw slider labels and valuesdrawLabelsAndValues();}functiondrawWaveform(){stroke(0);noFill();beginShape();for(leti=0;i<waveform.length;i++){letx=map(i,0,waveform.length,0,width);lety=map(waveform[i],0,1,150,100);vertex(x,y);}endShape();if(frameCount%2===0){waveform.push(ledState?1:0);if(waveform.length>width){waveform.splice(0,1);}}}functionupdateLED(){letcurrentTime=millis();letthresholdTime=lastTriggerTime+(resistanceSlider.value()*capacitanceSlider.value());if(currentTime>thresholdTime){ledState=!ledState;lastTriggerTime=currentTime;}}functiondrawLED(){fill(ledState?color(255,0,0):color(128));ellipse(200,50,30,30);}functiondrawLabelsAndValues(){fill(0);text("Resistance: "+resistanceSlider.value()+" Ω",20,canvasHeight-40);text("Capacitance: "+capacitanceSlider.value()+" µF",220,canvasHeight-40);}// Add this function to handle window resizingfunctionwindowResized(){resizeCanvas(canvasWidth,canvasHeight);}