Arduino doesn't turn off vibration after inputting 0 - arduino

I have arduino UNO with bluetooth module and vibration motor attached. I can turn on the vibrator but cant seem to turn it off. Here's the code
#include<SoftwareSerial.h>//import the serial library
int vib=8;
SoftwareSerial Genotronex(10,11);//RX,TX
int BluetoothData;//the data given
void setup() {
Genotronex.begin(9600);
Genotronex.println("Bluetooth on please press 1 to vibrate");
pinMode(vib,OUTPUT);
// put your setup code here, to run once:
}
void loop() {
if (Genotronex.available()){
BluetoothData=Genotronex.read();{
if(BluetoothData='1'){
digitalWrite(vib,'1');
Genotronex.println("Vibrator on");
delay(500);
}
else if (BluetoothData='0'){
digitalWrite(vib,'0');
Genotronex.println("Vibrator off");
delay(500);
}
}
}
delay(100);
// put your main code here, to run repeatedly:
}
In the bluetooth terminal, it stated
<1> Vibrator on
when i input '1'
but also stated
<0) Vibrator on
when i input '0' when it should've been Vibrator off.
Appreciate all the help

if(BluetoothData='1'){
^
Single = is assignment. Use == for comparisons.
Also, BluetoothData should probably be defined as a local variable in loop(). It'll work either way, but will compile to slightly more efficient (and more readable!) code.

Related

Arduino timer not counting accuratly

I am working on a school project for which I rotate a servo after a cable disconnects and a specific delay is over. This is my current code. We are using an Arduino Uno powered from the USB port
#include <Servo.h>
int reader=4;
int servo1Pin=8;
Servo servo1;
int value;
int pos=10;
int wacht=5000;
void setup() {
pinMode(reader, INPUT);
servo1.attach(servo1Pin);
}
void loop() {
value = digitalRead(reader);
servo1.write(pos);
if (value == LOW) {
delay(wacht);
pos=180;
}
else {
pos=10;
}
}
wacht is the specific delay. When we disconnected pin 4 to break that circuit we don't have a consistent time between the interruption of the power flow and the opening of the servo. It seems to vary from anywhere between 5 to 40 seconds of delay after triggering. Does anyone have any ideas to solve this issue?
try change
pinMode(reader, INPUT);
to
pinMode(reader, INPUT_PULLUP);
And for clarification delay don't use timer.
The order of your instructions seem strange. the usual order is:
Read
Decide
Act
Also, you may want to avoid sending useless instructions to the servo. For example, when you know the servo is already in the correct position. You should not rely on eternal code to do the right thing. In other terms, you have no idea how long servo1.write() takes to execute.
Which would give for your loop()
void loop()
{
if (digitalRead(reader) == LOW)
{
if (pos != 180)
{
delay(wacht); // delay() is only called once, when circuit breaks.
// this guarantees immediate response when circuit
∕/ closes again.
pos = 180;
servo1.write(pos);
}
}
else if (pos != 10)
{
pos = 10;
servo1.write(pos);
}
}
Also, have a look and implement Peter Plesník's answer. This will probably solve some of your problems. You input will definitely still have a random lag of up to 5 seconds when closing the circuit, though.

input !== output signal - why?

I'm using a STM32G431KB Nucleo board (nucleo_g431kb - 170MHz) with PlatformIO & the Arduino framework.
My simple code looks like this:
void setup()
{
pinMode(PA11, INPUT);
pinMode(PA12, OUTPUT);
}
void loop()
{
if (digitalReadFast(PA_11) == HIGH) {
digitalWriteFast(PA_12, HIGH);
}
else {
digitalWriteFast(PA_12, LOW);
}
}
On pin 11 I got a well defined input signal like shown in the picture below (yellow signal). The blue signal in the picture is the one from the STM32 (pin 12).
Now the blue signal is not that defined like the yellow one. In theory, they both should be identical, shouldn't they? How do I get the output signal mirroring the input signal? The chip should have enough power. I tried a pull-down resistor, but it didn't change anything. I guess, I'm simply stupi.
Try internal pullup resistor such as
pinMode(PA12, OUTPUT_PULLUP);
Also try changing the output pin as a test because it seems like something else( some other process) is trying to also trying to pull same output pin.

Code that returns analog read from arduino when specific key is typed

I am trying to create an arduino code that displays the analogread values of pins A0,A1 and A2 when 1 is passed through the serial monitor. The Arduino must stop transmitting values when anything else is pressed, say 0. It must start again when 1 is pressed and so on. My code (below) doesn't seem to work. Please help.
int oldv;
int newv;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()>0){
newv=(Serial.read());
if(newv==1){
//newv=1;
Serial.print(analogRead(A0));
Serial.print(",");
Serial.print(analogRead(A1));
Serial.print(",");
Serial.print(analogRead(A2));
Serial.println(",");
oldv=1;}
else{
oldv=0;
}
}
else{
newv=oldv;
if(newv==1){
Serial.print(analogRead(A0));
Serial.print(",");
Serial.print(analogRead(A1));
Serial.print(",");
Serial.print(analogRead(A2));
Serial.println(",");
oldv=1;}
else{
oldv=0;
}
}
}
You transmit text via Serial, so you should check for
if(newv=='1'){
You should also consider that there might come newline characters '\r' or '\n'.
Better you explicitly check for '0' or '1' and read but ignore the rest.

How to read switches via serial port C++?

I have an arduino nano. I want to connect MX Cherry switches and detect pressing throught the serial port. What pins should i use on arduino and what code should be uploaded to the plate?
I understand that i have to power the switches so there has to be 5v pin and input pin. But i'm new to electronics so i didn't manage to figure it out.
//that's just basic code for sending a number every second via 13 pin
int i=0;
void setup() {
Serial.begin(57600);
pinMode(13, OUTPUT);
}
void loop() {
i = i + 1;
Serial.println(i);
delay(1000);
}
Basically, i need a way of sending '1' if button is pressed and '0' if it's not.
Perhaps I've misunderstood your question. Why not just read the button and send a '1' if pressed and '0' if not?
void loop(){
int buttonState = digitalRead(buttonPin);
// Assumes active low button
if (buttonState == LOW){
Serial.print('1');
}
else {
Serial.print('0');
}
delay(500);
}
Of course you probably want to add some sort of timing to that so it doesn't send thousands of 0's and 1's per second. I added a delay, but that might not be the best answer for the application you have (and chose not to share). I've also assumed that your button is wired active-LOW with a pull-up since you didn't share that either.

Button press To Jump to the beginning of a Sketch

I have a program that has many different long running sections (can be 15 mins at a time) and it uses an “IF” statement to decide what section to execute.
My problem is that I want to be able to press a button and have it move to another section immediately without having to wait for the current section to complete.
I thought I could use and external interrupt but I see the interrupt just causes the program to stop execute the interrupt code and continue running from the same place it was before the interrupt was called .
I then thought I could use the “goto” statement, but that does not work ether because the compiler complains if your label is outside of the function you are using the “goto” statement.
I have posted my code below I basically want to be able to press the button and have the code move on to the next “IF” statement no matter what it was doing when I pressed the button.
The delays are just their to simulate what the program would do.
The program is actually for a robot that has many different modes.
In mode 1 in just navigates around.
In mode 2 it can be controlled by a controller.
In mode 3 it will just sit until the PIR sensor see some thing then it will start roaming around.
So you see the robot could be in any state for any amount of time doing any thing.I want to push a button and have it stop and change modes.
Example code
volatile int state = LOW;
int mode = 0;
void setup()
{
Serial.begin(9600);
attachInterrupt(0, blink, RISING);
}
void loop()
{
Serial.println();
Serial.print("##########################");
Serial.println();
Serial.print("Start it again");
Serial.println();
Serial.print("##########################");
if(mode==0)
{Serial.println();
Serial.print("0");
Serial.println();
delay(30000);}
if(mode==1)
{Serial.println();
Serial.print("1");
Serial.println();
delay(30000);}
if(mode==2)
{Serial.println();
Serial.print("2");
Serial.println();
delay(30000);}
if(mode==3)
{Serial.println();
Serial.print("3");
Serial.println();
delay(30000);}
if(mode==4)
{Serial.println();
Serial.print("4");
Serial.println();
delay(30000);}
}
void blink()
{ delay(800); // This is just a delay to allow for the button press
if(mode >= 4)
{mode = 0;}
else{mode = ++mode ;}
}
You should structure your code so that the subroutines don't take a long time.
while(1){
switch(mode){
case 0: //one cycle of case 0
break;
case 1: //one cycle of case 1
break;
}
}
Then, in your interrupt service routine, you can set the mode.
replace all occurances of delay(3000); with a new function, which does the following 30 times: if the button has been pressed, return, otherwise, delay 100.

Resources