Compiler throw an error when I added delay(timer) in the code, timer is a const integer defined at the beginning of the code.
When I comment out the delay line, the compiler completes successfully.
4th line from the bottom
// Include RadioHead Amplitude Shift Keying Library
#include <RH_ASK.h>
// Include dependant SPI Library
#include <SPI.h>
// Create Amplitude Shift Keying Object
RH_ASK rf_driver;
const int BLUE = 3; // the number of the pushbutton pin
const int GREEN = 4; // the number of the LED pin
const int RED = 5; // the number of the LED pin
const int thisPin = 4;
const int timer = 100;
// the setup function runs once when you press reset or power the board
void setup() {
// Initialize ASK Object
rf_driver.init();
Serial.begin(9600);
// initialize digital pin LED_BUILTIN as an output.
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
int mesg = 0;
// Set buffer to size of expected message
uint8_t buf[8];
uint8_t buflen = sizeof(buf);
if (rf_driver.recv(buf, &buflen)) {
// Message received with valid checksum
Serial.print("Message Received: ");
Serial.println((char*)buf);
mesg = 1;
}
if (mesg == 1) {
mesg = 0;
digitalWrite(thisPin, HIGH);
//delay(timer);
digitalWrite(thisPin, LOW);
}
}
Below is the error message:
C:\Users\Swee-Chuan Khoo\Documents\Arduino\libraries\RadioHead\RH_ASK.cpp: In member function 'setModeIdle.constprop':
C:\Users\Swee-Chuan Khoo\Documents\Arduino\libraries\RadioHead\RH_ASK.cpp:421:1: internal compiler error: Segmentation fault
}
^
Please submit a full bug report,
with preprocessed source if appropriate.
See <http://gcc.gnu.org/bugs.html> for instructions.
lto-wrapper.exe: fatal error: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\hardware\tools\avr/bin/avr-gcc returned 1 exit status
compilation terminated.
c:/program files/windowsapps/arduinollc.arduinoide_1.8.21.0_x86__mdqgnx93n4wtt/hardware/tools/avr/bin/../lib/gcc/avr/5.4.0/../../../../avr/bin/ld.exe: error: lto-wrapper failed
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board Arduino Nano.
Downgrade AVR core to 1.6.21 as suggested by Juraj. All working properly now.
Now i can continue with the project.
Thank you everyone for the help, datafiddler, Juraj and M.R.
Related
I am using a Feather Express NRF52840 and am trying to make use of the presupplied user button.
Reading the pinout says:
A tactile switch is provided for use in your projects, which is connected to P1.02 and is accessible in code as D7 in Arduino and SWITCH in CircuitPython.
I am using example code to read the button's state change.
The problem I am having is that the code will not compile when I use D7 rather than A1 in the original code. The error I get is:
sketch_nov04a:1:23: error: 'D7' was not declared in this scope; did you mean 'A7'?
1 | const int buttonPin = D7; // the number of the pushbutton pin
| ^~
| A7
exit status 1
'D7' was not declared in this scope; did you mean 'A7'?
I considered using A7 thinking the error message might be right but reading the pinout, AREF (A7/P0.31) is already in use.
What am I missing?
const int buttonPin = D7; // the number of the pushbutton pin
int buttonState; // the current reading from the input pin
int lastButtonState = HIGH; // the previous reading from the input pin
void setup() {
Serial.begin(115200);
delay(100);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
Serial.println("I ATE THE PIE!!");
}
}
Try:
const int buttonPin = 7;
There are probably macros defining A0 etc., but as far as I know there aren't any for D0 etc.; for those you just use the number, without the "D".
I am trying to get my Adafruit Trinket working as a keyboard. I am using the standard example code for it but it keeps giving me this compilation error.
exit status 1
'Keyboard' not found. Does your sketch include the line '#include <Keyboard.h>'?
this error keeps popping up even though i have it in my code. I have tried lots of different versions of this and messed with lots of things and it has always came up with this error.
This is my code.
#include <Keyboard.h>
const int buttonPin = 4; // input pin for pushbutton
int previousButtonState = HIGH; // for checking the state of a pushButton
int counter = 0; // button push counter
void setup() {
// make the pushButton pin an input:
pinMode(buttonPin, INPUT);
// initialize control over the keyboard:
Keyboard.begin();
}
void loop() {
// read the pushbutton:
int buttonState = digitalRead(buttonPin);
// if the button state has changed,
if ((buttonState != previousButtonState)
// and it's currently pressed:
&& (buttonState == HIGH)) {
// increment the button counter
counter++;
// type out a message
Keyboard.print("You pressed the button ");
Keyboard.print(counter);
Keyboard.println(" times.");
}
// save the current button state for comparison next time:
previousButtonState = buttonState;
}
The Keyboard.h library is for the official Arduino boards with native USB support.
For the Trinket you need to use the TrinketKeyboard.h from Adafruit.
I am trying to use my arduino to make a reaction game where it chooses a random length of time before turning the built in LED on where it waits for you to press the button then gives you a score of fast or slow, the problem is that when I try to upload it to the arduino (all settings are correct) it comes up with an error message on certain lines that are the exact same as another programme that is different bust uses the same things and works.
const int buttonPin = 4;
const int ledPin1 = 3;
const int ledPin2 = 2;
//~~~~~~~~~~##~~~~~~~~~~\\
int buttonState = 0;
int oldButtonState = 0;
int wait = 0;
bool isLoop = true;
//~~~~~~~~~~##~~~~~~~~~~\\
void setup() {
Serial.begin(9600);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(buttonPin, INPUT);
}
digitalWrite(LED_BUILTIN, LOW);
//~~~~~~~~~~##~~~~~~~~~~\\
void loop() {
// put your main code here, to run repeatedly:
randInt = random(500, 5001);
delay(randInt);
digitalWrite(LED_BUILTIN, HIGH);
while (isLoop == true){
buttonState = digitalRead(buttonPin);
if (buttonState == LOW){
isLoop = false;
}
}
wait = millis() - randInt
Serial.println(String(wait));
if (wait <= 1000){
digitalWrite(ledPin1, HIGH);
}else{
digitalWrite(ledPin2, HIGH);
}
delay(2000);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
}
the above is my code, and when I try to upload it I get the following error message:
Arduino: 1.8.5 (Windows Store 1.8.10.0) (Windows 10), Board:
"Arduino/Genuino Uno"
Reaction_Game:11: error: 'Serial' does not name a type
Serial.begin(9600);
^
Reaction_Game:12: error: expected constructor, destructor, or type
conversion before '(' token
pinMode(ledPin1, OUTPUT);
^
Reaction_Game:13: error: expected constructor, destructor, or type
conversion before '(' token
pinMode(ledPin2, OUTPUT);
^
Reaction_Game:14: error: expected constructor, destructor, or type
conversion before '(' token
pinMode(LED_BUILTIN, OUTPUT);
^
Reaction_Game:15: error: expected constructor, destructor, or type
conversion before '(' token
pinMode(buttonPin, INPUT);
^
Reaction_Game:16: error: expected declaration before '}' token
}
^
exit status 1 'Serial' does not name a type
This report would have more information with "Show verbose output
during compilation" option enabled in File -> Preferences.
this is despite the fact that this exact line:
Serial.begin(96000);
is in another programme that works perfectly (same place and everything)
could someone please help me? I have almost given up on this after finding nothing.
1 Get rid of these weird useless comments
//~~~~~~~~~~##~~~~~~~~~~\\
2 This is outside of void setup
digitalWrite(LED_BUILTIN, LOW);
3 Missing semicolon here
wait = millis() - randInt
4 Declarate randInt, put int before this line
randInt = random(500, 5001);
5 Tip 1
while (isLoop == true){
can be written as
while (isLoop) {
6 Tip 2, You dont have to stringify long
Serial.println(String(wait));
The following line is outside the void setup function:
digitalWrite(LED_BUILTIN, LOW);
You also need a semicolon in
wait = millis() - randInt
Declare int as the datatype in
randInt = random(500, 5001);
I'd like to set up a CAN network of multiple nodes using Arduino Pro Minis and MCP2515 cards. But I can't get the Receive to work.
#include <mcp_can.h>
#include <SPI.h>
long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
char msgString[128];
#define CAN0_INT 2 // Set INT to pin 2
MCP_CAN CAN0(10); // Set CS to pin 10
void setup() {
Serial.begin(115200);
// Initialize MCP2515 running at 8MHz with a baudrate of 125kb/s
// and the masks and filters disabled.
while (CAN_OK != CAN0.begin(MCP_ANY, CAN_125KBPS, MCP_8MHZ)) {
Serial.println("CAN BUS Module Failed to Initialize.");
}
Serial.println("MCP2515 Initialized Successfully!");
CAN0.setMode(MCP_NORMAL);
pinMode(CAN0_INT, INPUT); // Configuring pin for /INT input
}
void loop() {
if(!digitalRead(CAN0_INT)) { // If CAN0_INT is low, read receive buffer
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
if((rxId & 0x80000000) == 0x80000000) // Is ID standard (11 bits) or extended (29 bits)?
sprintf(msgString, "Extended ID: 0x%.8lX DLC: %1d Data:", (rxId & 0x1FFFFFFF), len);
else
sprintf(msgString, "Standard ID: 0x%.3lX DLC: %1d Data:", rxId, len);
Serial.print(msgString);
if((rxId & 0x40000000) == 0x40000000) { // Is message a remote request frame?
sprintf(msgString, " REMOTE REQUEST FRAME");
Serial.print(msgString);
} else {
for(byte i = 0; i<len; i++) {
sprintf(msgString, " 0x%.2X", rxBuf[i]);
Serial.print(msgString);
}
}
Serial.println();
}
}
However, all I get out are the error messages, including this:
Entering Configuration Mode Failure
What am I missing here?
I got the circuit to work. The 2-node CAN Bus is communicating.
I found this site and made a couple of changes:
My Arduino ProMini MISI, MISO pins were not aligned with the SI, SO pins on the MCP2515s.
I used the CAN_BUS_Shield library.
I try to build a data logger with SD card for saving sensor data. I need to reduce the power consumption as soon as the circuit is going to sleep. The problem is the power consumption of the SD card module of about 3mA. I read a lot about saving power and many do switch of the power to the SD card module with re-initializing the card when waking up. I can't achieve that. As soon as the SD card module is switched of only error messages are thrown. Can anybody give me a hint to put me on the right track? How do I re-initialize the SD card module right?
Thanks
1. Edit
Everything works fine until the first wake up. Than the code error message "Card failed, or not present" is thrown and the loop will start again without writing to SD card.
here is what I got so far:
// DHT sensor library
#include "DHT.h"
// SD card library
#include <SD.h>
// for sleep modes
#include <avr/interrupt.h>
#include <avr/power.h>
#include <avr/sleep.h>
#define DHTPIN 9
#define DHTTYPE DHT22 //DHT11, DHT21, DHT22
DHT dht(DHTPIN, DHTTYPE);
// make sure that the default chip select pin is set
const int chipSelect = 4;
int counter = 0;
int sdPower = 8;
volatile int sleepcounter = 0; // count sleep cycles
void setup() {
pinMode(sdPower, OUTPUT);
// output, even if you don't use it to ensure proper SD library working:
pinMode(10, OUTPUT);
digitalWrite(sdPower, LOW);
watchdogOn(); // switch on Watchdog timer
ADCSRA = ADCSRA & B01111111; // switch off ADC, ADEN bit7 zu 0
ACSR = B10000000; // switch off analog Comparator, ACD bit7 to 1
DIDR0 = DIDR0 | B00111111; // switch off digital input buffer, analog input pins 0-5 to 1
dht.begin();
}
// -------------------------------------- LOOP ---------------------------------
void loop() {
Serial.begin(9600);
// ------------------------- initialize SD card -------------------------
digitalWrite(sdPower, HIGH);
Serial.println("Start of recording");
delay(500);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
delay(100);
// don't do anything more:
return;
}
Serial.println("card initialized.");
// --------------------------------------------------------------------------
float humidity = dht.readHumidity(); //measure humidity
float temp = dht.readTemperature(); //measure temp
// make a string for assembling the data to log:
String dataString = "";
// ------------------------- read sensor and store in string -----------------------------
// check for valid number, throw error for NaN (not a number)
if (isnan(temp) || isnan(humidity)) {
Serial.println("no read for DHT22");
}
else {
dataString += "MP-";
dataString += String(counter);
dataString += ",";
dataString += String(temp);
dataString += ",";
dataString += String(humidity);
// ------------------------- open SD card and write values -----------------------------
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
delay(1000);
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close(); //dataFile.sync() doesn't change something
delay(500);
// print to the serial port too:
Serial.println(dataString);
delay(500);
counter = counter + 1;
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
//-------------------------------------- sleep mode activation ----------------------------
// Stay awake for 0.5 second, then sleep.
delay(500);
digitalWrite(sdPower, LOW);
delay(500);
pwrDown(5); // go to sleep for (x) sec.
}
}
// some methods for sleep mode are not shown
The often used SD.hlibrary is not able to manage powered down SD card modules. After the power is taken away the code will throw an error. The card can't get re-initialized.
I used the SdFat.h instead and it works just perfect. SD cards draw a lot of current. To switch the SD card module a MOSFet is recommended.