2

I want to halt the execution of that code, when arduino is receiving serial data for the execution. Right now the Serial data runs when the other code has completed its execution. How can I do it ?

void loop() { 
 dmd.setBrightness(brighVal); // set the brightness to led matrix
 scroll(); //function to scroll message
}
void serialEvent() {
 while (Serial.available()) {
 Jsondata = Serial.readString(); //reading json data from APP
 DynamicJsonBuffer jsonBuffer;
 Serial.println(Jsondata);
 JsonObject& root = jsonBuffer.parseObject(Jsondata);
 if (root.containsKey("BiM")) {
 brighVal = root["BiM"];
 switch (brighVal) { // set's the value of brightness according to brighVal
 case 0:
 brighVal = 2;
 break;
 case 1:
 brighVal = 10;
 break;
 case 2 :
 brighVal = 25;
 break;
 case 3 :
 brighVal = 200;
 break;
 }
 }
 }
}
void scroll() {
 dmd.clearScreen( true );
 dmd.selectFont(Arial_Black_16);
 dmd.drawMarquee("Scrolling Text", 17, (32 * DISPLAYS_ACROSS) - 1, 0);
 long start = millis();
 long timer = start;
 boolean ret = false;
 while (!ret) {
 if ((timer + 30) < millis()) {
 ret = dmd.stepMarquee(-1, 0);
 timer = millis();
 }
 }
}
asked Dec 20, 2017 at 13:03

1 Answer 1

7

Then let scroll return immediately

void scroll() {
 static long timer = 0;
 static boolean scroll = true;
 if(scroll){
 dmd.clearScreen( true );
 dmd.selectFont(Arial_Black_16);
 dmd.drawMarquee("Scrolling Text", 17, (32 * DISPLAYS_ACROSS) - 1, 0);
 scroll = false;
 timer = millis()
 }else {
 if ((millis() - timer) > 30) {
 scroll = dmd.stepMarquee(-1, 0);
 timer = millis();
 }
 }
}

The static keyword means that the value of the variable will be kept across calls.

I used the fact that it will be called in a loop to remove the while loop inside. I split the function up into 2 parts: The reset and the update. We first run the reset to init the marquee. After the reset it will set the boolean so the next time it will enter the update part. Then I used the exit condition of the original loop to make it reenter the update part next time.

answered Dec 20, 2017 at 13:16
6
  • it worked .Can you please explain the changes? Thank you so much for helping out. Commented Dec 20, 2017 at 13:34
  • @RaviParmar I added some explanation. Commented Dec 20, 2017 at 14:10
  • What if i have multiple functions @ratchetfreak. for example Commented Dec 21, 2017 at 14:35
  • @RaviParmar Do the same for each function, make sure that no function ever blocks for an extended period of time but instead returns instead. Then using saved state in a global or static local resume the correct actions. Commented Dec 21, 2017 at 14:37
  • thank you @ratchetfrek. it seems that i have used lot of delay functions in all the functions because of which arduino got halted. Commented Dec 21, 2017 at 14:40

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.