Wemos D1 reset when setting pins mode - arduino

I'm building a little car, remote controlled by a Wemos D1 board, in order to set the WiFi connection and the control logic I'm running this script:
#include <ESP8266WiFi.h>
const char* pass = "**********";
const char* ssid = "**********";
IPAddress ip(192,168,1,91);
IPAddress gat(192,168,1,1);
IPAddress dns(192,168,1,1);
IPAddress sub(255,255,255,0);
WiFiServer s(2000);
int inA1 = 1;
int inA2 = 2;
int enA = 3;
int inB1 = 4;
int inB2 = 5;
int enB = 6;
int trigger = 7;
int echo = 8;
double vSuono = 343; //Unità di misura: m/s
int speed = 255;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
WiFi.config(ip,gat,sub,dns);
WiFi.begin(ssid,pass);
delay(500);
while(WiFi.status() != WL_CONNECTED){
delay(500);
Serial.println(".");
}
Serial.println("Connected!");
delay(30);
s.begin();
Serial.println("Server running!");
delay(30);
//Here starts the problems
pinMode(inA1,OUTPUT);
pinMode(inA2,OUTPUT);
pinMode(enA,OUTPUT);
pinMode(inB1,OUTPUT);
pinMode(inB2,OUTPUT);
pinMode(enB,OUTPUT);
pinMode(trigger,OUTPUT);
pinMode(echo,INPUT);
delay(500);
}
void loop() {
// put your main code here, to run repeatedly:
WiFiClient c = s.available();
delay(30);
if(c){
Serial.println("New client connected!");
delay(3);
while(c.connected()){
if(c.available()){
String command = c.readStringUntil('\n');
if(command == "forward"){
Serial.println("forward");
forward(speed);
}else if(command == "right"){
Serial.println("right");
right(speed);
}else if(command == "left"){
Serial.println("left");
left(speed);
}else{
Serial.println("back");
back(speed);
}
}
delay(30);
}
c.stop();
}
}
void forward(int velocita){
digitalWrite(inA1,HIGH);
digitalWrite(inA2,LOW);
digitalWrite(inB1,HIGH);
digitalWrite(inB2,LOW);
analogWrite(enA,velocita);
analogWrite(enB,velocita);
}
void left(int velocita){
digitalWrite(inA1,HIGH);
digitalWrite(inA2,LOW);
digitalWrite(inB1,LOW);
digitalWrite(inB2,HIGH);
analogWrite(enA,velocita);
analogWrite(enB,velocita);
}
void right(int velocita){
digitalWrite(inA1,LOW);
digitalWrite(inA2,HIGH);
digitalWrite(inB1,HIGH);
digitalWrite(inB2,LOW);
analogWrite(enA,velocita);
analogWrite(enB,velocita);
}
void back(int velocita){
digitalWrite(inA1,LOW);
digitalWrite(inA2,HIGH);
digitalWrite(inB1,LOW);
digitalWrite(inB2,HIGH);
analogWrite(enA,velocita);
analogWrite(enB,velocita);
}
void stop(){
digitalWrite(inA1,LOW);
digitalWrite(inA2,LOW);
digitalWrite(inB1,LOW);
digitalWrite(inB2,LOW);
}
The problem is that when the board execute the pinMode function in the setup() block, the board stop the execution, crash and restart, and I'm not able to ping the board.
If I comment all the portion of the setup() block, with the pinMode calls, the program starts to work but obviously I can't use the pins.
On the serial monitor when the board crash appears this messages:
ets Jan 8 2013,rst cause:4, boot mode:(3,6)
wdt reset
load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v09f0c112
~ld
What could be the problem?

I don't know the pin mapping by heart, but you should stick to the GPIO pins named D1, D2...D8. You've named them 1, 2...8 which are different pins. You likely used a pin which is used by something else (like serial or reset).
int inA1 = D1;
int inA2 = D2;
int enA = D3;
int inB1 = D4;
int inB2 = D5;
int enB = D6;
int trigger = D7;
int echo = D8;

I was getting the same wdt reset error while trying to use pinMode with digitalWrite() function.
I solved it by figuring out how the Wemos Pin mapping works.
Actually, you need to refer the pin in 'Dx' notation.
e.g
digitalWrite(D15,LOW);
or
pinMode(D15, OUTPUT);
Also, make sure to select "Wemos D1 R1" in Tools > Boards so that Dx constants will match
the labels.
Have a look at the conversation here on Arduino FOrum to understand more about wemos pin mapping:
https://forum.arduino.cc/index.php?topic=545113.0

Related

How to Send Float/Double over SPI Arduino (SAMD21 Based Board)

I am trying to send a double/float over SPI from my SAMD21 based board, with chip select on pin A1/A2. I have copied some code from the internet, but I don't really understand it, plus it only works for sending data between two Arduino Unos. Here is my master:
#include <SPI.h>
float a = 3.14159;
float b = 2.252332;
uint8_t storage [12];
float buff[2] = {a, b};
void setup()
{
digitalWrite(SS, HIGH);
SPI.begin();
Serial.begin(9600);
SPI.setClockDivider(SPI_CLOCK_DIV8);
}
void loop()
{
digitalWrite(SS, LOW);
memcpy(storage, &buff, 8);
SPI.transfer(storage, sizeof storage ); //SPI library allows a user to
//transfer a whole array of bytes and you need to include the size of the
//array.
digitalWrite(SS, HIGH);
delay(1000);
}
And here is my slave:
#include <SPI.h>
byte storage [8];
volatile byte pos;
volatile boolean process;
float buff[2];
void setup()
{
pinMode(MISO,OUTPUT);
SPCR |= _BV(SPE);
SPCR |= _BV(SPIE);
pos = 0;
process = false;
Serial.begin(9600);
}
ISR(SPI_STC_vect)
{
byte gathered = SPDR;
if( pos < sizeof storage)
{
storage[pos++] = gathered;
}
else
process = true;
}
void loop()
{
if( process )
{
memcpy(buff,&storage,8);
Serial.print("This is val1:");Serial.println(buff[0], 5);
Serial.print("This is val2:");Serial.println(buff[1], 6);
storage[pos] = 0;
pos = 0;
process = false;
}
}
Any help would be aprreciated, and please understand that I am a newb in this subject.

How to fix arduino program to blink LED based on mqtt message

I am doing program on Arduino IDE with goal to blink LED on breadboard with NodeMCU whenever it receives ON message from MQTT topic.I have done the program but the LED is not blinking. If I do the program removing MQTT portion, things are fine. Might be I am doing something wrong in loop().
I created MQTT topic using Node-Red.When I tested publish-subscription on Node Red, the flow is working as expected. But when I am trying to read the message from arduino, I dont see LED changing it's state.
Copying relevant portion of the code-
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
const char* ssid = "NET";
const char* password = "xx";
const char* mqtt_server = "192.x.x.x";
const int lamp = 4;
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
messageTemp += (char)payload[i];
}
Serial.println();
Serial.println("messageTemp-"+messageTemp);
if (topic=="room/lamp") {
Serial.print("Changing Room lamp to ");
if (messageTemp == "on") {
digitalWrite(lamp, LOW);
Serial.print("On");
}
else if (messageTemp == "off") {
digitalWrite(lamp, HIGH);
Serial.print("Off");
}
}
Serial.println();
}
void setup() {
pinMode(lamp, OUTPUT);
Serial.begin(9600);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}
I expect whenever I am pushing ON and OFF messages on the topic 'room/lamp' from nodered, the LED should get turned on and off.But it is not happening now.I am new to Arduino.Please advise.

'Serial1' does not name a type - Error in arduino

I am trying to verify the Arduino code in ADS1198 and Arduino Due. It comes the error show 'Serial1' does not name a type' even I defined two serial port in the beginning(where I gave the comments now). How to deal with this error 'Serial1' does not name a type'. how can I define these two port on ArduinoDue so that complieling successfully.
#include <ads1298.h>
#include <Base64.h>
// Minimal sketch for connection to ADS129n family. Load this script and open Tools/SerialMonitor.
// You should see text like this
// Device Type (ID Control Register): 62 Channels: 8
// If you see "Channels: 0" then check your wiring
#include "ads1298.h"
#include "adsCMD.h"
#include <Base64.h>
#include <SPI.h> // include the SPI library:
int gMaxChan = 0; //maximum number of channels supported by ads129n = 4,6,8
int gIDval = 0; //Device ID : lower 5 bits of ID Control Register
int activeSerialPort = 0; //data will be sent to serial port that last sent commands. E.G. bluetooth or USB port
const int kPIN_LED = 13; //pin with in-built light - typically 13, 11 for Teensy 2.0.
//ADSCMD
#include "Arduino.h"
//For Leonardo SPI see http://openenergymonitor.blogspot.com/2012/06/arduino-leonardo-atmega32u4-and-rfm12b.html
//constants define pins on Arduino
// Arduino Due
const int IPIN_PWDN = 47; //not required for TI demo kit
const int PIN_CLKSEL = 49; //6;//*optional
const int IPIN_RESET = 48; //*optional
const int PIN_START = 46;
const int IPIN_DRDY = 45;
const int IPIN_CS = 52;
const int PIN_DOUT = 11; //SPI out
const int PIN_DIN = 12; //SPI in
const int PIN_SCLK = 13; //SPI clock
//
//function prototypes
void adc_wreg(int reg, int val); //write register
void adc_send_command(int cmd); //send command
int adc_rreg(int reg); //read register
//start Serial Peripheral Interface
int numActiveChannels = 0;
boolean gActiveChan[9]; // reports whether channels 1..9 are active
boolean isRdatac = false;
boolean base64Mode = false;
int sampleCount=0;
boolean isLimit=false;
char hexDigits[] = "0123456789ABCDEF";
uint8_t serialBytes[200];
char sampleBuffer[1000];
uint8_t chan1[2];
const char *hardwareType = "unknown";
const char *boardName = "HackEEG";
const char *makerName = "Hamid, Mujahid, Abdul Hameed";
const char *driverVersion = "ADS1298 driver v0.1";
#if defined(__SAM3X8E__)
//#define isDUE //Detect Arduino Due
//#define WiredSerial SerialUSB //Use Due's Native port
//#define NSerial SerialUSB
#endif
void setup(){
using namespace ADS1298;
//prepare pins to be outputs or inputs
pinMode(PIN_SCLK, OUTPUT); //optional - SPI library will do this for us
pinMode(PIN_DIN, OUTPUT); //optional - SPI library will do this for us
pinMode(PIN_DOUT, INPUT); //optional - SPI library will do this for us
pinMode(IPIN_CS, OUTPUT);
pinMode(PIN_START, OUTPUT);
pinMode(IPIN_DRDY, INPUT);
pinMode(PIN_CLKSEL, OUTPUT); // *optional
pinMode(IPIN_RESET, OUTPUT); // *optional
pinMode(IPIN_PWDN, OUTPUT); // *optional
digitalWrite(PIN_CLKSEL, HIGH); // External clock
//start Serial Peripheral Interface
SPI.begin();
SPI.setBitOrder(MSBFIRST);
#ifndef isDUE
SPI.setClockDivider(SPI_CLOCK_DIV4); //forum.pjrc.com/.../1156-Teensy-3-SPI-Basic-Clock-Questions
#endif
SPI.setDataMode(SPI_MODE1);
//Start ADS1298
delay(500); //wait for the ads129n to be ready - it can take a while to charge caps
digitalWrite(PIN_CLKSEL, HIGH); // External clock
delay(10); // wait for oscillator to wake up
delay(1);
digitalWrite(IPIN_PWDN, HIGH); // *optional - turn off power down mode
digitalWrite(IPIN_RESET, HIGH);
delay(1000);// *optional
digitalWrite(IPIN_RESET, LOW);
delay(1);// *optional
digitalWrite(IPIN_RESET, HIGH);
delay(1500); // *optional Wait for 18 tCLKs AKA 9 microseconds, we use 1 millisecond
adc_send_command(SDATAC); // Send SDATAC Command (Stop Read Data Continuously mode)
// delayMicroseconds(2);
delay(100);
// Determine model number and number of channels available
gIDval = adc_rreg(ID); //lower 5 bits of register 0 reveal chip type
switch (gIDval & B00011111 ) { //least significant bits reports channels
case B10000: //16
hardwareType = "ADS1294";
gMaxChan = 4; //ads1294
break;
case B10001: //17
hardwareType = "ADS1296";
gMaxChan = 6; //ads1296
break;
case B10010: //18
hardwareType = "ADS1298";
gMaxChan = 8; //ads1298
break;
case B11110: //30
hardwareType = "ADS1299";
gMaxChan = 8; //ads1299
break;
case B10110: //22
hardwareType = "ADS1198";
gMaxChan = 8; //ads1198
break;
default:
gMaxChan = 0;
}
}
void detectActiveChannels() { //set device into RDATAC (continous) mode -it will stream data//
if ((isRdatac) || (gMaxChan < 1)) return; //we can not read registers when in RDATAC mode
//Serial.println("Detect active channels: ");
using namespace ADS1298;
numActiveChannels = 0;
for (int i = 1; i <= gMaxChan; i++) {
delayMicroseconds(1);
int chSet = adc_rreg(CHnSET + i);
gActiveChan[i] = ((chSet & 7) != SHORTED);
if ( (chSet & 7) != SHORTED) numActiveChannels ++;
}
}
//start serial port
Serial1.begin(115200);
Serial.begin(115200); //use native port on Due
Serial1.begin(115200) and Serial.begin(115200) need to be placed in a function. Put them in setup().

XBee not communicating with Arduino connected

I have two XBee S2 modules. Both are communicating when I connect them directly to my computer and check via X-CTU terminal. The problem is when I try to send data wirelessly by connecting both of them with two Arduinos there is no communication at all. It doesn't send any value.
This is the code for the receiving side:
#include <SoftwareSerial.h>
SoftwareSerial XBSerial = SoftwareSerial(2, 3);
int BackMotorForward = 6;
int BackMotorReverse = 5;
int FrontMotorRight = 10;
int FrontMotorLeft = 9;
int sv1 = 0;
int sv2 = 0;
void setup ()
{
pinMode(BackMotorForward, OUTPUT); // Initialize the pin as an output.
pinMode(BackMotorReverse, OUTPUT); // Initialize the pin as an output.
pinMode(FrontMotorRight, OUTPUT); // Initialize the pin as an output.
pinMode(FrontMotorLeft, OUTPUT); // Initialize the pin as an output.
Serial.begin(9600);
// Set the data rate for the SoftwareSerial port
XBSerial.begin(9600);
// XBSerial.println(".");
}
void loop()
{
Serial.write(XBSerial.read());
if (XBSerial.available())
{
sv1 = XBSerial.read();
Serial.write(sv1);
}
if (XBSerial.available())
{
sv2 = XBSerial.read();
Serial.write(sv2);
}
if (sv1 < 280)
{
Serial.write("backward");
digitalWrite(BackMotorForward, HIGH);
digitalWrite(BackMotorReverse,LOW);
}
else if (sv1 > 380)
{
Serial.write("forward");
digitalWrite(BackMotorReverse,HIGH);
digitalWrite(BackMotorForward,LOW);
}
else
{
digitalWrite(BackMotorForward,LOW);
digitalWrite(BackMotorReverse,LOW);
}
if (sv2 > 380)
{
Serial.write("left");
digitalWrite(FrontMotorRight, HIGH);
digitalWrite(FrontMotorLeft,LOW);
}
else if (sv2 < 280)
{
Serial.write("right");
digitalWrite(FrontMotorLeft,HIGH);
digitalWrite(FrontMotorRight,LOW);
}
else
{
digitalWrite(FrontMotorRight,LOW);
digitalWrite(FrontMotorLeft,LOW);
}
}
This is the code for the sending side:
#include <SoftwareSerial.h>
SoftwareSerial XBSerial = SoftwareSerial(2, 3);
const int xpin = A0; // x-axis of the accelerometer
const int ypin = A1; // y-axis
void setup()
{
// Initialize the serial communications:
pinMode(xpin, INPUT); //x axis
pinMode(ypin, INPUT); //y axis
Serial.begin(9600);
Serial.println("testing");
// Set the data rate for the SoftwareSerial port
XBSerial.begin(9600);
XBSerial.println("testing!!!");
}
void loop()
{
// Print the sensor values:
Serial.print(analogRead(xpin));
Serial.print("\t");
Serial.print(analogRead(ypin));
// Print a tab between values:
Serial.print("\t");
Serial.println();
// Delay before next reading:
delay(100);
int val = analogRead(xpin);
int val2 = analogRead(ypin);
XBSerial.print(val); //Changed from write to print
XBSerial.print(val2);
}
Okay, it was a really stupid mistake. I was using softwareserial pins 2,3 for XBee, but instead I was connecting their pins directly to pin 0,1 (rx,tx) of the Arduino. That's the reason there was no communication.

Arduino Xbee Data parsing

Im sorry to make a post like this but i have tried everything and i cant get this working!
I have two arduinos hooked up with xbee's.
One is connected to my computer recieving data and the other is bettery powered and has a Wii nunchuck attached.
I know im getting good data from the nunchcuck cause i tested it without the xbee.
But i want to send the data over serial and recieve on the other to use for something else but doesnt seem to be working. Here is the code:
Arduino with wii:
#include <Wire.h>
#include <Servo.h>
const int vccPin = A3;
const int gndPin = A2;
Servo servo;
const int dataLength = 6; // Number of bytes to request
static byte rawData[dataLength];
enum nunchuckItems {
JoyX, JoyY, accelX, accelY, accelZ, btnZ, btnC};
void setup()
{
pinMode(gndPin, OUTPUT);
pinMode(vccPin, OUTPUT);
digitalWrite(gndPin, LOW);
digitalWrite(vccPin, HIGH);
servo.attach(9);
delay(1000);
Serial.begin(9600);
nunchuckInit();
}
void loop()
{
nunchuckRead();
int joyX = getValue(JoyX);
int joyY = getValue(JoyY);
Serial.print(joyX);
Serial.print(",");
Serial.print(joyY);
Serial.println();
}
void nunchuckInit(){
Wire.begin();
Wire.beginTransmission(0x52);
Wire.write((byte)0x40);
Wire.write((byte)0x00);
Wire.endTransmission();
}
static void nunchuckRequest(){
Wire.beginTransmission(0x52);
Wire.write((byte)0x00);
Wire.endTransmission();
}
boolean nunchuckRead(){
int cnt = 0;
Wire.requestFrom(0x52, dataLength);
while (Wire.available()){
rawData[cnt] = nunchuckDecode(Wire.read());
cnt++;
}
nunchuckRequest();
if (cnt >= dataLength)
return true;
else
return false;
}
static char nunchuckDecode(byte x){
return (x ^ 0x17) + 0x17;
}
int getValue(int item){
if (item <= accelZ)
return (int)rawData[item];
else if (item == btnZ)
return bitRead(rawData[5], 0) ? 0: 1;
else if (item == btnC)
return bitRead(rawData[5], 1) ? 0: 1;
}
How could i recieve this data on the recieving arduino?
Please help its for my school project!
Thank you!!
When you read the terminal (without the Xbee) do you see line with X,Y appear ? Because if your arduino terminal see it, the problem comes from the Xbee.
If your terminal see the line, look at your Xbee with Xctu. You must set the panID on both Xbee to see them communicate. you must also make the SL address of the sender equal to the DL address of the receiver (and same for the SH/DH).
Can you say us which Arduino, Xbee, shield you use. It can help us to have more details

Resources