Go Down

Topic: Sorting by light (Read 2250 times) previous topic - next topic

Nagaro

Hi,

After a few weeks, i finally figured out how to connect the components, and how to write the code for the arduino. I used a Arduino nano, a 330 ohm resistance and the ws2812b. No need of additionnal power supply, the usb linked to the arduino is enough.
I need to find how to share pictures.

But i'm still missing the connexion between Excel VBA and the Arduino.
I want that my excel sheet is sending an information to the arduino (which led to light on) and this led will turn on. But what i've found on the internet was only for Visual Basic, and not working with VBA in Excel.

Would somebody have a piece of code i could re-use for that ?

Thank you for your support

Nagaro

Hi again;

I found actually something today on the Internet, which is the cod written by David M. Hitchner called "ModComm".
It should actually work for my purpose.
Here is the link for those interested : http://www.thescarms.com/vbasic/CommIO.aspx

Nevertheless, when i use it, i declare the variable strdata as string and set it as "5" (if i want for example to switch on the led number 5), and then in the arduino i write the following code :


#include <FastLED.h>
#define LED_PIN     7
#define NUM_LEDS    60
CRGB leds[NUM_LEDS];

void setup() {
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  Serial.begin(9600);
}

void loop() {

  int value;
  if(Serial.available())
  {
    delay(50);
    while(Serial.available() >0)
    {
      value=Serial.read();
      leds[value] = CRGB(255, 0, 0);
      FastLED.show();
      delay(100);
     
    }
  }

}


So the variable "value" in the arduino should be the same that the variable "strdata" in VBA (so 5).
But no led is switching on, when i thought led number 5 would work.

Do you know where is my mistake ?

Thank you,

TolpuddleSartre

Code: [Select]
value=Serial.read();
      leds[value] = CRGB(255, 0, 0);
Are you sending 5, or '5'?

PaulRB

It's hard to say because you did not post the VBA code. (And by the way you did not post the Arduino code correctly), but I would say that "5" is not the same value as 5.

Grumpy_Mike

#19
May 02, 2018, 04:41 pm Last Edit: May 02, 2018, 05:18 pm by Grumpy_Mike
Just use
Code: [Select]
leds[value & 0xF] = CRGB(255, 0, 0);
That will convert a "5" to a 5

TolpuddleSartre

Just use
Code: [Select]
leds[value & 0x30] = CRGB(255, 0, 0);
That will convert a "5" to a 5
wouldn't '-' rather than '&' be a more effective operator?

Grumpy_Mike

Quote
wouldn't '-' rather than '&' be a more effective operator?
No.
They are both equally effective in what they do.

A minus sign is a more simple minded approach to bit manipulation which at the end of the day is what you want to do.

In fact I made an error in that last post, corrected now.

Kerwin50

Using the getting started with Adurino book where you can turn on an led by holding a push button I was successful. But then I tried to do 2 less an 2 pins but was unsuccessful.  I named leda and ledb.  I also did the same thing for the push buttons.
I used the const int an pinMode commands.
Does any have a code that I could look at to see where I went wrong.

TolpuddleSartre

Quote
Does any have a code that I could look at to see where I went wrong.
That would be your code, wouldn't it?

Nagaro

#24
May 02, 2018, 10:02 pm Last Edit: May 02, 2018, 10:05 pm by Nagaro
Thank you for the help.

The principle seems to work, neverheless i realized 2 things (for the moment) :
- the led switching on doesn't correspond to the one i want (for example, if i enter 8, it's the led 19 which switch on)
- if i change the value in the macro excel, and run it again, nothing will change. It will only change if i re-upload the code into the arduino.

Do you know to what it is due ?

Below the VBA code i'm using :

Code: [Select]


Sub TestModComm()

    
    Dim intPortID As Integer ' Ex. 1, 2, 3, 4 for COM1 - COM4
    Dim lngStatus As Long
    Dim strError  As String
    Dim strData   As String

    'Settings
    intPortID = 5 'COM number
    strData = "8" 'text to write in the arduino

    ' Initialize Communications
    lngStatus = CommOpen(intPortID, "COM" & CStr(intPortID), _
        "baud=9600 parity=N data=8 stop=1")
    
    If lngStatus <> 0 Then
    ' Handle error.
        lngStatus = CommGetError(strError)
    MsgBox "COM Error: " & strError
    End If
    

    ' Set modem control lines.
    lngStatus = CommSetLine(intPortID, LINE_RTS, True)
    lngStatus = CommSetLine(intPortID, LINE_DTR, True)

    ' Write data to serial port.

    
    lngSize = Len(strData)
    lngStatus = CommWrite(intPortID, strData)
    If lngStatus <> lngSize Then
    ' Handle error.
    End If



    ' Read maximum of 64 bytes from serial port.
    lngStatus = CommRead(intPortID, strData, 64)
    If lngStatus > 0 Then
        ' Process data.
    ElseIf lngStatus < 0 Then
        ' Handle error.
    End If

    ' Reset modem control lines.
    lngStatus = CommSetLine(intPortID, LINE_RTS, False)
    lngStatus = CommSetLine(intPortID, LINE_DTR, False)



    ' Close communications.
    Call CommClose(intPortID)


End Sub





And attached the code i'm using, found on the internet, called ModComm.

We are almost reaching our objective, that's great :-)


Kerwin50

That would be your code, wouldn't it?
I don't have anyway to post my code as of yet. The only way I can get on the net is by this cheap phone I have.
I was just wanting to look at someone else's code

Nagaro

Hi,

Did somebody have the chance to have a look at my problem ?
As explained, the wrong led is switching on, and only one time (i can't change it afterwards again).

When this will be solved, it will be great.

Thank you for your help,

Grumpy_Mike

Quote
Did somebody have the chance to have a look at my problem ?
You didn't post your lasted Arduino code. All I can see is the first code which as we pointed out had issues.

Nagaro

#28
May 04, 2018, 11:36 pm Last Edit: May 04, 2018, 11:40 pm by Nagaro
Here is the last Arduino code i'm using (i only updated the one line as it was proposed) :

Code: [Select]


#include <FastLED.h>
#define LED_PIN     7
#define NUM_LEDS    60
CRGB leds[NUM_LEDS];

void setup() {
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  Serial.begin(9600);
}

void loop() {

  int value;
  if(Serial.available())
  {
    delay(50);
    while(Serial.available() >0)
    {
      value=Serial.read();
      leds[value & 0xF] = CRGB(255, 0, 0);
      FastLED.show();
      delay(100);
     
    }
  }

}



Otherwise, in my previous post, there is also the full VBA i'm using to send the « value » information from Excel to the Arduino

Grumpy_Mike

I am not sure you understood what we were saying. That conversion only accepts single bytes between zero and 9. Also you are only ever reading a single byte with the Serial.read() call.

Note you can not get LED 19 lighting up from that code because the LED number lit can only ever be from zero to 15, no matter what byte you send it. That is what the " & 0xF does, it zeros all but the first four bits in the number so the number can only ever be 0 to 15.

If you think LED number 19 is lighting up then things are not what you think they are.

One explanation could be that you have three errors.
1) You are not starting counting from zero on your LED strip but from one.
AND
2)  You are sending 6 and not 8
AND
3) You have a WS2801 controlled strip and not a WS2812 strip.

Go Up