Arduino2max digital pin communication to max using an Arduino mega 2560 - c

Im working on connecting an Arduino Mega 2560 into max msp, I have adapted the Arduino2max arduino code and max patch.
I have adapted the max patch and succeeded with all 16 analog inputs from arduino into max but cannot get any digital pins over number 13 into max msp. I was wondering if anyone had had any sucsess with this?
Any help and comments would be greatly appreciated!
Many thanks
Joe
here is the arduino code adapted from Arduino2max v.5 which can be found here http://www.arduino.cc/playground/Interfacing/MaxMSP
int x = 0;
int ledpin = 13;
void setup ()
{
// 115200 is the default Arduino Bluetooth speed
Serial.begin(115200);
///startup blink
digitalWrite(13,HIGH);
delay(600);
digitalWrite(13,LOW);
pinMode(13,INPUT);
}
void loop()
{
// Check serial buffer for characters
if (Serial.available() > 0){
if (1){ //Serial.read() == 'r') { // If an 'r' is received then read the pins
// Read and send analog pins 0-15
for (int pin= 0; pin<=15; pin++)
{
x = analogRead(pin);
sendValue (x);
}
// Read and send digital pins 2-53
for (int pin= 2; pin<=53; pin++)
{
x = digitalRead(pin);
sendValue (x);
}
// Send a carriage return to mark end of pin data.
Serial.println();
// add a delay to prevent crashing/overloading of the serial port
delay (5);
}
}
}
// function to send the pin value followed by a "space".
void sendValue (int x){
Serial.print(x);
Serial.print(32, BYTE);
}
Thanks again!

I suggest you to use the OSC Protocol to communicate between the Arduino Mega and Max.
I use the library ardosc. There is no documentation on it but its not really hard to use it and it is a good library.
If you cannot use it do not hesitate to ask me some explanations

Related

Forwarding / passthrough UART from one to another port

I'm pretty new to programming in C, but getting used to registers and the way communication in C works. Since UART using the official Arduino read() / write() creates a high delay in passing commands through, I tried to translate this Arduino sketch into pure C. (and because I've time to play around, haha)
My ebike's controller and display are communicating using UART. I tried to read the commands and react to speed changes or brake signals, but first of all I need to get rid of this huge delay in updating the display.
I'm using an Robodyn Mega 2560 PRO (Embed), which has 4x hardware serial ports. In the first step I tried to read what's coming in and forward it to the other port. Shouldn't be to hard to implement in C, right?
void setup() {
Serial1.begin(1200); // BBSHD controller #RX18 TX19
Serial2.begin(1200); // Display DP-C18 #TX16 RX17
}
void loop() {
if (Serial2.available()) {
Serial1.write(Serial2.read());
}
if (Serial1.available()) {
Serial2.write(Serial1.read());
}
}
That's what I programmed in Atmel Studio 7 so far. I used the C example from ATmega640/1280/1281/2560/2561 - Complete Datasheet (Search for USART_Receive and USART_Transmit) and this guide Simple Serial Communications With AVR libc
Currently I can compile and flash it the 2560, but the communication is not being forwarded. I don't know what the default settings are, that Arduino with Serial uses. Which mode, number of stop bits, .. Is there anything obvious I'm missing?
#define F_CPU 16000000UL
#define BAUD 1200
#include <avr/io.h>
#include <stdio.h>
#include <util/setbaud.h>
void uart_init(void) {
/* Serial1 controller */
UBRR1H = UBRRH_VALUE;
UBRR1L = UBRRL_VALUE;
/* Serial2 display */
UBRR2H = UBRRH_VALUE;
UBRR2L = UBRRL_VALUE;
/* 8-bit data, 1stop bit*/
UCSR1C = (1<<UCSZ11) | (1<<UCSZ10);
UCSR2C = (1<<UCSZ21) | (1<<UCSZ20);
/* Enable RX and TX */
UCSR1B = (1<<RXEN1) | (1<<TXEN1);
UCSR2B = (1<<RXEN2) | (1<<TXEN2);
}
int main(void) {
uart_init();
while(1) {
if (!(UCSR1A & (1<<RXC1))) { /* Only if data is received */
while (!(UCSR1A & (1<<UDRE1))); /* Wait for empty transmit buffer */
UDR2 = UDR1; /* Put data into buffer, sends the data */
}
if (!(UCSR2A & (1<<RXC2))) { /* Only if data is received */
while (!(UCSR2A & (1<<UDRE2))); /* Wait for empty transmit buffer */
UDR1 = UDR2; /* Put data into buffer, sends the data */
}
}
}

How to read more than one ADC input and print in C for STM32F4

I'm using the MCU STM32F4 and I want to read the ADC inputs using Hal Library and show these on the Terminal. My ADCs inputs are running in continuous convertion mode and the convertion is called by tick of a Timer.
ADC_HandleTypeDef hadc1;
TIM_HandleTypeDef htim2;
char buffer[10];
uint32_t adc1, adc2, adc3;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim)
{
if(htim->Instance == TIM2)
{
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1,300);
adc1 = HAL_ADC_GetValue(&hadc1);
sprintf(buffer,"A1: %d ",(int)adc1);
CDC_Transmit_FS((uint8_t*)buffer,9);
HAL_ADC_PollForConversion(&hadc1,300);
adc2 = HAL_ADC_GetValue(&hadc1);
sprintf(buffer,"A2: %d ",(int)adc2);
CDC_Transmit_FS((uint8_t*)buffer,5);
HAL_ADC_Stop(&hadc1);
if(adc1 > 1000)
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_14,1);
}
else
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_14,0);
}
}
}
This code above works if I have a single channel to monitorate. But, if I use more than one channel, the output show only the channel 2 values, like the image shown below.
I dont know what is happening. Do can you help me?

Pulse generation and readout on arduino

Currently I'm working on a project where I have to read out pulses from a Arduino and check if the result is High or Low.
I had to write my own code to generate the high/low output from the Arduino:
//Pulse Generator Arduino Code
int potPin = 2; // select the input pin for the knob
int outputPin = 13; // select the pin for the output
float val = 0; // variable to store the value coming from the sensor
void setup() {
pinMode(outputPin, OUTPUT); // declare the outputPin as an OUTPUT
Serial.begin(9600);
}
void loop() {
val = analogRead(potPin); // read the value from the k
val = val/1024;
digitalWrite(outputPin, HIGH); // sets the output HIGH
delay(val*1000);
digitalWrite(outputPin, LOW); // sets the output LOW
delay(val*1000);
}
It uses a knob to change the delay between the pulses.
Im currently trying to read the high/low data with another Arduino (Lets call this one the "count Arduino") by simply connecting the 2 with the a cable from the "outputPin" to a port on the count Arduino.
I'm using digitalRead to read the port without any delay.
//Count Arduino Code
int sensorPin = 22;
int sensorState = 0;
void setup() {
pinMode(sensorPin, INPUT);
Serial.begin(9600);
}
void loop(){
sensorState = digitalRead(sensorPin);
Serial.println(sensorState);
}
First it tried with a pulse every 1 second but the result was a spam of a ton of lows and highs. Always 3 Lows and 3 highs and repeating. It wasn’t even close to one every 1 second but more like 1 every 1 millisecond.
I cant figure out what i'm doing wrong. Is it timing issue or is there a better way to detect these changes?
a spam of a ton of lows and highs
... happens if the GND of the two Arduinos are not connected.
Also, your reading arduino prints at every loop cycle, which were a few microseconds only, if the Serial buffer would not overflow.
Better printout changes only, or use a led to show what's happening.
void loop(){
static bool oldState;
bool sensorState = digitalRead(sensorPin);
if (sensorState != oldState) {
Serial.println(sensorState);
oldState = sensorState;
}
}

Arduino Not Receiving Serial Data When Shield Attached

I have nexus duino 3WD and tried to do serial comm to move the robot. I have to remove shield to upload sketch code. This is my sketch:
#include <fuzzy_table.h>
#include <PID_Beta6.h>
#include <PinChangeInt.h>
#include <PinChangeIntConfig.h>
#include <MotorWheel.h>
#include <Omni3WD.h>
#include <EEPROM.h>
#define _NAMIKI_MOTOR //for Namiki 22CL-103501PG80:1
/*******************************************/
int incoming = 0;
int speed = 100;
// Motors
irqISR(irq1,isr1);
MotorWheel wheel1(9,8,6,7,&irq1); // Pin9:PWM, Pin8:DIR, Pin6:PhaseA, Pin7:PhaseB
irqISR(irq2,isr2);
MotorWheel wheel2(10,11,14,15,&irq2); // Pin10:PWM, Pin11:DIR, Pin14:PhaseA, Pin15:PhaseB
irqISR(irq3,isr3);
MotorWheel wheel3(3,2,4,5,&irq3); // Pin3:PWM, Pin2:DIR, Pin4:PhaseA, Pin5:PhaseB
Omni3WD Omni(&wheel1,&wheel2,&wheel3);
/******************************************/
void setup() {
Serial.begin(38400);
Serial.println("setup");
TCCR1B=TCCR1B&0xf8|0x01; // Pin9,Pin10 PWM 31250Hz
TCCR2B=TCCR2B&0xf8|0x01; // Pin3,Pin11 PWM 31250Hz
Omni.PIDEnable(0.26,0.02,0,10);
}
/****************************************/
void loop() {
if(Serial.available() > 0) {
incoming = Serial.read();
Serial.print(incoming);
if(incoming==0) {
Omni.setCarStop();
} else if(incoming==1) {
Omni.setCarAdvance(speed);
} else if(incoming==2) {
Omni.setCarRotateLeft(speed);
} else if(incoming==3) {
Omni.setCarRotateRight(speed);
}
}
Omni.PIDRegulate();
}
I tried to send data both from Serial Monitor and also PC app with USB cable. When shield is not attached I can send data and receive feedback from arduino. But when shield is attached it seem arduino not receiving data, the RX led flash but not the TX led, got 'setup' text output so arduino can send data to PC. Other thing is I can only use 38400 baud rate, otherwise the output will be garbagish.
I'm not sure if I do wrong in the code? Probably device power issues?
If you change the jumpers from RS-485 to Bluetooth like what it says here http://www.dfrobot.com/wiki/index.php/Arduino_I/O_Expansion_Shield_(SKU:_DFR0014)
then everything will work fine. uploading to the board without removing the shield might still be an issue, and sonars probably will not work, but at least you'll be able to read serial through the board.

Reading Virtual Serial Port with MicroC for 8051

I have a problem , please help me.
for about a project homework ı need read from virtual serial port with microC and send this info to AT89C52 microconttoller..
This is my source code:
int uart_rd;
void main() {
P1=0X00;
UART1_Init(9600);
delay_ms(100);
while(1)
{
if(UART1_Data_Ready()){
uart_rd=UART1_read();
if(uart_rd=='1')
{P1=0X01; delay_ms(1500); P1=0X00; }
if(uart_rd=='2')
{P1=0X02; delay_ms(1500); P1=0X00; }
}
}
}
BUT I cant get info from the port. Where is the mistake.Please help me...
You are defining your UART receive variable (uart_rd) as an int, which is a 2 byte variable. I would expect UART1_read() to return a single byte (char).
I am not familiar with your particular setup or debugging/troubleshooting options, but you might try writing some code to assist in debugging your issue. The following example may be useful. It does assume that LEDs are connected to both port 1 and port 2, so some adjustment may be necessary.
char uart_rd;
void main()
{
UART1_Init(9600); // Initialize UART at 9600 bps
delay_ms(100); // Wait for UART to stabilize
while(1)
{
if(UART1_Data_Ready())
{
P2 = 0xFF; // Turn ON PORT2 LEDs upon data ready
uart_rd = UART1_read(); // Receive data
P1 = uart_rd; // Display data on port 1 LEDs
UART1_write(uart_rd); // Transmit same data back
delay_ms(1500); // Brief delay
P1 = 0x00; // Turn OFF port 1 LEDs
P2 = 0x00; // Turn OFF port 2 LEDs
}
}
}

Resources