Make Arduino UNO Interact with Sprite


#1

Hello,

I’m writing to you today because I’m trying to carry out an action that I did with the previous version ( 3) of mblock.

I have arduino Uno boards and I used to be able to put arduino and sprite blocks in the same block, which I can no longer do with version 5 of mblock.

Here is a print screen of my block, could you tell me if it is still possible to make it?

Thanks in advance


#2

Hi @dastu,
This now is done through the Upload Mode Broadcast extension. To add the extension:
Click here:
image

Add the first extension:
image

Add the same extension to your sprite in the spites tab. Your code will look like this:

On your Arduino:

On your Sprite:
image


#3
French Arduino Code

French Sprite Code

image

Faites-moi savoir si vous avez besoin d’autre chose !


#4

Hello @Best_codes,

First of all, thank you for taking the time to reply.

I’m really new to block programming but also arduino.

I’ve set up your procedure, but I’d have a few questions about how it works.
First question: in your code, you’re going to read the digital pin 3 instead of 13, can you tell me why?

Test 1: if I do exactly what you’ve indicated, I get the result that the led works as indicated in the program, but my sprite always remains on “night”.

image

Test 2 : I changed the value of the second “if” to 0

image

Result: well, it works, the led and the sprite work together. But I don’t quite understand why?

Test 3 : Set read digital pin to 13

image

Here’s the result: the arduino board’s LED remains permanently lit, but my sprite changes state.

Thanks again for your time


#5

The value of pin 3 is not changing, I put the wrong pin number, sorry. :frowning:
It should be:

In which case the LED should turn on and off and the sprite should print the value.
I could help you a lot better if I knew what you are trying to do exactly. :slight_smile:


#6

Hello ,

So I just want to do what you told me.

The LED on my arduino when it flashes, I want my sprite to say ‘day’ and when it is off I want it to say ‘night’.

To do another test I added my shield with the grove led connected to D3 but it still doesn’t work.

I’ll explain with your code, my D3 led flashes but my sprite remains stuck on ‘night’.

However, if I replace =1 with =0 in my second if, it works but I get the impression that I’m getting round my problem.

I don’t think it’s complicated, but it’s sticking with your code.

I’ve changed my arduino board just in case, but it’s the same thing.

Thanks again for your help.


#7

@dastu The problem is not likely related to the code, but to the hardware.

If you set a digital pin as high or low, it will not be in an input state, it will be in an output state. So when we light up the LED in the code, it does something like this in the Arduino code:

#include "src/OfflineBroadcast.h"

#include <Arduino.h>
#include <Wire.h>
#include <SoftwareSerial.h>

OfflineBroadcast broadcaster;

void _delay(float seconds) {
  long endTime = millis() + seconds * 1000;
  while(millis() < endTime) _loop();
}

void setup() {
  // Below is the issue
  pinMode(13,OUTPUT);
  pinMode(13,INPUT);
  Serial.begin(115200);
  while(1) {
      digitalWrite(13,1); // We write out and don't set the pin back to input later
      _delay(1);
      if(digitalRead(13) == 1.000000){ // Here we are reading an output pin
        broadcaster.broadcast(String("day"));

      }else{
        broadcaster.broadcast(String("night"));

      }
      digitalWrite(13,0);
      _delay(1);
      if(digitalRead(13) == 1.000000){
        broadcaster.broadcast(String("day"));

      }else{
        broadcaster.broadcast(String("night"));

      }

      _loop();
  }

}

void _loop() {
  broadcaster.loop();
}

void loop() {
  _loop();
}

So the output pin will always return the same value. All I can recommend doing is removing the if then checks, unless you need them for some later code. Something like this should work (I may have got the High / Low states backward):

image