Pages: [1]   Go Down
Author Topic: MEGA 2560 Help wanted with coding  (Read 813 times)
0 Members and 1 Guest are viewing this topic.
Lake District, UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 72
Electronics needs smoke to work, if it escapes its broken
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am currently working my way through the examples but cant seem to find a particular way of sending data from a PC to the Adrunio which will switch on 8 digital outputs.  I would like to be able to send instruction as a Byte from the PC with some sort of identification attached which would be stripped from the data before being transmitted.  Ideally I want the unit to serve my project using multiple comm send and receives for data aquisition.
reading through some examples i have found that i can read the analogue inputs, send digital outputs and perhaps have DMX output aswell.
the DMX would be used to send instructions to an 8CH led dimmer circuit (Hendrik's DMX Dimmer) to provide analogue outputs.
any help in this direction would be greatfully appreciated.

Also i keep reading that i cant use pins 4 & 5 as they are dedicated to Serial Comms.  Im using the Mega is this still true as i have more than enough pin and I have other magic pins labled 0 & 1 for serial comms.
"can i use thes instead until i find the other ones lol"
incidently the pin map starts at pin 1 not 0 WHY!, the functions dont even coincide as pin 1 is a PWM pin?
very confused, please advise.
and no not the famous Microsoft suggestion - Put it back in the box and return it-your to stupid to own a computer.
regards
Jason
Logged

Offline Offline
Edison Member
*
Karma: 5
Posts: 1730
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There are multiple hardware serial ports on the mega, not just 4,5 but they can be used just fine
you can use either the regular usb connection and the first serial to receive the byte, or a usb to serial converter going to any of the other hardware serial ports that are niceky labeled on the board
Code for something

Serial.begin(9600); // default serial avaiable at usb and pins 0,1
Serial1.begin(9600);// hardware serial on 18,19

To read
serial.read() or serial1.read()
etc and works on all them
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 644
Posts: 50524
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I am currently working my way through the examples but cant seem to find a particular way of sending data from a PC to the Adrunio which will switch on 8 digital outputs.
That's because you get to define what to send and what to do in reaction to that value/data/string appearing.

Quote
I would like to be able to send instruction as a Byte from the PC with some sort of identification attached which would be stripped from the data before being transmitted.
If you are going to strip off the ID, why send it?

Quote
Ideally I want the unit to serve my project using multiple comm send and receives for data aquisition.
You need to elaborate on this. What is "my project"? Does "using multiple comm send and receives" mean more than one connection to the PC, or does it mean doing the send/receive operation more than once?

How does sending a byte from the PC to the Arduino result in data acquisition?

Quote
Also i keep reading that i cant use pins 4 & 5 as they are dedicated to Serial Comms.
Take whatever you are reading this crap in to the nearest shredder.

Pins 0 and 1 are used by Serial. Other pins, clearly labeled on the board, are used by Serial1, Serial2, and Serial3. If you don't need those pins for those purposes, you can use the pins for other things.

Of course, if you want to communicate with the PC you HAVE to leave pins 0 and 1 alone.

Quote
Im using the Mega is this still true
I guess it is. You told us you are.

Quote
incidently the pin map starts at pin 1 not 0 WHY!
What pin map? If it's in the same material that says you can't use pins 4 and 5, it's rubbish.

Quote
the functions dont even coincide as pin 1 is a PWM pin?
What functions? Pin 1 is NOT a PWM pin.

Quote
and no not the famous Microsoft suggestion
Well, you do need to be smart enough to figure out what's rubbish and what's true. You're not doing too good, so far.

But, you are trying, and that's good.
Logged

Offline Offline
Edison Member
*
Karma: 5
Posts: 1730
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Im wondering why you would need an id at all, usually serial is between two devices and so an id is useless because you had to physically connect them,
unless you have multiple that are chained and they pass the info down the line until it matches the id of the device,
if that's what your looking into why not look at i2c or some other actual network protocol
Logged

Lake District, UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 72
Electronics needs smoke to work, if it escapes its broken
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,
thanks for replying.
I want to use my adruino mega to interface with a robot of sorts.
I have a VB.net application already which will use the usb link to the arduino to send and receive data.
I need to send 8 switch values (digital output), read 8 analogue instrument values and i would like to use DMX to produce 8 x analogue outputs (I only need the DMX bridge to transmit from the adruino the other hardware is already in place.
if i use the USB port as communication for the pc to the arduino to transmit and receive data, i will need a method of telling the arduino to produce a DMX command or drive a digital output.  I admit receiving the analogue inputs will be quite straight forward.
The arduino co-founders blog on the DMX code is flagging up an error- one of the statements is not declared (digital pin, somthing or other) if any one has knowledge of any issues or a working sketch it would be much appreciated.
I had a good laugh at Frasier Kranes comments (PaulS) you are a star - i guess i deserved it.

I discovered another member asked a similar question about pin mapping, when your confronted with a new piece of technology it hard to get your head around what the manufacturers are trying to convey if they dont actually make a statement anywhere which spells it out. maybees im just being perdantic.

Anyways - is there a way i can send a character/or data from the PC which will be transposed into a block of digital outputs ie. a byte becomes the on/off condition for a block of switches.  As im going to be sending digital output commands, DMX and reading analogue inputs i would like to get the number of communication commands down to a minimum.  Bit banging would use a huge number of commands per second for the USB port to handle.

also how could i block read the analogue inputs to receive a data stream instead of individually receiving them as in the example.

Best regards
Jason
Logged

Lake District, UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 72
Electronics needs smoke to work, if it escapes its broken
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi evryone,
still not found help

Can you please advise how i might send a byte of data via the usb to the mega
(received from a pc)
which will drive 8 digital outputs.

As I also intend sending another stream of data, is it possible to add an identifier to the data so that the mega knows where to direct the data ie write digital out data to a variable ready to be used.

I plan to use the same port to transmit serial data for dmx output to the mega hence the requirement for an identifier.

Regards
Jason
Logged

Offline Offline
Edison Member
*
Karma: 5
Posts: 1730
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How to send a byte from the computer is all up to the computer program you use, get a usb to serial or the port on the arduino and just send your byte, then interpret it on the arduino, ignoring any non important characters like line feed or whitespaces,
then use some bitwise math to separate the byte into your outputs
the way I have done an identifer is sending a 3 byte package with the pin and state included, like 131 or 130 to turn 13 on(1) or off(0)
take the first two character on the arduino side and convert from their ascii values to decimal, combing the two character into one real number
to do that you

Pinnumber = Incomingbyte1 - 48
Pinnumber = Pinnumber * 10
Incomingbyte2 = Incomingbyte2 - 48;
Pinnumber = Pinnumber + Incomingbyte2;

Remember to. Use a pin lik 3, send 03

Then. To turn on or off
Pinstatus = incomingbyte3 - 48;
PinMode(Pinnumber, output);
digitalWrite(pinnumber,Pinstatus);

This assumes you received the 3 bytes and stored them already

I used this technique and made a library for full control of my mega over serial, digtital and analog input, digital and pwm output,
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 644
Posts: 50524
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I discovered another member asked a similar question about pin mapping, when your confronted with a new piece of technology it hard to get your head around what the manufacturers are trying to convey if they dont actually make a statement anywhere which spells it out.
Which manufacturer are you referring to? ATMEL, who makes the chip that the Arduino uses, and clearly spells out what pin on their chip does what, or the Arduino folks who print right on the board which pins are for what purpose?

The discussion in the other thread is about trying to understand which ATMEL pin corresponds to which Arduino pin. The schematics tell you that, if it is important. If it is important, you probably know how to read a schematic.

Logged

Pages: [1]   Go Up
Jump to: