arduino "error: expected unqualified-id before '=' token" - compilation

I have my sketch(simplified version) as shown:
int sensorPin = 0; // select the input pin for the photocell
int ledPin = 11; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
int auto = 0;
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
Serial.print("Digital reading = ");
Serial.println(sensorValue); // the raw analog reading
// turn the ledPin on
if (auto > 1)
if (sensorValue < 700){
digitalWrite(ledPin, LOW);
}else{
digitalWrite(ledPin,HIGH);
}
// stop the program for <sensorValue> milliseconds:
delay(10);
}
however it shows the error shown and highlights this line. int auto = 0; I have tried everything in my power, such as moving it, and changing it to a Boolean, but i can't get it to work.

"auto" is a keyword meaning, that a variable is automatically given the data type of its initialiser. Replace it with a non-reserved work and it will compile.
This IDE does not syntax it. Where np++ and others does.

Related

Issues Calling Functions from Freertos Task (ESP32)

Currently I am having issues with a program running a freertos program. The purpose of the program is to control a stepper motor as well as an led. Implementing the motor control without microstepping does not have any issues as the two tasks take no parameters and call no functions.
However, when I introduce microstepping which requires two nested functions to be called by the move_routine task, the program will not do anything (no led flashing, no motor turning) when it did before. Does anyone have any solutions for this or any reasons on why this shouldn't work? From what I can see it should be fine to call a function from a freertos task.
#include <Arduino.h>
#include <Stepper.h>
/*================PIN DEFINITIONS================*/
#define LEDC_CHANNEL_0 0
#define LEDC_CHANNEL_1 1
#define LEDC_CHANNEL_2 2
#define LEDC_CHANNEL_3 3
const int A1A = 14;
const int A1B = 27;
const int B1A = 26;
const int B2A = 25;
const int ledPin = 33;
/*================VARIABLE DEFINITIONS================*/
int stepnumber = 0;
int Pa; int Pb;
const int stepsPerRev = 200;
Stepper myStepper(stepsPerRev, 14,27,26,25);
/*================Function Definitions================*/
//Analogwrite using LEDC capabilities
void ledcAnalogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 255) {
//calculation of duty cycle
uint32_t duty = (4095/valueMax)*min(value, valueMax);
ledcWrite(channel,duty);
}
void move(int stepnumber, int MAXpower, int wait) {
Pa = (sin(stepnumber*0.098174)*MAXpower);
Pb = (cos(stepnumber*0.098174)*MAXpower);
if (Pa>0)
{
ledcAnalogWrite(LEDC_CHANNEL_0,Pa);
ledcAnalogWrite(LEDC_CHANNEL_1,0);
}
else
{
ledcAnalogWrite(LEDC_CHANNEL_0,0);
ledcAnalogWrite(LEDC_CHANNEL_1,abs(Pa));
}
if (Pb>0)
{
ledcAnalogWrite(LEDC_CHANNEL_2,Pb);
ledcAnalogWrite(LEDC_CHANNEL_3,0);
}
else
{
ledcAnalogWrite(LEDC_CHANNEL_2,0);
ledcAnalogWrite(LEDC_CHANNEL_3,abs(Pb));
}
}
void move_routine(void *parameters) {
while(1) {
for (int i=0; i<3199; i++)
{
stepnumber++;
move(stepnumber,255,250);
}
vTaskDelay(3000/portTICK_PERIOD_MS);
for (int i=0; i<1599; i++)
{
stepnumber--;
move(stepnumber,255,1000);
}
vTaskDelay(3000/portTICK_PERIOD_MS);
}
}
void led_task(void * parameters){
while(1){
digitalWrite(ledPin, HIGH);
vTaskDelay(500/portTICK_PERIOD_MS);
digitalWrite(ledPin, LOW);
vTaskDelay(500/portTICK_PERIOD_MS);
}
}
void setup(){
myStepper.setSpeed(60);
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
xTaskCreatePinnedToCore(
move_routine,
"Move Routine",
8192,
NULL,
1,
NULL,
1
);
xTaskCreatePinnedToCore(
led_task,
"LED Task",
1024,
NULL,
1,
NULL,
1
);
}
void loop(){
}
Expected to see flashing led and motor turning but nothing witnessed
Your two tasks work as it were defined, the problem is your ledcAnalogWrite() function where you are calling ledcWrite() function with nowhere in your code initialise the LEDC with ledcSetup() and ledcAttachPin().
Read the documentation on LED Control(LEDC).

Incompatible types in assigmnet of int to char[16] error - Arduino UNO

I've created an array of structs but I'm getting the error written on the title of this question. I'm still new to this so I was wondering if I could get some help.
Code:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
lcd.begin(16, 2);
i = 0;
}
#define LIMIT 27
struct protocol {
char create[16];
char character;
int values;
int minimum;
int maximum;
};
struct protocol channels[LIMIT];
int i;
void create_channels() {
if (Serial.available() > 0) {
Serial.print("Enter the channel description");
channels[i].create = Serial.read();
Serial.print("Enter the starting character: ");
channels[i].character = Serial.read();
if (i == LIMIT) {
for (i = 0; i < LIMIT; i++)
{
Serial.println(channels[i].create);
Serial.println(channels[i].character);
}
i = 0;
}
}
}
Error:
cw.ino:24:38: error: incompatible types in assignment of 'int' to 'char [16]'
channels[i].create = Serial.read();

convert analog read to range to time

I'm trying to blink a led with analog sensor between 8 and 40 times a minutes
I have try this code but I realize that I had to convert the valorSensor to time. How is the way to do it?
int led = 13;
int pinAnalogo = A0;
int analogo;
int valorSensor;
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
analogo = analogRead(pinAnalogo);
valorSensor = map(analogo, 0, 1023, 4, 80);
digitalWrite(led, HIGH);
delay(valorSensor);
digitalWrite(led, LOW);
delay(valorSensor);
}
The problem here is not so much the code but biology. To see a blinking (and not just a flicker you will need times from 250ms and up) 24 frames/sec are perceived as motion so to get a "blink" you might start with 4 frames/sec (=250ms)
So my proposal is a blink without delay as function and a tuning parameter (blinkSpeedMultiplyer) for testing
/* Blink without Delay as function
Turns on and off a light emitting diode (LED) connected to a digital pin,
without using the delay() function. This means that other code can run at the
same time without being interrupted by the LED code.*/
// constants won't change. Used here to set a pin number:
const int blinkSpeedMultiplyer = 50; // very low
const int ledPin = 13;
const int pinAnalogo = A0;
// Variables will change:
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long startTime = millis(); // will store last time LED was updated
unsigned long blinkTime; // interval at which to blink (milliseconds)
int analogo;
int valorSensor;
int ledState = LOW; // ledState used to set the LED
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
analogo = analogRead(pinAnalogo);
valorSensor = map(analogo, 0, 1023, 4, 80);
blinkLed(valorSensor); // call the function
}
// Function to blink without blocking delay
void blinkLed(int inValue) {
blinkTime = blinkSpeedMultiplyer * inValue;
if (millis() - startTime >= blinkTime) {
// save the last time you blinked the LED
startTime = millis();
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}

Attiny85 with ArduinoUno for I2c comm

I am working on attiny85 for I2C communication. I have gone through different libraries already like Wire.h, TinyWire.h, tinyWireM.h, tinyWireS.h.
In the start I want to send some byte of data through I2C comm and tried to scope the pin with oscilloscope but its not giving me the appropriate results. Looking on the internet about different ways to make attiny85 work with I2c is really heartless and I could not achieve the task. Finally, I tried to make attiny85 as master and arduino Uno as slave as it was spare in my box.
I tried to make attiny85 as master and send data to arduino and looks the output on serial monitor but its showing zero.
For the reference, the master and slave codes are attached and my task is just simple to check on serial.
Attiny85 as Master
#include <TinyWireM.h>
void setup()
{
TinyWireM.begin();
}
void loop()
{
TinyWireM.begin();
TinyWireM.beginTransmission(0x08);
TinyWireM.send(0x99);
int Byte1 = TinyWireM.endTransmission();
delay(1000);
}
Arduino as Slave
#include <Wire.h>
const byte add = 0x08;
int byte1;
void setup()
{
Wire.begin(add);
Wire.onReceive(receiveEvent);
Serial.begin(9600);
}
void loop()
{
Serial.println ("Data receiving");
Serial.println(byte1);
delay(1000);
}
void receiveEvent(int bytes)
{
byte1 = Wire.read();
}
But I am not able to get the output on serial monitor of arduino.
What am i doing wrong here?
I have used Atiny85 as a slave using TinyWireS lib (https://github.com/nadavmatalon/TinyWireS) some time back and it worked fine.
Below were the pin configurations
ATtiny85 pin 5 with Arduino Uno A4 and
ATtiny85 pin 7 with Arduino Uno A5
Below are my codes
Atiny.
#include "TinyWireS.h"
const byte SLAVE_ADDR = 100;
const byte NUM_BYTES = 4;
volatile byte data = { 0, 1, 2, 3 };
void setup() {
TinyWireS.begin(SLAVE_ADDR);
TinyWireS.onRequest(requestISR);
}
void loop() {}
void requestISR() {
for (byte i=0; i<NUM_BYTES; i++) {
TinyWireS.write(data[i]);
data[i] += 1;
}
}
Uno.
#include <Wire.h>
const byte SLAVE_ADDR = 100;
const byte NUM_BYTES = 4;
byte data[NUM_BYTES] = { 0 };
byte bytesReceived = 0;
unsigned long timeNow = millis();
void setup() {
Serial.begin(9600);
Wire.begin();
Serial.print(F("\n\nSerial is Open\n\n"));
}
void loop() {
if (millis() - timeNow >= 750) { // trigger every 750mS
Wire.requestFrom(SLAVE_ADDR, NUM_BYTES); // request bytes from slave
bytesReceived = Wire.available(); // count how many bytes received
if (bytesReceived == NUM_BYTES) { // if received correct number of bytes...
for (byte i=0; i<NUM_BYTES; i++) data[i] = Wire.read(); // read and store each byte
printData(); // print the received data
} else { // if received wrong number of bytes...
Serial.print(F("\nRequested ")); // print message with how many bytes received
Serial.print(NUM_BYTES);
Serial.print(F(" bytes, but got "));
Serial.print(bytesReceived);
Serial.print(F(" bytes\n"));
}
timeNow = millis(); // mark preset time for next trigger
}
}
void printData() {
Serial.print(F("\n"));
for (byte i=0; i<NUM_BYTES; i++) {
Serial.print(F("Byte["));
Serial.print(i);
Serial.print(F("]: "));
Serial.print(data[i]);
Serial.print(F("\t"));
}
Serial.print(F("\n"));
}

Measuring Time of Flight with Arduino Uno

I am trying to implement a master/slave setup to determine the time of flight between two Arduino Uno boards and ultimately use that as a measure of distance between the two. Using the standard 16MHz crystal and the APC220 from DFRobot communicating between the two is easy but getting a time of flight reading is where I get stuck.
I use the following code on the master side to send the first signal and receive the echo from the slave:
// set pins:
const int switchPin = 3; // pin number of the switch
// variables:
int switchState = 0; // variable for reading switch status
int iDisplay = 1;
unsigned long start, finished, elapsed;
// initialize
void setup()
{
// initialize switch pin as input:
pinMode(switchPin, INPUT);
// initialize serial wireless communication:
Serial.begin(9600);
// read initial state of switch
switchState = digitalRead(switchPin);
}
void displayResult()
{
float h,m,s,ms;
unsigned long over;
elapsed=finished-start;
h=int(elapsed/3600000);
over=elapsed%3600000;
m=int(over/60000);
over=over%60000;
s=int(over/1000);
ms=over%1000;
Serial.print("Raw elapsed time: ");
Serial.println(elapsed);
Serial.print("Elapsed time: ");
Serial.print(h,0);
Serial.print("h ");
Serial.print(m,0);
Serial.print("m ");
Serial.print(s,0);
Serial.print("s ");
Serial.print(ms,0);
Serial.println("ms");
Serial.println();
}
// program loop
void loop()
{
if (Serial.available()>0 && iDisplay == 1)
{
finished=millis();
displayResult();
iDisplay = 0;//Only once
}
// read switch state and print line if state has changed
switch (digitalRead(switchPin)) { // read pin status
case HIGH:
if (switchState == LOW)
{ // check if message has to be sent
start=millis();
delay(200); // for debounce
iDisplay = 1;//Only once
Serial.println(100); // send message about switch
switchState = HIGH; // message has been sent
}
break;
case LOW:
if (switchState == HIGH)
{ // check if message has to be sent
start=millis();
delay(200); // for debounce
iDisplay = 1;//Only once
Serial.println(100); // send message about switch
switchState = LOW; // message has been sent
}
break;
}
}
And the following code for the slave:
// variables:
int intTime = 0;
// initialize
void setup()
{
// initialize serial wireless communication:
Serial.begin(9600);
}
// program loop
void loop()
{
if (Serial.available()>0)
{
intTime = Serial.parseInt();
if (intTime > 1)
{
Serial.println(intTime);
}
intTime = 0;
}
}
Yet this only returns the 200 Milliseconds from the debounce delay, can this be done with the Arduino? Am I getting the math or the code wrong?

Resources