Scripted Component Help
Quote from owhs on October 11, 2024, 9:15 pmHi I work only in analogue circuits; I'm trying to create a scripted component which functions as a 2s timer relay
24v across A1 - A2
T1 immediately gets fired, then after 2s it switches to T2
Initial code before trying to introduce any kind of timing logic;
IoPin@ A1 = component.getPin("A1"); IoPin@ A2 = component.getPin("A2"); IoPin@ COM = component.getPin("COM"); IoPin@ T1 = component.getPin("T1"); IoPin@ T2 = component.getPin("T2"); void setup() { print("timerrelay init "); } void reset() { print("resetting timerrelay"); A1.setPinMode( 1 ); // Input A2.setPinMode( 1 ); // Input COM.setPinMode( 1 ); // Intput T1.setPinMode( 3 ); // Output T2.setPinMode( 3 ); // Output A1.changeCallBack( element, true ); } void voltChanged() { bool input = A1.getInpState(); // print("timerrelay input changed " + input ); if (input) { T1.setVoltage(24); } else { T1.setVoltage(0); } }
I spoke with chatgpt; https://chatgpt.com/share/67099316-9fc8-8011-825f-54448e068d58
Failed to implement:
1. The error you're seeing is because
component.schedule()
might not be available in the scripting environment you're working with in SimulIDE. Let's work around that by implementing our own timer using a loop with a time check instead.-----
2. It seems that the
getTime()
function is also not available in your environment. We'll need to find another way to handle timing without relying on built-in timing functions likegetTime()
orschedule()
.
Is there a delayed called, or "setTimeout" in JS equivalent I can use?
Many thanks! 🙂
Hi I work only in analogue circuits; I'm trying to create a scripted component which functions as a 2s timer relay
24v across A1 - A2
T1 immediately gets fired, then after 2s it switches to T2
Initial code before trying to introduce any kind of timing logic;
IoPin@ A1 = component.getPin("A1");
IoPin@ A2 = component.getPin("A2");
IoPin@ COM = component.getPin("COM");
IoPin@ T1 = component.getPin("T1");
IoPin@ T2 = component.getPin("T2");
void setup()
{
print("timerrelay init ");
}
void reset()
{
print("resetting timerrelay");
A1.setPinMode( 1 ); // Input
A2.setPinMode( 1 ); // Input
COM.setPinMode( 1 ); // Intput
T1.setPinMode( 3 ); // Output
T2.setPinMode( 3 ); // Output
A1.changeCallBack( element, true );
}
void voltChanged()
{
bool input = A1.getInpState();
// print("timerrelay input changed " + input );
if (input) {
T1.setVoltage(24);
} else {
T1.setVoltage(0);
}
}
I spoke with chatgpt; https://chatgpt.com/share/67099316-9fc8-8011-825f-54448e068d58
Failed to implement:
1. The error you're seeing is because component.schedule()
might not be available in the scripting environment you're working with in SimulIDE. Let's work around that by implementing our own timer using a loop with a time check instead.
-----
2. It seems that the getTime()
function is also not available in your environment. We'll need to find another way to handle timing without relying on built-in timing functions like getTime()
or schedule()
.
Is there a delayed called, or "setTimeout" in JS equivalent I can use?
Many thanks! 🙂
Quote from arcachofo on October 11, 2024, 10:05 pmHi.
ChatGpt idea was good, but he doesn't know the correct syntax.
Instead of:component.schedule(2000, "switchVoltage"); // Schedule the switch in 2 seconds (2000 ms)
You can use this:
component.addEvent( 2e12 ); // Schedule the switch in 2 seconds (2e12 ps)
And instead of:
void switchVoltage()
Use this:
void runEvent()
If you find any other problem don't hesitate to ask.
Hi.
ChatGpt idea was good, but he doesn't know the correct syntax.
Instead of:
component.schedule(2000, "switchVoltage"); // Schedule the switch in 2 seconds (2000 ms)
You can use this:
component.addEvent( 2e12 ); // Schedule the switch in 2 seconds (2e12 ps)
And instead of:
void switchVoltage()
Use this:
void runEvent()
If you find any other problem don't hesitate to ask.