matlab - Lighting a LED continuously and independent from the main script -


im trying develop small circuit using matlab , arduino.
i want turn led bulb on inside script display output.
but, instead of turning on need keep blinking continuously.

the way know how use infinite loop , thats not need because want script keep running while having blinking led.
can point me in right direction please ?

you cannot run 2 lines of code t time in arduino. arduino has 1 processor can execute 1 line of code @ time. if want make program said have 2 options

  • either need program linearly. specify each line of code required in program separately. in case during execution of normal program write code turning on , off linearly need ( blink). difficult when program large, make program more complex , large.
  • second method obtained here. haven't given code here, demonstrating simultaneous run of several task choose program above site :

    // severalthingsatthesametime.ino  // expansion of blinkwithoutdelay concept illustrate how script //  can appear several things @ same time  // sketch following //    blinks onboard led (as in blinkwithoutdelay sketch) //    blinks 2 external leds (leda , ledb) connected pins 12 , 11. //    turns led (buttonled connected pin 10) on or off whenever button //       connected pin 7 pressed //    sweeps servo (connected pin 5) , forth @ different speeds   //  1 leg of each led should connected relevant pin , other leg should connected //   resistor of 470 ohms or more , other end of resistor arduino gnd.  //   if led doesn't light connected wrong way round.  //  on uno , mega "button" piece of wire inserted pin 7.  //   touching end of wire moist finger sufficient cause switching action //   of course proper press-on-release-off button switch used!  //  arduino not capable of supplying enough 5v power operate servo //    servo should have it's own power supply , power supply ground should //      connected arduino ground.  // sketch written illustrate few different programming features. //    use of many functions short pieces of code.  //       short pieces of code easier follow , debug //    use of variables record state of (e.g. onboardledstate) means //       enable different functions determine do. //    use of millis() manage timing of activities //    definition of numbers used program @ top of sketch  //       can found if need changed   //========================================  // ----------libraries--------------  #include <servo.h>  // --------constants (won't change)---------------  const int onboardledpin =  13;      // pin numbers leds const int led_a_pin = 12; const int led_b_pin = 11; const int buttonled_pin = 10;  const int buttonpin = 7; // pin number button  const int servopin = 5; // pin number servo signal  const int onboardledinterval = 500; // number of millisecs between blinks const int led_a_interval = 2500; const int led_b_interval = 4500;  const int blinkduration = 500; // number of millisecs led's on - 3 leds use  const int buttoninterval = 300; // number of millisecs between button readings  const int servomindegrees = 20; // limits servo movement const int servomaxdegrees = 150;   //------------ variables (will change)---------------------  byte onboardledstate = low;             // used record whether leds on or off byte led_a_state = low;           //   low = off byte led_b_state = low; byte buttonled_state = low;  servo myservo;  // create servo object control servo   int servoposition = 90;     // current angle of servo - starting @ 90. int servoslowinterval = 80; // millisecs between servo moves int servofastinterval = 10; int servointerval = servoslowinterval; // initial millisecs between servo moves int servodegrees = 2;       // amount servo moves @ each step                          //    changed negative value movement in other direction  unsigned long currentmillis = 0;    // stores value of millis() in each iteration of loop() unsigned long previousonboardledmillis = 0;   // store last time led updated unsigned long previousled_a_millis = 0; unsigned long previousled_b_millis = 0;  unsigned long previousbuttonmillis = 0; // time when button press last checked  unsigned long previousservomillis = 0; // time when servo last moved   //========================================  void setup() {    serial.begin(9600);   serial.println("starting severalthingsatthesametimerev1.ino");  // know sketch running        // set led pins output:   pinmode(onboardledpin, output);   pinmode(led_a_pin, output);   pinmode(led_b_pin, output);   pinmode(buttonled_pin, output);    // set button pin input pullup resistor ensure defaults high  pinmode(buttonpin, input_pullup);   myservo.write(servoposition); // sets initial position  myservo.attach(servopin);  }   //========================================  void loop() {        // notice none of action happens in loop() apart reading millis()       //   calls functions have action code    currentmillis = millis();   // capture latest value of millis()                           //   equivalent noting time clock                           //   use same time led flashes keep them synchronized    readbutton();               // call functions work   updateonboardledstate();   updateled_a_state();   updateled_b_state();   switchleds();   servosweep();  }  //========================================   void updateonboardledstate() {     if (onboardledstate == low) {            // if led off, must wait interval expire before turning on      if (currentmillis - previousonboardledmillis >= onboardledinterval)  {       // time up, change state high    onboardledstate = high;       // , save time when made change    previousonboardledmillis += onboardledinterval;       // note: previous line alternatively       //              previousonboardledmillis = currentmillis       //        style used in blinkwithoutdelay example sketch       //        adding on interval better way ensure succesive periods identical  } } else {  // i.e. if onboardledstate high        // if led on, must wait duration expire before turning off if (currentmillis - previousonboardledmillis >= blinkduration) {       // time up, change state low    onboardledstate = low;       // , save time when made change    previousonboardledmillis += blinkduration; }  } }  //========================================  void updateled_a_state() {    if (led_a_state == low) {     if (currentmillis - previousled_a_millis >= led_a_interval) {     led_a_state = high;    previousled_a_millis += led_a_interval; }  }  else {   if (currentmillis - previousled_a_millis >= blinkduration) {     led_a_state = low;     previousled_a_millis += blinkduration; }  }     }  //========================================    void updateled_b_state() {    if (led_b_state == low) {     if (currentmillis - previousled_b_millis >= led_b_interval) {        led_b_state = high;        previousled_b_millis += led_b_interval;     }   }   else {     if (currentmillis - previousled_b_millis >= blinkduration) {        led_b_state = low;        previousled_b_millis += blinkduration;     }   }     }  //========================================  void switchleds() {    // code switches leds on , off     digitalwrite(onboardledpin, onboardledstate);  digitalwrite(led_a_pin, led_a_state);  digitalwrite(led_b_pin, led_b_state);  digitalwrite(buttonled_pin, buttonled_state); }  //========================================  void readbutton() {    // reads button state after button interval has elapsed   //  avoids multiple flashes if button bounces   // every time button pressed changes buttonled_state causing led go on or off   // notice there no need synchronize use of millis() flashing leds  if (millis() - previousbuttonmillis >= buttoninterval) {  if (digitalread(buttonpin) == low) {   buttonled_state = ! buttonled_state; // changes low if high                                         //   , high if low   previousbuttonmillis += buttoninterval; }  }  }  //======================================   void servosweep() {    // similar servo sweep example except uses millis() rather delay()    // nothing happens unless interval has expired   // value of currentmillis set in loop()    if (currentmillis - previousservomillis >= servointerval) {     // time move previousservomillis += servointerval;  servoposition = servoposition + servodegrees; // servodegrees might negative  if (servoposition <= servomindegrees) {       // when servo gets minimum position change interval change speed    if (servointerval == servoslowinterval) {      servointerval = servofastinterval;    }    else {     servointerval = servoslowinterval;    } }  if ((servoposition >= servomaxdegrees) || (servoposition <= servomindegrees))  {       // if servo @ either extreme change sign of degrees make move other way   servodegrees = - servodegrees; // reverse direction       // , update position ensure within range   servoposition = servoposition + servodegrees;  }      // make servo move next position myservo.write(servoposition);     // , record time when move happened   }  }  //========================================end 

the above sketch uses concept in "blink without delay" cause 3 leds blink @ different intervals, fourth led controlled button , servo sweeps , forth @ 2 different speeds. idea demonstrate how different processes can accommodated in same general framework.


Comments