Serial port not sending data - c

Yesterday, I started with some serial port communication. Today, I tried to open exactly the same sketch I used yesterday. It worked, but a few minutes later when I uploaded it again, it doesn't work at all.
Here's the code:
#include "Blink_main.h"
int pin = 1;
void pulsePin(int abc, int length){
digitalWrite(abc, true);
delay(length);
digitalWrite(abc, false);
}
void setup() {
pinMode(pin, OUTPUT);
Serial.begin(9600);
Serial.println("Hi serial!");
pulsePin(pin, 1000);
}
void loop() {
if(Serial.available() > 0){
Serial.println(Serial.read());
pulsePin(pin, 1000);
}
}
When opening the application, I don't see the "Hi serial!" message, When sending something, I don't get a message back and I don't see the LED flashing. Why is this happening?

You are using pin 1 to blink the led, move the led to another pin since it is used as the serial TX
Digital Pins 0-1/Serial In/Out - TX/RX - These pins cannot be used for digital i/o (digitalRead and digitalWrite) if you are also using serial communication (e.g. Serial.begin).

It look like the problem come from the Arduino board.
Firstly, try to upload a Serial example sketch.
If it doesn't work, try to reinstall the driver or change the COM port used.
If the problem is still happening, it's probably the ATMEGA8U2 or ATMEGA16U2 which is dead. You can test with an external USB to UART convert directly on pins 0 and 1

Related

Arduino Ultrasonic Distance Sensor with Passive Buzzer to achieve different tones

I have an Ultrasonic Distance Sensor with Passive Buzzer. The passive buzzer was set up with different tones. The buzzer will keep playing until the Ultrasonic Distance Sensor detect any obstruction items. However, the Arduino couldn't compile the code. it displays the error:
exit status 1
Error compiling for board Arduino/Genuino Uno.
Here is the full error message:
Arduino: 1.8.9 (Windows 10), Board: "Arduino/Genuino Uno"
Tone.cpp.o (symbol from plugin): In function `timer0_pin_port':
(.text+0x0): multiple definition of `__vector_7'
libraries\NewPing\NewPing.cpp.o (symbol from plugin):(.text+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board Arduino/Genuino Uno.
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
I could locate the function timer0_pin_port in Tone.cpp.o. But I couldn't find the same function in NewPing.cpp.o.
Because of the space limit, I counldn't post the NewPing.cpp.o here. You can download the NewPing.cpp.o here: https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home
Tone.cpp.o is the original document in the library.
#include <NewPing.h>
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 10 // Maximum distance we want to ping for (in centimeters).
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void setup() {
Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
pinMode(2,OUTPUT);
}
void loop() {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
Serial.print("Ping: ");
Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
if (sonar.ping_cm() == 0)
tone(2,4000);
else
tone(2,0);
}
Expected: The Buzzer will stop playing when the DIstance sensor detects any items. You have to use tone method to support different Tones. Or any similar functions that can support different tones.
From what I understand that the Tone and the NewPing library have a conflicting use of the same interrupt __vector_7. NewPing is known to have conflicting issues, I would suggest you use the original ping in Arduino. Here's a comprehensive example for it.
If you're certain that you're not using the ping_timer() method then in the NewPing.h file make TIMER_ENABLED to false.
Here's a link which talks about Multiple Definition of "__vector_7" Error further.
Here's the thread of a similar problem on the arduino forum.

STM32 USB VCP (Virtual Com Port)

I generated a code for "stm32f103c8t6" with CubeMX for USB VCP, when I add "CDC_Transmit_FS" command to send data, the port isn't recognized by windows10!
what should I do? Here is the code which is compiled without error:
#include "stm32f1xx_hal.h"
#include "usb_device.h"
#include "usbd_cdc_if.h"
int main(void)
{
uint8_t Text[] = "Hello\r\n";
while (1)
{
CDC_Transmit_FS(Text,6); /*when commented the port is recognized*/
HAL_Delay(1000);
}
}
There are three things you need to check in my experience:
startup_stm32f405xx.s --> Increase the Heap size. I use heap size 800 and stack size 800 as well.
usbd_cdc_if.c --> APP_RX_DATA_SIZE 64 and APP_TX_DATA_SIZE 64
usbd_cdc_if.c --> add below code to the CDC_Control_FS() function
Code:
case CDC_SET_LINE_CODING:
tempbuf[0]=pbuf[0];
tempbuf[1]=pbuf[1];
tempbuf[2]=pbuf[2];
tempbuf[3]=pbuf[3];
tempbuf[4]=pbuf[4];
tempbuf[5]=pbuf[5];
tempbuf[6]=pbuf[6];
break;
case CDC_GET_LINE_CODING:
pbuf[0]=tempbuf[0];
pbuf[1]=tempbuf[1];
pbuf[2]=tempbuf[2];
pbuf[3]=tempbuf[3];
pbuf[4]=tempbuf[4];
pbuf[5]=tempbuf[5];
pbuf[6]=tempbuf[6];
break;
and define the uint8_t tempbuf[7]; in the user private_variables section.
Without the increased heap size, Windows does not react at all.
Without the point 3, Windows will send the baud rate information and then read the baud rate, expecting to get back the same values. Since you do not return any values, the virtual com port remains as driver-not-loaded.
If you do all of that, the Windows 10 out-of-the-box VCP driver can be used. No need to install the very old ST VCP driver on your system.
PS: I read somewhere turning on VSense makes problems, too. Don't know, I have not configured it and all works like a charm.
Put delay before CDC_Transmit_FS call - it will wait for the initiatialization. Your code should be like this
int main(void)
{
uint8_t Text[] = "Hello\r\n";
HAL_Delay(1000);
while (1)
{
CDC_Transmit_FS(Text,6); /*when commented the port is recognized*/
HAL_Delay(1000);
}
}
I had similar issue. I couldn't connect to a port and the port appears as just "virtual com port". I added while loop to wait for USBD_OK from CDC_Transmit_FS. Then it stars work even with out it or a delay after init function. I am not sure what the issue was.
while(CDC_Transmit_FS((uint8_t*)txBuf, strlen(txBuf))!=USBD_OK)
{
}
you may have to install driver to get device recognized as com port
you can get it from st site
if not installed the device is listed with question or exclamation mark on device manager
note that you cannot send until device get connected to host!
not sure that CubeMX CDC_Transmit_FS is checking for this
also instead of delay to resend you shall check the CDC class data "TXSstate"
is 0 mean tx is over.
I know it's a bit late, but I stumbled upon this post and it was extremely helpful.
Here is what I needed to do:
do the Line-Coding (I think only necessary on Windows-Systems)
increase Heap (Stack was left at default 0x200)
Here is what wasn't necessary for me (on a STM32F405RGT6 Chip):
change APP_RX_DATA_SIZE / APP_TX_DATA_SIZE (left it at 2048)
add a delay befor running CDC_Tranmit_FS()
Also some things to consider that happened to me in the past:
be sure to use a USB-Cable with data lines (most charging-cables don't have them)
double check the traces/connections if you use a custom board

The simplest bridge example won't work - Arduino Yun

I tried to modify the Temperature Web Panel example (found in arduino-1.5.6-rw/libraries/Bridge/examples/TemperatureWebPanel) for a light sensor. Unfortunately it seems even the simplest receive and transmit result over wifi doesn't work! I even commented out the working part to just send back some text to the browser as you can see, but I still see nothing in the browser:
#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>
// Listen on default port 5555, the webserver on the Yun
// will forward there all the HTTP requests for us.
YunServer server;
String startString;
long hits = 0;
void setup() {
Serial.begin(9600);
// For debugging, wait until the serial console is connected.
/*delay(4000);
while(!Serial);
Bridge.begin();
*/
// Bridge startup
pinMode(13, OUTPUT);
Bridge.begin();
digitalWrite(13, HIGH);
pinMode(A0, INPUT);
// Listen for incoming connection only from localhost
// (no one from the external network could connect)
server.listenOnLocalhost();
server.begin();
// get the time that this sketch started:
Process startTime;
startTime.runShellCommand("date");
while (startTime.available()) {
char c = startTime.read();
startString += c;
}
Serial.println("yeah\n");
Serial.println(startTime);
}
void loop() {
// Get clients coming from server
Serial.println("a\n");
YunClient client = server.accept();
// There is a new client?
if (client) {
Serial.println("Client!\n");
// read the command
String command = client.readString();
client.print('(This should definitely be sent over bridge)');
/*command.trim(); //kill whitespace
Serial.println(command);
// is "temperature" command?
if (command == "temperature") {
// get the time from the server:
Process time;
time.runShellCommand("date");
String timeString = "";
while (time.available()) {
char c = time.read();
timeString += c;
}
Serial.println(timeString);
int sensorValue = analogRead(A0);
// convert the reading to millivolts:
client.print("Current time on the Yún: ");
client.println(timeString);
client.print("<br>Current value: ");
client.print(sensorValue);
client.print("<br>This sketch has been running since ");
client.print(startString);
client.print("<br>Hits so far: ");
client.print(hits);
}*/
// Close connection and free resources.
client.stop();
hits++;
}
delay(50); // Poll every 50ms
}
I see the "a" multiple times in the serial monitor, but never see anything in the arduino.local/arduino/temperature url, just a blank response.
Furthurmore, after awhile it seems the Yun was disconnecting from the network, not accessible over http or ssh. How does one debug an issue like this, considering ssh is the main way to communicate with this computer?
After debugging step by step on my own configuration, I found that the code never advanced past Bridge.begin().
Upon further investigation, I found that the default Bridge baud rate of 250000 no longer matched the kernel baud rate of 115200.
Changing to: Bridge.begin(115200) ... fixed the issue for me.
To determine your kernel speed, run cat /proc/cmdline from a terminal into your Yun
See this link for more info: https://groups.google.com/forum/#!msg/linino/-rSmpjX4UOM/Cnjv-uzrlfgJ
If this isn't your issue, consider adding debug information (ie.. Serial.print()) in the actual source files for Bridge.cpp, etc. Unfortunately, it appears that Arduino/Linino devs often make breaking changes and do not have the resources to update documentation, examples, etc.
If you are on Windows, don't use 'arduino.local', because Windows has problems to resolve this host.
Have you tried with the IP address ?
You must televerse your script through wifi, and not through serial (in arduino Ide you must change the port)
Have you created the path 'arduino/www/'
You need a micro SD card plugged in to your Yún with a folder named “arduino” at the root. Inside the “arduino” folder, there must be a directory called “www”. You need to upload the sketch via WiFi to transfer the contents of the local “www” folder. You cannot transfer files via USB. Once uploaded, you can open your favorite browser and go to http://arduino.local/sd/TemperatureWebPanel.
you must open http://YUNS_IP/sd/TemperatureWebPanel
if you are using the Yun Shield u need to comment out the Serial commands or remove all references to serial as the Bridge and the Serial port all share the same hardware serial. I faced the same problem there was no connection.
Replace serial.begin(115...) by Bridge.begin().

MCU/Arduino RESET

I have below setup. I found that My arduino Getting restarted again. I have Arduino board , lCD Display, SENSOR. The Sensor and LCD Display SHARE common ground and 5v supply from 7805 IC and Arduino board get powered on using 7812 regulator IC . finally they are under common ground potential.Now i have Peice of code. Individually i tried each function are working fine without any error and Arduino Board will not get reseted.When i put together all my code block. getting restarted. Once it restart work fine for couple of minute and again reseted.
How to resolve this issue?
#include <LiquidCrystal.h>
#include <avr/wdt.h>
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
void setup()
{
Serial.begin(9600);
wdt_enable(WDTO_8S);
MODE=INIT;
pinMode(beeper, OUTPUT);
lcdClear();
}
void loop()
{
Track_loop();
LCD_Display();
CHK_Key();
wdt_reset();
Serial.println("..........................");
}
void Track_loop()
{
calcPos();
calcTime();
calcElevationAngle();
callMode();
actuate();
// checkHWFaults();
Wind_calc();
Print_Result();
}
void Print_Result()
{
Print_Date();
Print_Time();
}
I have added these function in my Serial.println statement
void Print_Date(){
Serial.print("Local Date:");
Serial.print(local_day);
Serial.print("/");
Serial.print(local_month);
Serial.print("/");
Serial.println(local_year);
}
void Print_Time()
{ Serial.print("local_time is:");
Serial.print(local_h);
Serial.print("-");
Serial.print(local_m);
Serial.print("-");
Serial.println(local_s);
}
I suspect your use of the Watchdog is causing issues.
If we say that http://www.embedds.com/using-watchdog-timer-in-your-projects/ is an "authoritive" source of information, then maybe your call to wdt_reset() is not being called in time and therefore your system is being reset?
If you enabled watchdog timer, you have to take care and reset it
before it fills up and resets MCU. Otherwise if your program hangs or
sticks in some infinite loop without reset watchdog simply counts up
and resets system
From http://www.nongnu.org/avr-libc/user-manual/group_avr_watchdog.html
#define wdt_reset() __asm__ __volatile__ ("wdr")
Reset the watchdog timer. When the watchdog timer is enabled, a call
to this instruction is required before the timer expires, otherwise a
watchdog-initiated device reset will occur.
If you disable the watchdog from your project, do you still get the same outcome?
Update 1
To debug your code you use the Serial.println("xxxx") function to output the required text to the serial port that you have setup.
See
Google Search
LadyAda Tutorial
Countless others
NOTE: Should we update these instructions so that they contain the full instructions? Marking as Community Wiki so everyone can update as required.

Program halts while sending byte to USB-to-Prallel (D-Sub 25pins) port

I have an USB to 25-pin parallel port cable. I connected it to laptop and get a port at /dev/usb/lp0, then I connect pin1/pin25 of parallel port with +/- LED it lights.
Now I move +pin of LED to pin 2 of parallel port, and try to send byte to port , it should light up but it doesn't. following is my c code in linux.
#include<stdlib.h>
#include<stdio.h>
#include<sys/io.h>
void main(void)
{
int port=0x378;
outb(0xff,port);
outb(0x01,port+2);
sleep(1);
outb(0x00,port+2);
printf("‌​End");
}
this is my complete program, I also checked it with port=0x3bc but did not work. Even I checked strobe pin 1 by connecting + of LED, it alsways lights but does not go low. May be there is problem with port address.
for /dev/usb/lp0 I also used command line command like $echo $'\xFF' > /dev/usb/lp0 I observed that it does something but does not light up the LED. When I connect LED with pin2 and 25 as + and -. I can see a very little light in side the LED in the beginning but when I run above command that very tiny light disappears but LED does not glow high like it does when I connect it with strobe pin. Also I can only execute above command only two times. Third and later times I thing command does not work while it seems halted.
Can any one help why it halts or how can I get LED switch on/off on data pins? Thanks

Resources