I have looked at previous posts and can't find an answer to my question, here is my code:
const int buttonPin3 = 4;
const int ledPin3 = 11;
int buttonState3 = 0;
{
pinMode(ledPin3, OUTPUT);
pinMode(buttonPin3, INPUT);
}
{
buttonState3 = digitalRead(buttonPin3);
if (buttonState3 == HIGH) {
digitalWrite(ledPin3, HIGH);
} else {
digitalWrite(ledPin3, LOW);
}
}
This isn't all of my code, but only the part where the error shows up
just before the buttonState3 = digitalRead(buttonPin3); there is a { that is where the error shows for me.
In Arduino land, you generally need to provide an setup function which is called once when the program starts, and a loop function which will be called continuously while the program is running.
Your code appears to be missing the actual function declaration parts, instead opting for code blocks at file level, which causes that exact error:
testprog.cpp:6:5: error: expected unqualified-id before ‘{’ token
{
^
You'll need something like the following to get it to compile:
const int buttonPin3 = 4;
const int ledPin3 = 11;
int buttonState3 = 0;
void setup() { // << note here
pinMode(ledPin3, OUTPUT);
pinMode(buttonPin3, INPUT);
}
void loop() { // << and here
buttonState3 = digitalRead(buttonPin3);
if (buttonState3 == HIGH) {
digitalWrite(ledPin3, HIGH);
} else {
digitalWrite(ledPin3, LOW);
}
}
Related
I am attempting to upload my code to the ATTiny85, but whenever I do, I get this error.
Here is the error:
error: ISO C++ forbids declaration of 'str' with no type [-fpermissive]
void sendPronto(const __FlashStringHelper *str, unsigned int times = 1U);
Here is the code:
#include <IRremote.h>
int IRpin = 1;
IRrecv IR(IRpin);
//Motor 1 (Right) Backward Pin
const byte MOTOR1_BWD = 2;
//Motor 1 (Right) Forward Pin
const byte MOTOR1_FWD = 3;
decode_results cmd;
int speedpin = 5;
const byte spd = 255;
void stop() {
digitalWrite(MOTOR1_BWD, LOW);
digitalWrite(MOTOR1_FWD, LOW);
}
void back() {
digitalWrite(MOTOR1_BWD, HIGH);
digitalWrite(MOTOR1_FWD, LOW);
analogWrite(speedpin, spd);
}
void forward() {
digitalWrite(MOTOR1_BWD, LOW);
digitalWrite(MOTOR1_FWD, HIGH);
analogWrite(speedpin, spd);
}
void setup() {
IR.enableIRIn();
pinMode(MOTOR1_BWD, OUTPUT);
pinMode(MOTOR1_FWD, OUTPUT);
digitalWrite(MOTOR1_BWD, LOW);
digitalWrite(MOTOR1_FWD, LOW);
stop();
}
void loop() {
IR.resume();
if (cmd.value == 0xFF906F) {
forward();
}
if (cmd.value == 0xFFA25D) {
stop();
}
if (cmd.value == 0xFFE01F) {
back();
}
}
I copy pasted your code and it compiled just fine for the ATTiny85. Try redownloading that library using the library manager in the IDE
I am trying to merge 3 separate DataLogger codes in to one. they work fine individually; however I can get all 3 to work together, can anyone help?
#include <SPI.h>
#include <SD.h>
#include "RTClib.h"
RTC_DS1307 rtc;
int sensor = A0;
int sensorInput;
double temp;
int led = 13;
int sensorMotion = A1;
int state = LOW;
int val = 0;
const char* filename = "DataLogger.csv";
File file;
void setup() {
Serial.begin(9600);
#ifndef ESP8266
while (!Serial);
#endif
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
pinMode(10, OUTPUT);
if (!SD.begin(10)) {
Serial.println("Error : Push the reset button");
for (;;);
}
file = SD.open(filename, FILE_WRITE);
if (file.size() == 0) {
file.println("Brightness value per seconds");
file.flush();
}
}
DateTime time = rtc.now();
val = digitalRead(sensor); // read sensor value
if (val == HIGH) { // check if the sensor is HIGH
digitalWrite(led, HIGH); // turn LED ON
delay(500); // delay 100 milliseconds
if (state == LOW) {
Serial.println(String("Motion detected
")+","+time.timestamp(DateTime::TIMESTAMP_TIME)+","+time.timestamp(DateTime::TIMESTAMP_DATE));
file.println(String("Motion detected
")+","+time.timestamp(DateTime::TIMESTAMP_TIME)+","+time.timestamp(DateTime::TIMESTAMP_DATE));
file.flush();
state = HIGH; // update variable state to HIGH
}
}
else {
digitalWrite(led, LOW); // turn LED OFF
delay(500); // delay 200 milliseconds
if (state == HIGH){
Serial.println("Motion stopped!");
state = LOW; // update variable state to LOW
}
}
}
void loop() {
measure();
delay(1000);
DateTime time = rtc.now();
sensorInput = analogRead(A0);
temp = (double)sensorInput / 1024;
temp = temp * 5;
temp = temp - 0.5;
temp = temp * 100;
delay(1000);
Serial.println(String(temp)+","+time.timestamp(DateTime::TIMESTAMP_TIME)+","+time.timestamp(DateTime::TIMESTAMP_DATE));
file.println(String(temp)+","+time.timestamp(DateTime::TIMESTAMP_TIME)+","+time.timestamp(DateTime::TIMESTAMP_DATE));
file.flush();
}
void measure() {
DateTime time = rtc.now();
int lightvalue = analogRead(A0);
Serial.println(String(lightvalue)+","+time.timestamp(DateTime::TIMESTAMP_TIME)+","+time.timestamp(DateTime::TIMESTAMP_DATE));
file.println(String(lightvalue)+","+time.timestamp(DateTime::TIMESTAMP_TIME)+","+time.timestamp(DateTime::TIMESTAMP_DATE));
file.flush();
}
I want to implement the function interrupt () but I don't know exactly how..In this case there is 2 for loops which can be seen in the code:I want whenever one of the 2 buttons is pressed the process inside the loop to be interrupted immediately:
void loop() {
int brightButton = digitalRead(K1);
int ldrStatus = analogRead(ldrPin);
if (brightButton == LOW && ldrStatus >= 200)
{
for (int i = 0; i < 10; i++)
{
digitalWrite(greenLed, HIGH);
tone(buzzer,400);
delay(500);
noTone(buzzer);
delay(500);
}
}
else {
digitalWrite(greenLed, LOW);
}
int tempButton = digitalRead(K2);
int valNTC = analogRead(NTC);
if (tempButton == LOW && valNTC > 512)
{
for (int i = 0; i <10; i++)
{
digitalWrite(redLed, HIGH);
tone(buzzer,450);
delay(300);
noTone(buzzer);
delay(1000);
}
}
else {
digitalWrite(redLed, LOW);
}
}
Example code from the Arduino manual:
https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/
const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}
void loop() {
digitalWrite(ledPin, state);
}
void blink() {
state = !state;
}
Note that this will interrupt the for loop and return to it once the interrupt service routine is finished.
If you want to abort the for loop check the pin state in every loop cycle and break if you want to leave the for loop or return if you want to leave loop().
Of course this is not "immediately".
I am not getting why this is not working. I was researching on fixes and they say to add that "if" block in the void loop. Well, that was always in the void loop. Can you tell me the meaning and fix? Also, if there are more errors, please notify me so I can fix it. My code can be a little disorganized.
My code:
#include <LiquidCrystal.h>
LiquidCrystal lcd (7,8,9,10,11,12);
int pinDHT11 = 2;
SimpleDHT11 dht11;
void setup() {
Serial.begin(9600);
lcd.begin(16,2);
}
void loop() {
}
if (dht11.read(pinDHT11, &temperature, &humidity,data))
Serial.print("Read DHT11 failed");
return;
byte temperature = 0;
byte humidity = 0;
byte data[40] = {0};
if(dht11.read (pinDHT11, &temperature, &humidity, data))
return;
Serial.print("Read DHT11 failed");
Serial.println("=================================");
Serial.println("Sample DHT11...");
// read with raw sample data.
byte temperature = 0;
byte humidity = 0;
byte data[40] = {0};
}
Serial.print("Sample RAW Bits: ");
for (int i = 0; i < 40; i++) {
Serial.print((int)data[i]);
if (i > 0 && ((i + 1) % 4) == 0) {
Serial.print(' ');
}
}
Serial.println("");
Serial.print("Sample OK: ");
Serial.print((int)temperature); Serial.print("*C,");
Serial.print((int)humidity); Serial.println("%");
lcd.setCursor(0,0);
lcd.print((int)temperature);
lcd.setCursor(0,1);
lcd,print((int)humidity);
// DHT11 sampling rate is 1HZ.
delay(1000);
Your void loop is empty.
void loop() {
}
The error message is caused because you may not have code outside of functions. The only thing that is allowed outside functions are declarations.
There is a } without a matching {.
You also have two unconditional return statements.
, instead of .
lcd,print((int)humidity);
new here! been recommended many times to come here for help so here I am.
I'm supposed to write a program that allows me to change the rate of a blinking LED light through the comm port. I'm sure this is easy but I've honestly got no clue as I am behind in this class.
anything would help really, i honestly want to learn how to do this, not just come here and get the answer.
thanks in advanced!
// global variables
#include <EEPROM.h>
unsigned long ms_runtime;
int state;
// possible values 0 -> 1 -> 2 -> 3 -> 0
int one_ms_timer;
// define all timers as unsigned long (they are incremented every 100ms = 0.1s)
unsigned long timer1;
unsigned long button_dbnc_tmr = 0;
// timer1 is used for blinking LED
const int LED1 = 13;
// function prototypes
void read_memory(void);
void update_memory(void);
void comm_control(void);
void led_control(void);
void turnoff(void);
void flash_1s(void);
void flash_2s(void);
void flash_3s(void);
void timers(void);
void setup()
{
read_memory();
pinMode(LED1, OUTPUT);
Serial.begin(9600);
//initialize uart
}
void loop()
{
static bool allow_change;
static int counter;
timers();
comm_control();
led_control();
}
void led_control()
{
switch (state)
{
case 0:
turnoff();
break;
case 1:
flash_1s();
break;
case 2:
flash_2s();
break;
case 3:
flash_3s();
break;
}
}
void turnoff()
{
digitalWrite(LED1, LOW);
}
void flash_1s()
{
if (timer1 < 10)
digitalWrite(LED1, HIGH);
else
{
digitalWrite(LED1, LOW);
if (timer1 >= 20)
timer1 = 0;
}
}
void flash_2s()
{
if (timer1<20)
digitalWrite(LED1, HIGH);
else
{
digitalWrite(LED1, LOW);
if (timer1 >= 30)
timer1 = 0;
}
}
void flash_3s()
{
if (timer1<30)
digitalWrite(LED1, HIGH);
else
{
digitalWrite(LED1, LOW);
if (timer1 >= 40)
timer1 = 0;
}
}
void read_memory()
{
timer1 = EEPROM.read(one_ms_timer);
timer1++;
EEPROM.write(one_ms_timer, timer1);
Serial.begin(9600);
}
void update_memory()
{
EEPROM.update(timer1, one_ms_timer);
}
void comm_control(void);
{
char comm_reveier = serial_read;
if (
)
}
void timers(void)
{
if (millis() > (ms_runtime + 1))
{
ms_runtime = ms_runtime + 1;
one_ms_timer++;
}
else if (ms_runtime > millis())
ms_runtime = millis();
if (one_ms_timer > 99) //every 100 ms
{
one_ms_timer = 0;
button_dbnc_tmr++;
timer1++;
}
}
Load the Standard Firmata library on your Arduino board. Then use a library of your choice to build your comm prog. An overview of these can be found here.
Assuming you are using the Arduino IDE, this code sample should give you the general idea of what you need to do:
// pins for the LEDs:
const int ledPin = 13;
// Default blink rate
int rate = 1000;
void setup() {
// initialize serial:
Serial.begin(9600);
// make the pins outputs:
pinMode(ledPin, OUTPUT);
}
void loop() {
// if there's any serial available, read it:
while (Serial.available() > 0) {
// look for the next valid integer in the incoming serial stream:
int rate = Serial.parseInt();
}
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(rate); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
delay(rate); // wait for a second
}