I am executing a block of code every certain period of time. The time is indicated by the value of the potentiometer. Everything works well. But in some parts of the potentiometer it is as if the value were 0 or a very low number that makes the code block run continuously.
Here the piece of code:
const int p = A0;
unsigned long t = 0;
void start(){
Serial.begin(9600);
}
void loop(){
if(millis() > t+(analogRead(p)*100)){
t = millis();
Serial.println("Something...");
}
}
Your start() function is wrong, it should be setup() and should include pin mode declaration.
void setup(){
Serial.begin(9600);
pinMode(p, INPUT);
}
Related
I want to count milliseconds on my Pico to make nonblocking code in C++.
Arduino has the millis() function, but after looking though all the example programs and scouring the internet I couldn't find anything like that for the Pico using C++.
This is the BlinkWithoutDelay sketch from the Arduino examples that shows what I want to do:
const int ledPin = LED_BUILTIN;// the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
// 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 previousMillis = 0; // will store last time LED was updated
// constants won't change:
const long interval = 1000; // interval at which to blink (milliseconds)
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
}
void loop() {
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the difference
// between the current time and last time you blinked the LED is bigger than
// the interval at which you want to blink the LED.
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// 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);
}
}
I decided to go with a repeating timer as suggested by some comments and the examples that used it really difficult to understand for me. So I'll try to explain it the best I can.
*Note that I don't really understand some of the values being passed into functions but I know enough to make this work. Please feel free to correct me
#include "pico/stdlib.h"
bool led_state;
//Function Called by the timer
bool timer_callback( repeating_timer_t *rt )
{
//Blinks the LED
led_state = !led_state;
gpio_put(25, led_state);
return (true);
}
int main(){
//From what I can tell this creates an identifier for the timer
static repeating_timer_t timer;
stdio_init_all();
//GPIO setting (GPIO_25 is the LED pin on the pico)
gpio_init(25);
gpio_set_dir(25, GPIO_OUT);
//Sets a repeating timer to call a function every 1000 milliseconds
//add_repeating_timer_ms( int interval_in_milliseconds, ?&function to call?, ???, ?timer identifier? );
add_repeating_timer_ms( 1000, &timer_callback, NULL, &timer );
while(true){
//Blinks LED briefly at a different interval than the repeating timer
//This is to show that my code isn't blocked
gpio_put(25, !led_state);
sleep_ms(50);
gpio_put(25, led_state);
sleep_ms(450);
}
return 0;
}
I'm trying to interrupt the serial monitor to execute a certain function and continue with the previous function after completion of the interrupted function, my code is as bellow
#define red1 13
#define amber1 12
#define green1 11
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(red1, OUTPUT);
pinMode(amber1, OUTPUT);
pinMode(green1, OUTPUT);
}
void Delay(int sec) {
for(int i=0;i<sec;i++){
if (Serial.available())
{
Button();
break;
}
delay(1);
}
}
void Light()
{
digitalWrite(red1,HIGH);
Delay(5000);
digitalWrite(red1,LOW);
digitalWrite(amber1,HIGH);
Delay(5000);
digitalWrite(amber1,LOW);
}
void Button()
{
digitalWrite(red1,LOW);
digitalWrite(amber1,LOW);
digitalWrite(green1,HIGH);
delay(1000);
digitalWrite(green1,LOW);
}
void loop()
{
Light();
}
Whenever I enter a value in serial monitor Button() function is executed, it should end as soon as function completes due to a break, but this function keeps repeating continuously? How can I fix this such that whenever a serial monitor is interrupted, Button() is executed onetime and then continues with Light().
I tried using a break but the serial interface buffer is not cleared so its keep continuing this function also it exits if loop, so I tried using return
void Delay(int sec) {
for(int i=0;i<sec;i++){
if (Serial.available())
{
Button();
return;
}
delay(1);
}
}
the same thing happens
the function doesn't exit it keeps repeating the same since the serial interface is not cleared from an earlier encounter, I could've used serial.available in void loop() but whenever Arduino is at delay function its sleeping so it doesn't read a serial interrupter, so I created separate Delay()
so how can I change the code to end this function and go back to Light()
but the serial interface buffer is not cleared
Then you have to clear it:
while (Serial.available())
Serial.read(); // remove 1 character
I am writing a driver for an anemometer, which is based on the Hall effect and I am trying to use hardware interrupts to monitor every rotation of the sensor.
By doing so I found an issue that I am not able to solve and is expressed by the following code:
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
const int interruptPin = D5;
volatile uint16_t lastDebounceTime = 0;
volatile int numberOfInterrupts = 0;
uint16_t previousUpdate = 0;
const uint16_t updateInterval = 5000;
void handleInterrupt();
void countInterrupts();
void setup() {
Serial.begin(115200);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), handleInterrupt, FALLING);
}
void loop() {
if ((millis() - previousUpdate) > updateInterval) {
Serial.println(".................");
uint16_t currentMillis = millis();
Serial.println(currentMillis);
Serial.println(previousUpdate);
Serial.println(currentMillis - previousUpdate);
if (currentMillis - previousUpdate < updateInterval) Serial.println("This line shoudn't be executed, but it actually is!!");
Serial.println(".................");
previousUpdate = millis();
}
}
void handleInterrupt() {
numberOfInterrupts++;
}
At first the code run as it was supposed to, but after a while it simply stop checking the conditionals if ((millis() - previousUpdate) > updateInterval) and if (currentMillis - previousUpdate < updateInterval) Serial.println("This line shoudn't be executed, but it actually is!!"); and then the at each iteration the ESP8266 sends data to the serial interface. millis() is working the interrupt function is working as well.
I'm really trying to understand and solve this problem, it would be awesome if somebody could help me with it!
Thank you!
I am Working on an Arduino mini project. its to design an automatic room light controller using IR modules and arduino UNO R3. Before coming to my question i'd explain it a bit .Its like an arduino is connected to an IR module and a counter keeps count of people entering the room, when the room is empty light turns off automatically.But my Code isnt working the way im expecting it to work. . given below is the code for my mini project.
#define x 14 // x sensor 1
#define y 19 // y sensor 2
#define relay 2 // relay for output
int count = 0; // initialisation
// void IN() //{ count ++;} // void OUT() // { cout--; // }
void setup()
{
// put your setup code here, to run once:
pinMode(x, INPUT);
pinMode(y, INPUT);
pinMode(relay, OUTPUT);
}
void loop()
{
// put your main code here, to run repeatedly:
if(digitalRead(x)) //IN();
{
count++;
}
if (digitalRead(y)) // OUT();
{
count--;
}
if (count <= 0) {
digitalWrite(relay, LOW);
delay(20);
}
else
digitalWrite(relay, HIGH);
}
The problem with my code is that the transmitter and receiver sense interference both at the same time resulting in turning of the light while the person is still inside the room.As im new to arduino need your inputs.
I am try to do a write to serial using a NANO. This is my current code
#include "HardwareSerial.h"
long previousMillis = 0;
long interval = 2000;
void setup() {
// put your setup code here, to run once:
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
HardwareSerial serial = Serial;
serial.write("hello");
}
}
However when I monitor the serial using a serial monitor I only get
he
for each serial write. Please help
I have changed the class type in my method from hardwareserial to stream &serial this is now working the way it should. Thanks for the help