Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Help with a function please

Voxer

New Coder
I´ve been trying to run this function with delays to turn on some LED's
JavaScript:
function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}
 


async function functiontest() {
 
  GPIO26value = 1;
  LED26 = 1;
  await sleep(3000);
 
  GPIO26value = 0;
  LED26 = 0;
  await sleep(3000);
 
  GPIO20value = 1;
  LED20 = 1;
  await sleep(3000);
 
  GPIO20value = 0;
  LED20 = 0;
  await sleep(3000);
 
  GPIO15value = 1;
  LED15 = 1;
  await sleep(3000);
 
  GPIO15value = 0;
  LED15 = 0;
  await sleep(3000);
 
  GPIO21value = 1
  LED21 = 1;
  await sleep(3000);
 
  GPIO21value = 0;
  LED21 = 0;
  await sleep(3000);
 
  GPIO16value = 1;
  LED16 = 1;
  await sleep(3000);
 
  GPIO16value = 0;
  LED16 = 0;
  await sleep(3000);
 
  GPIO19value = 1;
  LED19 = 1;
  await sleep(3000);
 
  GPIO19value = 0;
  LED19 = 0;
  await sleep(3000);
 
  GPIO13value = 1;
  LED13 = 1;
  await sleep(3000);
 
  GPIO13value = 0;
  LED13 = 0;
  await sleep(3000);
 
  GPIO6value = 1;
  LED6 = 1;
  await sleep(3000);
 
  GPIO6value = 0;
  LED6 = 0;
  await sleep(3000);
 
  GPIO5value = 1;
  LED5 = 1;
  await sleep(3000);
 
  GPIO5value = 0;
  LED5 = 0;
  await sleep(3000);
 
  GPIO12value = 1;
  LED12 = 1;
  await sleep(3000);
 
  GPIO12value = 0;
  LED12 = 0;
  await sleep(3000);
 
  GPIO25value = 1;
  LED25 = 1;
  await sleep(3000);
 
  GPIO25value = 0;
  LED25 = 0;
  await sleep(3000);
 
  GPIO24value = 1;
  LED24 = 1;
  await sleep(3000);
 
  GPIO24value = 0;
  LED24 = 0;
  await sleep(3000);
 
  GPIO23value = 1;
  LED23 = 1;
  await sleep(3000);
 
  GPIO23value = 0;
  LED23 = 0;
  await sleep(3000);
 
  GPIO22value = 1;
  LED22 = 1;
  await sleep(3000);
 
  GPIO22value = 0;
  LED22 = 0;
  await sleep(3000);
 
  GPIO27value = 1;
  LED27 = 1;
  await sleep(3000);
 
  GPIO27value = 0;
  LED27 = 0;
  await sleep(3000);
 
  GPIO17value = 1;
  LED17 = 1;
  await sleep(3000);
 
  GPIO17value = 0;
  LED17 = 0;
  await sleep(3000);
 
  GPIO18value = 1;
  LED18 = 1;
  await sleep(3000);
 
  GPIO18value = 0;
  LED18 = 0;
  await sleep(3000);
 
  GPIO4value = 1;
  LED4 = 1;
  await sleep(3000);
 
  GPIO4value = 0;
  LED4 = 0;
  await sleep(3000);
 
  GPIO14value = 1;
  LED14 = 1;
  await sleep(3000);
 
  GPIO14value = 0;
  LED14 = 0; 
}
 

   

if(GPIO2value == 1){
functiontest();
}
could someone please tell me what im doing wrong
Am using a Raspbery Pi B+
 
Last edited by a moderator:
I think setTimeout actually returns immediately an ID so what you are doing is resolving a Promise immediately with a setTimeout.
What you should try for is like new Promise, and resolve it when function is done using resolve method.
JavaScript:
let promise = new Promise();
setTimout(function(){promise.resolve()},ms);
return promise;
X E.
 
Well, I searched about it over the internet, and I found there are some issues with your code.
You can try this code to fix the error.

Code:
const Gpio = require('onoff').Gpio;


// Define GPIO pins and LEDs
const pins = [
  { gpioPin: 26, ledPin: 1 },
  { gpioPin: 20, ledPin: 2 },
  // Add more pin mappings as needed
];


// Initialize GPIO pins and LEDs
const gpioPins = pins.map(({ gpioPin }) => new Gpio(gpioPin, 'out'));
const leds = pins.map(({ ledPin }) => new Gpio(ledPin, 'out'));


async function functiontest() {
  try {
    for (let i = 0; i < pins.length; i++) {
      // Turn on LED
      leds[i].writeSync(1);
      gpioPins[i].writeSync(1); // Set GPIO pin to 1 (assuming GPIO is used for some purpose)
      await sleep(3000);
      
      // Turn off LED
      leds[i].writeSync(0);
      gpioPins[i].writeSync(0); // Set GPIO pin to 0
      await sleep(3000);
    }
  } catch (error) {
    console.error('Error:', error);
  }
}


// Function to sleep
function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}


// Call the functiontest function if GPIO2value is 1
if (GPIO2value == 1) {
  functiontest();
}

Thanks
 

Latest posts

Buy us a coffee!

Back
Top Bottom