So my problem is that after like two readings of digital pin 6,7 the pin stays on forever for some reason here's my code. I can't seem to find why does it happen I tried to add some delay(x); but it didn't help .. never happend for pin 5 which is even more strange to me... any help will be appreciated
#include <Stepper.h>
int obstaclePin = A0;
int obstaclePin2 = A1;
int obstaclePin3 = A2;
const int button1Pin = 5;
const int button2Pin = 6;
const int button3Pin = 7;
const int stepsPerRevolution = 80;
Stepper myStepper(stepsPerRevolution, 50, 51, 52, 53);
Stepper myStepper2(stepsPerRevolution, 46, 47, 48, 49);
Stepper myStepper3(stepsPerRevolution, 42, 43, 44, 45);
void setup() {
myStepper.setSpeed(100);
myStepper2.setSpeed(100);
myStepper3.setSpeed(100);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(obstaclePin, INPUT); // sets the sensor pin
pinMode(obstaclePin2, INPUT); // sets the sensor pin
pinMode(obstaclePin3, INPUT); // sets the sensor pin
pinMode(button1Pin, INPUT_PULLUP); // declare pushbutton as input
pinMode(button2Pin, INPUT_PULLUP);
pinMode(button3Pin, INPUT_PULLUP);
Serial.begin(9600); // initialize the serial port:
}
int counter = 0;
void loop() {
while(true){
if(digitalRead(button1Pin)==0){
counter = 1;
break;
}
else if(digitalRead(button2Pin)==0){
counter = 2;
break;
}
else if(digitalRead(button3Pin)==0){
counter = 3;
break;
}
}
while(counter!=0){
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
//*******************************************************************************************************************************************************
if(counter==1){
if(analogRead(obstaclePin)<=400){
counter=0;
delay(100);
break;
}
Serial.println("Motor 1");
myStepper.step(1);
}
//*******************************************************************************************************************************************************
else if(counter==2){
if(analogRead(obstaclePin2)<=400){
counter=0;
delay(100);
break;
}
Serial.println("Motor 2");
myStepper2.step(1);
}
//*******************************************************************************************************************************************************
else if(counter==3){
if(digitalRead(obstaclePin3)==0){
counter=0;
delay(100);
break;
}
Serial.println("Motor 3");
myStepper3.step(1);
}
}
}
Debouncing the inputs is the solution here.
Related
If I remove attachInterrupt(digitalPinToInterrupt(encoder1),readEncoder,RISING); The code works. But once its added, the radio.available doesnt let anything under it run.
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
struct InputData // define stuct
{
int x;
int y;
};
InputData data;
// Motor A connections
int motor_enA = 9;
int motor_in1 = 10;
int motor_in2 = 6;
int encoder1 = 2;
int encoder2 = 3;
int counter = 0;
int angle = 0;
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(1, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
// Set all the motor control pins to outputs
pinMode(motor_enA, OUTPUT);
pinMode(motor_in1, OUTPUT);
pinMode(motor_in2, OUTPUT);
// Turn off motors - Initial state
digitalWrite(motor_in1, LOW);
digitalWrite(motor_in2, LOW);
analogWrite(motor_enA, 255);
pinMode (encoder1, INPUT);
pinMode (encoder2, INPUT);
attachInterrupt(digitalPinToInterrupt(encoder1),readEncoder,RISING);
}
void loop() {
readEncoder();
if (radio.available()) {
radio.read(&data, sizeof(data));
// Serial.println(data.y);
if (data.y > 5) {
digitalWrite(motor_in1, HIGH);
digitalWrite(motor_in2, LOW);
}
else if (data.y < -5) {
digitalWrite(motor_in1, LOW);
digitalWrite(motor_in2, HIGH);
}
else {
digitalWrite(motor_in1, LOW);
digitalWrite(motor_in2, LOW);
}
}
if(counter>1){
counter=0;
angle+=2;
}else if(counter<-1){
counter=0;
angle-=2;
}
Serial.print("Position: ");
Serial.println(angle);
}
void readEncoder()
{
if(digitalRead(encoder1)==HIGH){
int b = digitalRead(encoder2);
if(b>0){
counter++;
}
else{
counter--;
}
}
}
I have tried removing and adding the line, as described above^^
as mentioned by Hcheung, make counter volatile and remove readEncoder(); from loop.
I simplify a bit ISR readEncoder();
volatile int counter = 0;
[....]
void readEncoder() {
//if(digitalRead(encoder1)==HIGH){ //we are precisely here because digitalRead(encoder1) = HIGH !
if(digitalRead(encoder2)) counter++;
else counter--;
}
I'm trying to make a program so when 2 switches are flipped they light up two 4 pin LEDs, but only when both switches are flipped. I'm trying to make a cool BattleBots arena and thought something like that would be cool to have as a starting sequence! If you spot any extra errors feel free to correct me.
int buttonPin = 13;
int buttonPin2 = 3;
int counter = 0;
int redpin = 11; //select the pin for the red LED
int bluepin =10; // select the pin for the blue LED
int greenpin =9;// select the pin for the green LED
int redpin2 = 7; //select the pin for the 2 red LED
int bluepin2 =6; // select the pin for the 2 blue LED
int greenpin2 =5;// select the pin for the 2 green LED
int pinButton = 13; //the pin where we connect the button
int pinButton2 = 3; //the pin where we connect the button
int val;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(redpin, OUTPUT);
pinMode(bluepin, OUTPUT);
pinMode(greenpin, OUTPUT);
pinMode(redpin2, OUTPUT);
pinMode(bluepin2, OUTPUT);
pinMode(greenpin2, OUTPUT);
Serial.begin(9600);
}
void loop()
{ int buttonState;
buttonState = digitalRead(buttonPin);
int buttonState2;
buttonState2 = digitalRead(buttonPin2);
}
int main()
if (buttonState == LOW && buttonState2 == LOW);
analogWrite(11, 0);
analogWrite(10, 225);
analogWrite(9, 0);
delay(5);
analogWrite(7, 0);
analogWrite(6, 225);
analogWrite(5, 0);
delay(5);
}else{
analogWrite(11, 225);
analogWrite(10, 0);
analogWrite(9, 0);
delay(5);
analogWrite(7, 0);
analogWrite(6, 0);
analogWrite(5, 225);
delay(5);
}
Your code seem to be a cut and paste from several sources, here's what it should look like, I added notes in the code:
const int buttonPin = 13;
const int buttonPin2 = 3;
const int redpin = 11; //select the pin for the red LED
const int bluepin = 10; // select the pin for the blue LED
const int greenpin = 9; // select the pin for the green LED
const int redpin2 = 7; //select the pin for the 2 red LED
const int bluepin2 = 6; // select the pin for the 2 blue LED
const int greenpin2 = 5; // select the pin for the 2 green LED
/*
Unused variables removed:
int counter = 0;
int pinButton = 13;
int pinButton2 = 3;
int val;
*/
void setup() {
pinMode(buttonPin, INPUT);
pinMode(redpin, OUTPUT);
/*
Depending on your hardware you may want to pull-up the button pins:
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
*/
pinMode(bluepin, OUTPUT);
pinMode(greenpin, OUTPUT);
pinMode(redpin2, OUTPUT);
pinMode(bluepin2, OUTPUT);
pinMode(greenpin2, OUTPUT);
Serial.begin(9600);
}
void loop() {
int buttonState;
buttonState = digitalRead(buttonPin);
int buttonState2;
buttonState2 = digitalRead(buttonPin2);
/*
main() function had several issues, it was a function returning an integer without
any return statement and it is never called by your code. Looks like it should be
part of loop() like so:
*/
if (buttonState == LOW && buttonState2 == LOW) {
analogWrite(11, 0);
analogWrite(10, 225);
analogWrite(9, 0);
delay(5);
analogWrite(7, 0);
analogWrite(6, 225);
analogWrite(5, 0);
delay(5);
} else {
analogWrite(11, 225);
analogWrite(10, 0);
analogWrite(9, 0);
delay(5);
analogWrite(7, 0);
analogWrite(6, 0);
analogWrite(5, 225);
delay(5);
}
}
i keep getting these Buzzer-CLiCK:33:19: error: a function-definition is not allowed here before '{' token
void circling() {
^
Buzzer-CLiCK:40:17: error: a function-definition is not allowed here before '{' token
void stopc () {
^
exit status 1
a function-definition is not allowed here before '{' token
below is my code
const int BUTTON_PIN = 0; // Arduino pin connected to button's pin
const int BUZZER_PIN = 12; // Arduino pin connected to Buzzer's pin
const int LED_1 = 13;
int point;
int pin[] = {9, 8, 7, 6, 2, 3, 4, 5};
void setup() {
Serial.begin(9600);
for (int i = 0 ; i <= 7; i++) { // initialize serial
pinMode(pin[i], OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
pinMode(BUZZER_PIN, OUTPUT); // set arduino pin to output mode
}
}
void loop() {
int buttonState = digitalRead(BUTTON_PIN); // read new state
if (buttonState == LOW) {
Serial.println("The button is being pressed");
digitalWrite(BUZZER_PIN, HIGH); // turn on
digitalWrite(LED_1, HIGH);
circling();
}
else if (buttonState == HIGH) {
Serial.println("The button is unpressed");
digitalWrite(BUZZER_PIN, LOW); // turn off
digitalWrite(LED_1, LOW);
stopc();
}
void circling() {
for (int i = 0; i <= 7 ; i++) {
digitalWrite(pin[i], HIGH);
digitalWrite(pin[i], LOW);
}
}
void stopc () {
for (int i = 0 ; i <= 7; i++) {
digitalWrite(pin[i], LOW);
}
}
}
I am finishing up a project for school, and I am at the last step for getting my device 100% working. For a little information on the project. I am developing a wireless speedometer that tracks rpm and converts it to MPH through a hall effects sensor. The modules I am using Heltec LoRa esp32 in addition to the LoRa library through Arduino. The issue I am having is that I am trying to use the data packets received and use the sensor values sent out to be used in an if statement to activate a vibration motor to indicate if they reached a certain top speed. My code is below, and any input on how to use the packets correctly would be much appreciated. Thank you.
Receiver:
//lora stuff
#include <SPI.h>
#include <LoRa.h>
#include "SSD1306.h"
SSD1306 display(0x3c, 4, 15);
#define SS 18
#define RST 14
#define DI0 26
#define BAND 433E6
//neopixel
#include <Adafruit_NeoPixel.h>
#define LED_PIN 32
#define LED_COUNT 1
Adafruit_NeoPixel strip = Adafruit_NeoPixel(1, 32, NEO_GRB + NEO_KHZ800);
const int buttonPin = 17;// button
const int neo = 32; //
const int neoCount = 1; //number of pixels
int counter = 0; //button state counter
int buttonState = 0;
//Vibration motor
int motorPin = 25;
//level of speed in mph to trigger motor
int beginner = 10;
int intermediate = 15;
int expert = 25;
int mphVal = 0;
String data;
unsigned int totalMPH;
void setup() {
//lora
pinMode(16, OUTPUT);
digitalWrite(16, LOW); // set GPIO16 low to reset OLED
delay(50);
digitalWrite(16, HIGH);
display.init();
display.flipScreenVertically();
display.setFont(ArialMT_Plain_10);
display.setTextAlignment(TEXT_ALIGN_LEFT);
// button
pinMode(buttonPin, INPUT);
//motor
pinMode(motorPin, OUTPUT);
//neopixel
strip.begin();
strip.show();
strip.setBrightness(50);
Serial.begin(115200);
//lora initial
while (!Serial); //if just the the basic function, must connect to a computer
delay(100);
Serial.println("Speed Receiver");
display.drawString(5, 5, "Speed Receiver");
display.display();
SPI.begin(5, 19, 27, 18);
LoRa.setPins(SS, RST, DI0);
if (!LoRa.begin(BAND)) {
display.drawString(5, 25, "Starting LoRa failed!");
while (1);
}
Serial.println("LoRa Initial OK!");
display.drawString(5, 25, "LoRa Initializing OK!");
display.display();
}
void loop() {
// try to parse packet
int packetSize = LoRa.parsePacket();
if (packetSize) {
// received a packets
//Serial.print("Received packet. ");
display.clear();
display.setFont(ArialMT_Plain_16);
display.drawString(3, 0, "Received Speed ");
display.display();
// read packet
while (LoRa.available()) {
String data = LoRa.readString();
Serial.print(data);
display.drawString(20, 22, data);
display.display();
// if speed reaches x then motor will buzz
if (data.equals("MPH: 12.5")) {
digitalWrite(motorPin, HIGH);
delay(10);
digitalWrite(motorPin, LOW);
delay(10);
}
}
// print RSSI of packet
//Serial.print(" with RSSI ");
//Serial.println(LoRa.packetRssi());
// display.drawString(20, 45, "RSSI: ");
// display.drawString(70, 45, (String)LoRa.packetRssi());
// display.display();
}
//taking the packet and turning it into an int from string
//String data = data.toInt();
//int data = totalMPH;
//float totalMPH = data.toFloat();
// char data;
// float totalMPH;
//totalMPH = atof(data);
//Serial.println(totalMPH);
//button
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
counter++;
delay(150);
}
//button states with three colors
//beginner
else if (counter == 0) {
//Serial.println("pushed");
strip.setPixelColor(0, 255, 0, 0);
strip.show();
//Serial.println(0);
display.drawString(10, 45, "REC: 10 MPH");
display.display();
//vibration motor will trigger if the speed recieved is less then or equal to 12mph
// digitalWrite(motorPin, HIGH);
// delay(4000);
// digitalWrite(motorPin, LOW);
// delay(120000);
}
//intermediate
else if (counter == 1) {
strip.setPixelColor(0, 0, 255, 0);
strip.show();
Serial.println(1);
display.drawString(10, 45, "REC: 15 MPH");
display.display();
//vibration motor will trigger if the speed recieved is less then or equal to 17mph
// digitalWrite(motorPin, HIGH);
// delay(4000);
// digitalWrite(motorPin, LOW);
// delay(240000);
//digitalWrite(motorPin, LOW);
}
//expert
else if (counter == 2) {
strip.setPixelColor(0, 0, 0, 255);
strip.show();
Serial.print(2);
display.drawString(10, 45, "REC: 20 MPH");
display.display();
//digitalWrite(motorPin, LOW);
}
//reset settings
else {
counter = 0;
}
}
Transmitter:
//lora
#include <SPI.h>
#include <LoRa.h>
#include "SSD1306.h"
#include <Arduino.h>
#include <Bounce2.h>
Bounce hall = Bounce();
SSD1306 display(0x3c, 4, 15);
#define SS 18
#define RST 14
#define DI0 26
#define BAND 433E6 //915E6
int counter = 0;
int rpmTimer = 0;
int rpmInterval = 1000;
void setup() {
//board and lcd
pinMode(25, OUTPUT); //Send success, LED will bright 1 second
pinMode(16, OUTPUT);
digitalWrite(16, LOW); // set GPIO16 low to reset OLED
delay(50);
digitalWrite(16, HIGH);
// sensor and led
hall.attach(37, INPUT);
hall.interval(1);
//pinMode(led, OUTPUT);
//pinMode(hallSens, INPUT);
Serial.begin(115200);
//lcd
while (!Serial); //If just the the basic function, must connect to a computer
// Initialising the UI will init the display too.
display.init();
display.flipScreenVertically();
display.setFont(ArialMT_Plain_10);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.drawString(5, 5, "LoRa Sender");
display.display();
//lora
SPI.begin(5, 19, 27, 18);
LoRa.setPins(SS, RST, DI0);
Serial.println("LoRa Sender");
if (!LoRa.begin(BAND)) {
Serial.println("Starting LoRa failed!");
while (1);
}
Serial.println("LoRa Initial OK!");
display.drawString(5, 20, "LoRa Initializing OK!");
display.display();
delay(10);
}
void loop() {
hall.update();
if(hall.rose()){
counter++;
}
if(millis() - rpmTimer > rpmInterval){
float rpm = counter * 60;
float rph = rpm * 60;
float dia = 0.00004349;
float cir = dia * 3.14;
float totalMPH = rph * cir;
Serial.println(totalMPH);
rpmTimer = millis();
counter = 0;
//sending packet
LoRa.beginPacket();
LoRa.print("MPH: " + String(totalMPH));
LoRa.endPacket();
//LoRa.sleep(); //puts lora receiver to sleep probably dont need
digitalWrite(25, HIGH); // turn the LED on (HIGH is the voltage level)
delay(10); // wait for a second
digitalWrite(25, LOW); // turn the LED off by making the voltage LOW
delay(100); // wait for a second
//delay(300);
}
}
In terms of wire-based API design:
// packet is the packet from the transmitter
String packet = "S12.5"
String measurement = packet.substring(0, 1); // "S"
if (measurement == "S") {
// at this point we know that the rest of the message is the MPH
String data = packet.substring(1, strlen(packet)-1); // "12.5"
float mph = data.toFloat();
if (mph >= TOP_SPEED) {
doSomething();
}
else {
doSomethingElse();
}
}
you could use charAt(0) to get the measurement instead of substring() if you know there are less than 25 sensors on the network.
each measurement would have its own char at the start of message and the data that related to that measurement would immediately follow. The sender and receiver are therefore interacting like a client/server in an API exchange. There is an implicit contract between them such that, when the transmitter says "S12.5", the receiver knows that means 12.5MPH. It's your system so you can design the communication packets any way you like.
I am trying to use my arduino and a potentiometer to make a motor spin one way when the potentiometer is past 0 and spin the other when the potentiometer is past 0 the other direction. The code is working on the SensorValue < 512 side but not on the >507 side.
const int analogInPin = A1; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; //
const int analogOutPin_2 = 11; //
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(analogInPin);
if (sensorValue < 507) {
analogWrite(analogOutPin, LOW);
outputValue = map(sensorValue, 0, 512, 0, 255);
analogWrite(analogOutPin_2, outputValue);
}
if (512 > sensorValue) {
analogWrite(analogOutPin_2, LOW);
outputValue = map(sensorValue, 512, 1023, 0, 255);
analogWrite(analogOutPin, outputValue);
}
else {
}
delay(2);
}
For solving issues such as this. I recommend a few strategic prints and a for loop to simulate the data range, reveals the following code with a few tweaks works better
const int analogInPin = A1; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; //
const int analogOutPin_2 = 11; //
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
delay(1000);
Serial.println("Begin1");
}
void loop() {
// sensorValue = analogRead(analogInPin);
for (sensorValue = 0; sensorValue < 1024; sensorValue++) {
Serial.print("sensorValue = ");
Serial.print(sensorValue);
if (sensorValue < 507) {
outputValue = map(sensorValue, 0, 512, 255, 0);
Serial.print(" Forward : ");
Serial.print(outputValue);
analogWrite(analogOutPin, LOW);
analogWrite(analogOutPin_2, outputValue);
} else if (512 < sensorValue) {
outputValue = map(sensorValue, 512, 1023, 0, 255);
Serial.print(" Reverse : ");
Serial.print(outputValue);
analogWrite(analogOutPin_2, LOW);
analogWrite(analogOutPin, outputValue);
}
Serial.println();
}
while(1);
delay(1000);
}