xxxxxxxxxx
191
let voltageSlider;
let voltage = 2.55;
let current = 10;
let canvasWidth = 600;
let canvasHeight = 550;
let plotWidth = 500;
let plotHeight = 400;
let xOffset = 70;
let yOffset = 50;
let dataPoints = [];
let voltages = [];
let currents = [];
function setup() {
createCanvas(canvasWidth, canvasHeight);
background('aliceblue');
// Initialize data points
dataPoints = [
{ v: 2.39, i: 0 },
{ v: 2.52, i: 1 },
{ v: 2.53, i: 2 },
{ v: 2.54, i: 5 },
{ v: 2.55, i: 7 },
{ v: 2.56, i: 10 },
{ v: 2.58, i: 20 },
{ v: 2.60, i: 28 },
{ v: 2.61, i: 34 },
{ v: 2.65, i: 52 },
{ v: 2.68, i: 71 },
{ v: 2.70, i: 88 },
{ v: 2.72, i: 100 },
{ v: 2.74, i: 113 },
{ v: 2.76, i: 124 },
{ v: 2.78, i: 140 }
];
// Extract voltages and currents for interpolation
for (let point of dataPoints) {
voltages.push(point.v);
currents.push(point.i);
}
// Create the voltage slider
voltageSlider = createSlider(2.35, 2.78, 2.55, 0.01);
voltageSlider.position(xOffset, plotHeight + yOffset + 50);
voltageSlider.style('width', `${plotWidth}px`);
}
function draw() {
background('aliceblue');
fill('snow');
rect(xOffset, yOffset, plotWidth, plotHeight);
drawGrid();
drawAxes();
drawCurve();
drawDataPoints();
drawLabels();
// Get the current voltage value from the slider
voltage = voltageSlider.value();
// Draw the moving dot
drawDotOnCurve(voltage);
}
function drawGrid() {
stroke(200); // light gray for grid lines
strokeWeight(1);
// Vertical grid lines
for (let x = xOffset; x <= xOffset + plotWidth; x += plotWidth / 10) {
line(x+9, yOffset, x+9, yOffset + plotHeight);
}
// Horizontal grid lines
for (let y = yOffset; y <= yOffset + plotHeight; y += plotHeight / 7) {
line(xOffset, y, xOffset + plotWidth, y);
}
}
function drawAxes() {
stroke(0);
strokeWeight(2);
// X-axis
line(xOffset, yOffset + plotHeight, xOffset + plotWidth, yOffset + plotHeight);
// Y-axis
line(xOffset, yOffset, xOffset, yOffset + plotHeight);
// X-axis ticks and labels
textSize(12);
fill(0);
noStroke();
textAlign(CENTER, TOP);
let xTickStep = 0.05;
for (let v = 2.35; v <= 2.78; v += xTickStep) {
let x = map(v, 2.35, 2.78, xOffset, xOffset + plotWidth);
stroke(0);
line(x, yOffset + plotHeight, x, yOffset + plotHeight + 5);
noStroke();
text(v.toFixed(2), x, yOffset + plotHeight + 8);
}
// Y-axis ticks and labels
textAlign(RIGHT, CENTER);
let yTickStep = 20;
for (let i = 0; i <= 140; i += yTickStep) {
let y = map(i, 0, 140, yOffset + plotHeight, yOffset);
stroke(0);
line(xOffset - 5, y, xOffset, y);
noStroke();
text(i, xOffset - 10, y);
}
}
function drawCurve() {
noFill();
stroke('black');
strokeWeight(2);
beginShape();
for (let v = 2.35; v <= 2.78; v += 0.001) {
let i = interpolateCurrent(v);
let x = map(v, 2.35, 2.78, xOffset, xOffset + plotWidth);
let y = map(i, 0, 140, yOffset + plotHeight, yOffset);
vertex(x, y);
}
endShape();
}
function drawDataPoints() {
fill('red');
noStroke();
for (let point of dataPoints) {
let x = map(point.v, 2.35, 2.78, xOffset, xOffset + plotWidth);
let y = map(point.i, 0, 140, yOffset + plotHeight, yOffset);
ellipse(x, y, 6, 6);
}
}
function drawDotOnCurve(v) {
let i = interpolateCurrent(v);
let x = map(v, 2.35, 2.78, xOffset, xOffset + plotWidth);
let y = map(i, 0, 140, yOffset + plotHeight, yOffset);
fill('blue');
noStroke();
circle(x, y, 10);
}
function drawLabels() {
// Title
noStroke();
fill('black');
textSize(18);
textAlign(CENTER);
text("LED Noodle Voltage vs. Current", canvasWidth / 2, 30);
// X-axis label
textSize(16);
text("Input Voltage (V)", canvasWidth / 2, plotHeight + 85);
text("Voltage="+ voltage, 120, plotHeight + 130);
text("Current(mA)=" + round(interpolateCurrent(voltage), 2), 250, plotHeight + 130);
// Y-axis label
push();
translate(20, canvasHeight / 2);
rotate(-PI / 2);
text("Current (mA)", 0, 0);
pop();
}
function interpolateCurrent(v) {
// Linear interpolation between data points
if (v <= voltages[0]) return currents[0];
if (v >= voltages[voltages.length - 1]) return currents[currents.length - 1];
for (let i = 0; i < voltages.length - 1; i++) {
if (v >= voltages[i] && v <= voltages[i + 1]) {
let v1 = voltages[i];
let v2 = voltages[i + 1];
let i1 = currents[i];
let i2 = currents[i + 1];
let fraction = (v - v1) / (v2 - v1);
return i1 + fraction * (i2 - i1);
}
}
return 0;
}