Arduino Not Receiving Serial Data When Shield Attached - c

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.

Related

Sharp Memory display via SPI from Arduino Nano RP2040 C++

I have an Adafruit Sharp Memory display connected via SPI to Arduino Nano Connect RP2040. I don't use Arduino IDE nor libraries. I write the code in C++ like I would write for RP2040 on Pi Pico, so I'm using Pico/RP2040 libraries. I'm building it and then just copying the .uf2 file to Arduino.
I'm trying to control the display but without success and I have no idea what I'm doing wrong. Here is my code, where I'm sending a command which should clear the display but it does not:
#include <stdio.h>
#include <stdlib.h>
#include "pico/stdlib.h"
#include "hardware/spi.h"
#include "hardware/gpio.h"
// ---SPI
// Pins for Arduino
#define SCK_PIN 13 // SCLK / SCK
#define MOSI_PIN 11 // MOSI / COPI
#define SS_PIN 10 // SS / CS
// ---Sharp display
#define WIDTH 144
#define HEIGHT 168
int main() {
stdio_init_all();
printf("START \n");
bool vcom_bool_{false};
spi_init(spi0, 2000000);
spi_set_format( spi0, 8, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST);
gpio_set_function(MOSI_PIN, GPIO_FUNC_SPI);
gpio_set_function(SCK_PIN, GPIO_FUNC_SPI);
gpio_init(SS_PIN);
gpio_set_dir(SS_PIN, GPIO_OUT);
gpio_put(SS_PIN, 0); // this display is low on inactive
while(true)
{
printf("VCOM = %b \n", vcom_bool_);
gpio_put(SS_PIN, 1);
// Clear the display
uint8_t buf[2];
if(vcom_bool_)
{
buf[0] = 0b01100000;
vcom_bool_ = false;
}
else
{
buf[0] = 0b00100000;
vcom_bool_ = true;
}
buf[1] = 0b00000000;
spi_write_blocking(spi0, buf, 2);
gpio_put(SS_PIN, 0);
sleep_ms(10);
sleep_ms(500);
}
}
Display and wiring are ok, because when I run example code from Adafruit (it is using Arduino's libraries) it works.
I follow this documentation from Sharp: https://www.sharpsde.com/fileadmin/products/Displays/2016_SDE_App_Note_for_Memory_LCD_programming_V1.3.pdf
I also reviewed the code from Adafruit and the SPI communication seems to be done in the same way.
What am I doing wrong?
Is there some issue with using Pico libraries for Arduino Nano RP2040? I did I2C communication in the same way (Pico/RP2040 libraries, .uf2 copied to Arduino Nano RP2040) and it worked.

Neo 6M GPS module with Arduino Nano and OLED Display, "SSD1306 allocation failed" Error

I am getting started with GPS modules, for my project I am using the NEO 6M GPS module with a Ceramic Antenna.
So, earlier I tried to connect the GPS module with an Arduino Nano and was able to successfully display the GPS NEMA sentences output on the Arduino IDE Serial Monitor, although the GPS module was not able to connect to any satellites.
Now, I am trying to add an SSD1306 OLED module to this project. So, that the latitude and the longitude values can be fetched from the GPS module and can be displayed on the OLED Screen whenever the GPS updates its position.
The code which I have written so far was compiled properly in the Arduino IDE, but the OLED module failed to display the expected information.
I kept on getting the "SSD1306 allocation failed" error on the Serial Monitor.
I have already verified the I2C Address for my OLED display module which is 0x3C instead of 0x3D.
I have also tested the OLED display with a separate code and it is working fine.
Then also I am not able to figure out what's wrong with this code that it is leading to these unexpected results.
The code which I am using for this project is attached below.
So, if a fellow programmer from the community can help me solve this issue then that would be really appreciated.
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#define OLED_WIDTH 128
#define OLED_HEIGHT 64
#define OLED_RESET -1
#define OLED_ADDR 0x3C
Adafruit_SSD1306 oled(OLED_WIDTH, OLED_HEIGHT, &Wire, OLED_RESET);
SoftwareSerial serial_connect(4, 3); //Rx:pin(4) Tx:pin(3)
TinyGPSPlus gps;
void setup()
{
Serial.begin(9600);
serial_connect.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!oled.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR))
{
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
Serial.println("GPS start");
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(SSD1306_WHITE);
oled.setCursor(0, 0);
oled.print("Detecting GPS");
oled.setCursor(0, 10);
oled.print("Coordinates");
oled.display();
delay(5000); //5 seconds delay
}
void loop()
{
while(serial_connect.available())
{
gps.encode(serial_connect.read());
}
if(gps.location.isUpdated())
{
Serial.println("Satellite Count:");
Serial.println(gps.satellites.value());
Serial.println("Latitude:");
Serial.println(gps.location.lat(),6);
Serial.println("Longitude:");
Serial.println(gps.location.lng(),6);
Serial.println("Speed MPH:");
Serial.println(gps.speed.mph());
Serial.println("Altitude Feet:");
Serial.println(gps.altitude.feet());
Serial.println(" ");
oled.clearDisplay();
oled.setCursor(0, 0);
oled.print("Lati=");
oled.setCursor(0, 10);
oled.print(gps.location.lat(),6);
oled.setCursor(10, 0);
oled.print("Long=");
oled.setCursor(10, 10);
oled.print(gps.location.lng(),6);
oled.display();
delay(500);
}
}
It's a bit late now, but letsee... That's an interesting issue.
Your code follows the examples provided by Adafruit, w.r.t. SSD1306. When you define pins 3 and 4 for the GPS's UART, which pins are you using? D3 and D4? Which pins are you using for the I2C OLED display? You have to make sure that your OLED display has the SDA and the SCL connected to A4 and A5, respectively.
Have you done anything different when the display worked, e.g. use a different platform or MCU?

Can't send data to ESP8266 via arduino

I have an ESP8266-01 and I want to send sensor data from Arduino to ESP8266 via serial communication but the data I receive is not correct, I tried changing baud rates but no luck, here is the code
Code for ESP8266:
#include <SoftwareSerial.h>
void setup() {
// put your setup code here, to run once:
Serial.begin(38400);
}
void loop() {
// put your main code here, to run repeatedly:
while(Serial.available()) {
Serial.println("yes");
Serial.println(Serial.read());
}
delay(5);
}
Code for Arduino
#include <SoftwareSerial.h>
SoftwareSerial esp(1, 0);
String str;
void setup(){
Serial.begin(38400);
esp.begin(38400);
delay(2000);
}
void loop() {
// put your main code here, to run repeatedly:
str = String("Hi there");
esp.println(str);
esp.println("hi there");
delay(1000);
}
Here is the serial monitor:
Serial monitor showing numbers instead of "hi there"
The wiring connections are correct.
if you are using simple wires for the connection you might need to reduce the baud rate at 9600 or lower.
For higher speed you will need coaxial cables to get a decent communication distance. Moreover I am sure that you have checked the electrical connection obvously.
On the programming side, Serial.read() returns the first byte of incoming serial data available (or -1 if no data is available). Data type: int. This is why you are getting a single number instead of your string.
I would suggest to use Serial.readString() instead.
Please see the proper documentation here:
https://www.arduino.cc/reference/en/language/functions/communication/serial/
All the best

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 */
}
}
}

Arduino2max digital pin communication to max using an Arduino mega 2560

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

Resources