I'm trying to turn on an LED connected to an Arduino from a ruby file, as well as sending a string from that Arduino to my ruby file / terminal. (I'm doing these two things separately, to avoid potential problems).
I'm using a USB port to connect Arduino - Computer, and the serialport gem. I'm working in Linux.
To receive the string on my computer:
I have followed several tutorials that all recommend to run this ruby file:
require 'serialport'
port_str = '/dev/ttyACM0'
baud_rate = 9600
data_bits = 8
stop_bits = 1
parity = SerialPort::NONE
sp = SerialPort.new(port_str, baud_rate, data_bits, stop_bits, parity)
while(true) do
message = sp.gets.chomp
puts message
end
And uploaded this to the Arduino:
uint8_t c;
int i;
void setup() {
Serial.begin(9600); // set baud rate
pinMode(13, OUTPUT); // set pin 13 to be output for LED
digitalWrite(13, LOW); // start with LED off
}
void loop() {
while (Serial.available() > 0) { // check if serial port has data
// writes to computer
Serial.println("Hello world");
}
}
I got this error message: undefined method `chomp' for nil:NilClass (NoMethodError). When getting rid of .chomp, at some point I could get some parts of "Hello world" being printed on my terminal, such as "Hell", and then far later "o w", etc.
Now I don't even get anything anymore.
To turn the led on:
In the ruby file, I replaced the message part by
sp.write('a')
And in the arduino file,
void loop() {
while (Serial.available() > 0) { // check if serial port has data
c = Serial.read(); // read the byte
if (c == 'a'){
digitalWrite(13, HIGH); // turn on LED
delay(500); // wait 500 milliseconds
digitalWrite(13, LOW); // turn off LED
delay(500); // wait 500 milliseconds
}
}
}
The led turns on if I remove the condition (c == 'a'), but obviously I need it if I want the arduino to perform different actions.
I'm new to Serial communication, so I'm not quite sure where the error comes from, since I feel somehow some data seems to be transmitted.
Related
I'm currently implementing a driver for the WINC1500 to be used with an ATMEGA32 MCU and it's getting stuck on this line of "while(!spi_is_tx_empty(WINC1500_SPI));". The code builds and runs but it won't clear what's inside in this function to proceed through my code and boot up the Wifi Module. I've been stuck on this problem for weeks now with no progress and don't know how to clear it.
static inline bool spi_is_tx_empty(volatile avr32_spi_t *spi)
{
// 1 = All Transmissions complete
// 0 = Transmissions not complete
return (spi->sr & AVR32_SPI_SR_TXEMPTY_MASK) != 0;
}
Here is my implementation of the SPI Tx/Rx function
void m2mStub_SpiTxRx(uint8_t *p_txBuf,
uint16_t txLen,
uint8_t *p_rxBuf,
uint16_t rxLen)
{
uint16_t byteCount;
uint16_t i;
uint16_t data;
// Calculate the number of clock cycles necessary, this implies a full-duplex SPI.
byteCount = (txLen >= rxLen) ? txLen : rxLen;
// Read / Transmit.
for (i = 0; i < byteCount; ++i)
{
// Wait for transmitter to be ready.
while(!spi_is_tx_ready(WINC1500_SPI));
// Transmit.
if (txLen > 0)
{
// Send data from the transmit buffer
spi_put(WINC1500_SPI, *p_txBuf++);
--txLen;
}
else
{
// No more Tx data to send, just send something to keep clock active.
// Here we clock out a don't care byte
spi_put(WINC1500_SPI, 0x00U);
// Not reading it back, not being cleared 16/1/2020
}
// Reference http://asf.atmel.com/docs/latest/avr32.components.memory.sdmmc.spi.example.evk1101/html/avr32_drivers_spi_quick_start.html
// Wait for transfer to finish, stuck on here
// Need to clear the buffer for it to be able to continue
while(!spi_is_tx_empty(WINC1500_SPI));
// Wait for transmitter to be ready again
while(!spi_is_tx_ready(WINC1500_SPI));
// Send dummy data to slave, so we can read something from it.
spi_put(WINC1500_SPI, 0x00U); // Change dummy data from 00U to 0xFF idea
// Wait for a complete transmission
while(!spi_is_tx_empty(WINC1500_SPI));
// Read or throw away data from the slave as required.
if (rxLen > 0)
{
*p_rxBuf++ = spi_get(WINC1500_SPI);
--rxLen;
}
else
{
spi_get(WINC1500_SPI);
}
}
Debug output log
Disable SPI
Init SPI module as master
Configure SPI and Clock settings
spi_enable(WINC1500_SPI)
InitStateMachine()
INIT_START_STATE
InitStateMachine()
INIT_WAIT_FOR_CHIP_RESET_STATE
m2mStub_PinSet_CE
m2mStub_PinSet_RESET
m2mStub_GetOneMsTimer();
SetChipHardwareResetState (CHIP_HARDWARE_RESET_FIRST_DELAY_1MS)
InitStateMachine()
INIT_WAIT_FOR_CHIP_RESET_STATE
if(m2m_get_elapsed_time(startTime) >= 2)
m2mStub_PinSet_CE(M2M_WIFI_PIN_HIGH)
startTime = m2mStub_GetOneMsTimer();
SetChipHardwareResetState(CHIP_HARDWARE_RESET_SECOND_DELAY_5_MS);
InitStateMachine()
INIT_WAIT_FOR_CHIP_RESET_STATE
m2m_get_elapsed_time(startTime) >= 6
m2mStub_PinSet_RESET(M2M_WIFI_PIN_HIGH)
startTime = m2mStub_GetOneMsTimer();
SetChipHardwareResetState(CHIP_HARDWARE_RESET_FINAL_DELAY);
InitStateMachine()
INIT_WAIT_FOR_CHIP_RESET_STATE
m2m_get_elapsed_time(startTime) >= 10
SetChipHardwareResetState(CHIP_HARDWARE_RESET_COMPLETE)
retVal = true // State machine has completed successfully
g_scanInProgress = false
nm_spi_init();
reg = spi_read_reg(NMI_SPI_PROTOCOL_CONFIG)
Wait for a complete transmission
Wait for transmitter to be ready
SPI_PUT(WINC1500_SPI, *p_txBuf++);
--txLen;
Wait for transfer to finish, stuck on here
Wait for transfer to finish, stuck on here
The ATmega32 is an 8-bit AVR but you seem to be using code for the AVR32, a family of 32-bit AVRs. You're probably just using the totally wrong code and you should consult the datasheet of the ATmega32, and search for SPI for the AVR ATmega family.
I'm trying to program my MCU in mickroc still I do not get a output.
Is there any difference between PIC18f65k40 and PIC18f65k22 in terms of initiating uart transmission? Whether there is any need to initiate or disable any special registers in PIC18f65k40?
In mikroc software there present a library for uart so I just copied the program from mickroe website and add my transmitter and receiver pins(RX4PPS = 0x11; and TX4PPS = 0x10;)in program by configuring portc as output but my circuit does not work.
char i ;
void main()
{
TRISC = 0b00000000;// making port as output
RX4PPS = 0x11;
TX4PPS = 0x10;
UART4_Init(9600); // Initialize USART module
// (8 bit, 9600 baud rate, no parity bit...)
delay_ms(500);
UART4_Write_Text("Hello world!");
UART4_Write(13); // Start a new line
UART4_Write(10);
UART4_Write_Text("PIC18F65K40 UART example");
UART4_Write(13); // Start a new line
UART4_Write(10);
while (1) {
if (UART4_Data_Ready()) { // If data has been received
i = UART4_Read(); // read it
UART4_Write(i); // and send it back
}
}
}
The PIC18F65k22 has only 2 UARTs and you are working with UART4. I guess the library don't support the controller. Better write the code on your own. There are some examples in the datasheet.
I am trying to send an echo from a shell script on some event.
echo naval > /dev/cu.usbmodem1421
on this /dev/cu.usbmodem1421 serial port my Blend Micro Atmega32u4 Board is listening.
But every time I send an echo it says, resource busy.
I am not sure what can be the solution?
Arduino code:
int incomingByte = 0; // for incoming serial data
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print((char)incomingByte);
}
}
have you tried killing the process ?, it seems it is hanging waiting a bit broad of a question but it could be caused by numerous things.
I can't get my arduino fio with bluetooth bee paired with my mac. I got my application working with a different board (arduino uno) and USB connection. The code I'm uploading to my arduino fio is below:
#include <SoftwareSerial.h>
SoftwareSerial softSerial(2, 3); // RX, TX
void setup() {
// bluetooth bee setup
softSerial.print("\r\n+STWMOD=0\r\n"); // set to slave
delay(1000);
softSerial.print("\r\n+STNA=MYAPP\r\n"); // set name
delay(1000);
// Serial.print("\r\n+STAUTO=1\r\n"); // permit auto-connect of paired devices
softSerial.print("\r\n+STOAUT=1\r\n");
delay(1000);
//Serial.print("\r\n +STPIN=0000\r\n"); // set PIN
//delay(1000);
softSerial.print("\r\n+STBD=9600\r\n"); // set baud
delay(2000); // required
// initiate BTBee connection
softSerial.print("\r\n+INQ=1\r\n");
delay(20000); // wait for pairing
// Start the software serial.
softSerial.begin(9600);
// Start the hardware serial.
Serial.begin(9600);
}
I think the pins are right -- 2 and 3 seem to be the pins that connect to the bluetooth bee. I've been googling for 2 days straight, and people don't seem to have problems pairing. What am I doing wrong?
Thanks,
Ok -- this took me nearly three solid days of Google-fu, and I stumbled across this page. Apparently that guy, also, had an immense amount of trouble finding a solution, so hopefully having the solution posted on StackOverflow will help future inquirers.
Really, two things are necessary. First, for whatever reason, I have no idea why, you don't worry about the "software serial". Just address the "Serial". Secondly, it will not work if you don't have the baud for the Serial at 38400. I'm actually using a "software serial" to talk to another device, and that baud is at 9600, but for the bluetooth Serial, you want it at 38400.
If you define "setup" as follows, the BluetoothBee should blink red and green, and pair (mac has nothing to do with it):
long DATARATE = 38400; // default data rate for BT Bee
char inChar = 0;
int LED = 13; // Pin 13 is connected to a LED on many Arduinos
void setup() {
Serial.begin(DATARATE);
// bluetooth bee setup
Serial.print("\r\n+STWMOD=0\r\n"); // set to slave
delay(1000);
Serial.print("\r\n+STNA=myDeviceName\r\n"); // set the device name
delay(1000);
Serial.print("\r\n+STAUTO=0\r\n"); // don't permit auto-connect
delay(1000);
Serial.print("\r\n+STOAUT=1\r\n"); // existing default
delay(1000);
Serial.print("\r\n +STPIN=0000\r\n"); // existing default
delay(2000); // required
// initiate BTBee connection
Serial.print("\r\n+INQ=1\r\n");
delay(2000); // wait for pairing
pinMode(LED, OUTPUT);
}
Then, after pairing you should see another serial port under 'tools -> serial port' in your Arduino IDE. If you select that and define the "loop" function as follows, you should be able to send those commands and get verification that you are, in fact, talking to the bluetooth bee:
void loop() {
// test app:
// wait for character,
// a returns message, h=led on, l=led off
if (Serial.available()) {
inChar = Serial.read();
if (inChar == 'a') {
Serial.print("connected"); // test return connection
}
if (inChar == 'h') {
digitalWrite(LED, HIGH); // on
}
if (inChar == 'l') {
digitalWrite(LED, LOW); // off
}
}
}
I had the following script working fine after changing from sp.getc to sp.gets.
require "rubygems"
require "serialport"
require "data_mapper"
#params for serial port
port_str = "/dev/ttyACM0"
baud_rate = 19200
data_bits = 8
stop_bits = 1
parity = SerialPort::NONE
sp = SerialPort.new(port_str, baud_rate, data_bits, stop_bits, parity)
loop do
sp_char = sp.gets
if sp_char.start_with?("Time")
printf("%s", sp_char)
end
end
I then reloaded the Arduino with the following (which I thought was what I had anyway?)
//SMS Send and Receive program
//Using Mega 2560 & Elecrow SIM900 GSM Shield
//4 July 2013
int count = 0;
int sendCount=0;
int n_sms,x,sms_start;
char data[300];
void setup()
{
Serial1.begin(19200); // the GPRS/GSM baud rate
Serial.begin(19200); // the USB baud rate
Serial1.println("AT\r");
Serial1.flush();
delay(1000);
Serial1.println("AT+CMGF=1\r");
Serial1.flush();
delay(1000);
Serial.println("Text Mode Selected");
Serial.flush();
//sendSMS();
}
void loop()
{
if(sendCount>720) //approx 60 minutes between sends
{
//sendSMS();
sendCount=0;
}
else
Serial1.print("AT+CMGR=1\r"); //Reads the first SMS
Serial1.flush();
for (x=0;x < 255;x++){
data[x]='\0';
}
x=0;
do{
while(Serial1.available()==0);
data[x]=Serial1.read();
x++;
if(data[x-1]==0x0D&&data[x-2]=='"'){
x=0;
}
}while(!(data[x-1]=='K'&&data[x-2]=='O'));
data[x-3]='\0'; //finish the string before the OK
Serial.println(data); //shows the message
Serial.flush();
Serial1.print("AT+CMGD=1,4\r");
Serial1.flush();
delay(5000);
sendCount++;
}
void sendSMS()
{
Serial1.println("AT+CMGS=\"+64xxxxxxxxxx\"\r");
//Replace this number with the target mobile number.
Serial1.flush();
delay(1000);
Serial1.println("SA\r"); //The text for the message
Serial1.flush();
delay(1000);
Serial1.write(0x1A); //Equivalent to sending Ctrl+Z
}
Now I get the following error :
serial_arduino.rb:16:in block in <main>': undefined methodstart_with?'for nil:NilClass (NoMethodError)
from serial_arduino.rb:14:in loop'
from serial_arduino.rb:14:in'
If I remove the start_with? method, then all is fine. Testing with irb shows method is OK
irb(main):003:0> 'AT'.start_with? 'Time'
=> false
irb(main):004:0> 'Time:0902'.start_with? 'Time'
=> true
Appears to be related to Arduino or my vitualised Debian machine. I have got it going after rebooting VM and powering off Arduino.