im doing a tachometer with arduino uno and this is my code:
int ledPin = 13;
volatile byte rpmcount;
unsigned int rpm;
unsigned long timeold;
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
void rpm2()
{
rpmcount++;
}
void setup()
{
lcd.begin(16, 2);
attachInterrupt(0, rpm2, FALLING);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
rpmcount = 0;
rpm = 0;
timeold = 0;
}
void loop()
{
delay(1000);
detachInterrupt(0);
rpm = 30*1000/(millis() -Timeold)*rpmcount;
timeold = millis();
rpmcount = 0;
lcd.clear();
lcd.print("RPM=");
lcd.print(rpm);
attachInterrupt(0, rpm2, FALLING);
}
When i turn on arduino e starts giving me random numbers on rpm and i have no idea why. Even if i remove the IR it keeps showing the random numbers
Related
I have an arduino with 3 sensors connected and need to send the information I'm getting from the said sensors to another arduino and have it displayed on a connected lcd. I tried to go about this by changing the float values I'm getting into a string, sending the string to the other arduino and displaying that on the lcd. But when I try to execute this, I get an error saying :
" In function 'void receiveEvent(int)':
16:24: error: incompatible types in assignment of int to char [7] "
#include <Wire.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
int sensePin = A0;
int sensorInput;
int lightPin = A1;
int lightInput;
int gasPin = A2;
int gasInput;
int buzzer = 13;
int b_state = 3;
float temp;
float light;
char t_buffer[7];
int state = 0;
void setup()
{
Wire.begin();
pinMode(A0, INPUT);
pinMode(A1,INPUT);
pinMode(buzzer, OUTPUT);
pinMode(A2, INPUT);
pinMode(buzzer, OUTPUT);
pinMode(b_state, INPUT_PULLUP);
Serial.begin(9600);
lcd.begin(16, 2);
attachInterrupt(digitalPinToInterrupt(3), buttons, FALLING);
}
void loop()
{
gasInput = analogRead(gasPin);
sensorInput = analogRead(sensePin);
lightInput = analogRead(lightPin);
temp = (float)sensorInput / 1024;
temp = temp * 5;
temp = temp - 0.498;
temp = temp * 100;
light = ((float)lightInput - 26) / 8.97;
dtostrf(temp, 6, 2, t_buffer);
Serial.print("Current Temperature: ");
Serial.println(temp);
Serial.print("Current Light: ");
Serial.println(light);
Serial.print("Gas (conc.?): ");
Serial.println(gasInput);
Wire.beginTransmission(9);
Wire.write(t_buffer, 7);
Wire.endTransmission();
delay(100);
if (state == 0){
lcd.setCursor(0, 0);
lcd.print("Temp (C): ");
lcd.setCursor(0,1);
lcd.print(t_buffer);
}
else {
lcd.setCursor(0,0);
lcd.print("Light Intensity: ");
lcd.setCursor (0,1);
lcd.print(light);
}
if (gasInput > 800)
digitalWrite(buzzer, HIGH);
else
digitalWrite(buzzer, LOW);
}
void buttons()
{
lcd.clear();
state = !state;
}
#include <Wire.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
float temp;
char t_buffer[7];
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
Wire.begin(9);
Wire.onReceive(receiveEvent);
}
void receiveEvent(int bytes) {
t_buffer = Wire.read();
//temp = t_buffer.toFloat();
}
void loop() {
lcd.setCursor(0, 0);
lcd.print("Temp (C): ");
lcd.setCursor(0,1);
lcd.print(t_buffer);
}
Also I have 2 separate float values that I need to send and I'm thinking I could time the transmissions properly and send them both one after the other. Is this a good way of doing this or is there an alternative way?
The circuit
This may not be a direct answer to your question but it may help you figure it out.
t_buffer is a pointer, while t_buffer[0] is the first character in your buffer. The Wire.read returns a char and not a pointer.
If the explanation above does not make sense you may want to read on arrays.
Having said that, since you main loop is almost empty you can use the suggestion by Juraj and use Wire.readBytes(t_buffer , bytes) this way you do not have to worry about indexing through the buffer.
I would strongly suggest that you put text data in t_buffer and test your application before trying to send a float.
I am an aeronautical student, new to the coding environment.
I'm currently working on a GPS neo 6m module with Arduino mega 2560, where I wanted to save the current location upon pressing the push button. Which function is to be used to save the location by pressing the push button.
Here is what I have done so far. Any help would be much appreciated. Thanks in advance.
#include <SoftwareSerial.h>
// The TinyGPS++ object
TinyGPSPlus gps;
static const int RXPin = 4, TXPin = 3; //gps module connections
static const uint32_t GPSBaud = 9600;
// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);
const int PUSH_BUTTON = 2;
void setup(){
pinMode(PUSH_BUTTON, INPUT_PULLUP); //push button input
Serial.begin(9600);
ss.begin(GPSBaud);
}
void loop(){
unsigned char i;
static const double homeLat = 12.334455, homeLon = 05.112233;
while (ss.available() > 0){
gps.encode(ss.read());
if (gps.location.isUpdated()){
Serial.print("Latitude= ");
Serial.print(gps.location.lat(), 6);
Serial.print(" Longitude= ");
Serial.println(gps.location.lng(), 6);
}
status = digitalRead(PUSH_BUTTON);
if (status== HIGH){
*missing code/confused*
}
delay(1000);
}
}```
It is simple just store the values in 2 variables.
#include <SoftwareSerial.h>
// The TinyGPS++ object
TinyGPSPlus gps;
static const int RXPin = 4, TXPin = 3; //gps module connections
static const uint32_t GPSBaud = 9600;
// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);
const int PUSH_BUTTON = 2;
double save_lat, save_lang;
void setup(){
pinMode(PUSH_BUTTON, INPUT_PULLUP); //push button input
Serial.begin(9600);
ss.begin(GPSBaud);
}
void loop(){
unsigned char i;
static const double homeLat = 12.334455, homeLon = 05.112233;
while (ss.available() > 0){
gps.encode(ss.read());
if (gps.location.isUpdated()){
Serial.print("Latitude= ");
Serial.print(gps.location.lat(), 6);
Serial.print(" Longitude= ");
Serial.println(gps.location.lng(), 6);
}
status = digitalRead(PUSH_BUTTON);
if (status== HIGH){
if (gps.location.isUpdated()){
save_lat = gps.location.lat();
save_lang = gps.location.lng();
}
}
delay(1000);
}
}
I'm trying to make an smart car with Arduino Mega, and I need to turn both of the back wheels on for an specific time sometimes. I've been told that I can set a "digital HIGH" time using tone, But as I need them to work in a same time, Is there a way to set tone for two pins in one line or something to do instead?
Thanks for your help.
#include <Servo.h>
/////////////////////
Servo servo;
/////////////////////
int trig = 12;
int echo = 13;
long duration;
int distance;
int dist_right;
int dist_left;
int ang = 90;
unsigned int value = 255;
unsigned long tone_time = 3000;
float forward_time;
/////////////////////
int ena = 35;
int in1 = 7;
int in2 = 6;
int in3 = 5;
int in4 = 4;
int enb = 47;
/////////////////////
void setup()
{
Serial.begin(9600);
servo.attach(22);
pinMode(trig, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(ena, OUTPUT);
pinMode(enb, OUTPUT);
pinMode(echo, INPUT);
digitalWrite(ena, HIGH);
digitalWrite(enb, HIGH);
}
void loop() {
servo.write(90);
distance = dist();
if(distance<=15)
{
for(ang;ang>=0;ang-=2)
{
servo.write(ang);
delay(30);
}
dist_right = dist();
Serial.println(dist_right);
for(ang;ang<=180;ang+=2)
{
servo.write(ang);
delay(30);
}
dist_left = dist();
Serial.println(dist_left);
for(ang;ang>=90;ang-=2)
{
servo.write(ang);
delay(30);
}
if(dist_right>=dist_left)
{
tone(in3, value, tone_time);
}
else if(dist_right<dist_left)
{
tone(in1, value, tone_time);
}
servo.write(90);
ang=90;
}
else{
forward_time=distance/25;
tone((in1,in3), value, forward_time);
}
}
int dist(){
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
distance= duration*0.034/2;
return distance;
}
I'm 99.99% sure, that your motors will not feel the time differencce if you turn them one by one. Try simplest case and you will see.
// Define your wheel control pins (use same as in your mega)
const int motor1Pin = 5;
const int motor1Pin = 6;
// somewhere in setup method
outputMode(motor1Pin, OUTPUT);
outputMode(motor2Pin, OUTPUT);
// Create function to turn motors and remember the time
unsigned long turnMotorsOn(int seconds) {
// turn motors ant return time when they should be stopped
return millis() + seconds * 1000;
}
// In you code check if it is time to turn off
if (millis() > timeWhenTurnMotorsOff) {
// turn them off
}
Kindly help me in the Arduino code as I am new in this field. I have Arduino code that turns the light in bulb ON and OFF using toggle switch. It is successfully running and giving output.
The following is the code:
int buttonPinBulb = 11;
int relay1 = 10;
void setup() {
pinMode(buttonPinBulb, INPUT_PULLUP);
pinMode(relay1, OUTPUT);
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
int buttonBulb = digitalRead(buttonPinBulb);
if(buttonBulb == HIGH){
digitalWrite(relay1, HIGH);
} else {
digitalWrite(relay1, LOW);
}
Serial.println(buttonBulb);
}
Before following suggestion in the comment, the output was:
Issue
Bulb is turning ON and OFF when I toggle switch ON and OFF, and the output is showing on serial monitor continuously. But I want only one value that is not repeated. Like if I toggle the switch ON, then the value shown on serial monitor should be 1 and not 11111111....
Please help me about that. How can I do that?
After following suggestion in the comment, the code is:
int buttonPinBulb = 11;
int relay1 = 10;
int buttonBulb;
void setup() {
pinMode(buttonPinBulb, INPUT_PULLUP);
pinMode(relay1, OUTPUT);
Serial.begin(115200);
Serial.println(buttonBulb);
}
void loop() {
// put your main code here, to run repeatedly:
buttonBulb = digitalRead(buttonPinBulb);
if(buttonBulb == HIGH){
digitalWrite(relay1, HIGH);
}else{
digitalWrite(relay1, LOW);
}
//Serial.println(buttonBulb);
}
And the output was:
You can use a global variable to store the current status of the button.
You may also want to debounce your button (using millis() in the below example, unless the debouncing is already done in hardware) - especially when you want to update a database each time the status changes.
int buttonPinBulb = 11;
int relay1 = 10;
int currentStatus = LOW;
// Debounce
unsigned long lastMillis = 0;
const unsigned long debounceTime = 100; // 100ms wait
void setup() {
pinMode(buttonPinBulb, INPUT_PULLUP);
pinMode(relay1, OUTPUT);
Serial.begin(115200);
}
void loop() {
unsigned long currentMillis = millis();
if ( (currentMillis - lastMillis > debounceTime)
|| (currentMillis < lastMillis)) { // protect against overflow
int buttonBulb = digitalRead(buttonPinBulb);
if (buttonBulb != currentStatus) {
digitalWrite(relay1, buttonBulb);
Serial.println(buttonBulb);
currentStatus = buttonBulb;
// update database here
}
lastMillis = currentMillis;
}
}
As you have been told in comments, just store the current value of the pin and issue digitalWrite () only if a change is required.
ccesfully running and giving output. The following is the code:
int buttonPinBulb = 11;
int relay1 = 10;
int curRel1;
void setup()
{
pinMode(buttonPinBulb, INPUT_PULLUP);
pinMode(relay1, OUTPUT);
curRel1 = digitalRead(relay1);
Serial.begin(115200);
}
void loop()
{
int buttonBulb = digitalRead(buttonPinBulb);
if(buttonBulb == HIGH)
{
digitalWrite(relay1, HIGH);
}
else
{
digitalWrite(relay1, LOW);
}
if (buttonBulb != curRel1)
{
Serial.println(buttonBulb);
curRel1 = buttonBulb;
}
}
You could also update curRel1 in loop() calling digitalRead().
I am building an Arduino car, which avoids obstacles and when I try to upload the code to my Arduino from the PlatformIO package of Atom I get an error message like this:
avrdude: verifying ...
avrdude: verification error, first mismatch at byte 0x0aaa
0x68 != 0x60
avrdude: verification error; content mismatch
avrdude done. Thank you.
This started happening a few days ago for no reason. It worked perfectly and suddenly I started getting this error message.
My code is:
#include <Arduino.h>
#include <Servo.h>
const int trigPin = 6;
const int echoPin = 7;
const int motorRF = 3;
const int sleep = 4;
const int motorRB = 9;
const int motorLF = 10;
const int motorLB = 11;
int minDistance = 350;
long value;
int speed = 1000;
int randNum;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(motorRF, OUTPUT);
pinMode(motorRB, OUTPUT);
pinMode(motorLF, OUTPUT);
pinMode(motorLB, OUTPUT);
pinMode(sleep, OUTPUT);
Serial.begin(9600);
}
//Define the directions of the motors
void forward() {
analogWrite(motorRF, speed);
analogWrite(motorLF, speed);
analogWrite(motorRB, 0);
analogWrite(motorLB, 0);
}
void backward() {
analogWrite(motorRF, 0);
analogWrite(motorLF, 0);
analogWrite(motorRB, speed);
analogWrite(motorLB, speed);
}
void right() {
analogWrite(motorRF, 0);
analogWrite(motorLF, speed);
analogWrite(motorRB, speed);
analogWrite(motorLB, 0);
}
void left() {
analogWrite(motorRF, speed);
analogWrite(motorLF, 0);
analogWrite(motorRB, 0);
analogWrite(motorLB, speed);
}
void loop() {
//Ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
value = pulseIn(echoPin, HIGH);
Serial.println("Value = "); Serial.println(value);
delay(50);
//Motors
digitalWrite(sleep, HIGH);
if(value > minDistance) {
//Drive forward
backward();
}
else {
//Drive backward
forward();
delay(1000);
//Pick between number 1 and 2
randNum = random(0, 2);
Serial.println("Random Num = "); Serial.println(randNum);
//If the number is 2 then drive right
if(randNum == 1) {
right();
delay(500);
}
//Else drive left
else {
left();
delay(500);
}
}
}
Thank you.
It is a hardware problem. The program is fine. I tried to upload it to another Arduino and it worked perfectly. Thank you.