servo retaining its position everytime i send some value - arduino

i wanted to control my servo with bluetooth modue. I gave certain values for rotating it. but when i press those values the servo rotates but then retains it's original position and pc pc also make the sound of arduino board disconnecting because i powered the board with my pc(tried with powerbank also)
this is my code
#include <Servo.h>
Servo myservo;
int pos = 0;
char data = 0;
void setup() {
myservo.attach(9);
Serial.begin(9600);}
void loop(){
if(Serial.available() > 0){
data = Serial.read();
if(data == 'F'){
// for (pos = 0; pos <= 180; pos += 1) even this line dosent work
myservo.write(180);
delay(1);
}}
else if(data == 'B'){
// for (pos = 180; pos >= 0; pos -= 1) even this line dosent work
myservo.write(0);
delay(1);
}}
connections
servo
red wire -- 5v (not working in 3.3 v)
orange wire -- pin9
brown wire -- gnd
bluetooth (hc05)
5v -- 5v
gnd -- gnd
rx -- tx
tx -- rx

Your else if (data=='B'){ is in the wrong place - it's part of the if(Serial.available() > 0) { condition - instead of the if(data == 'F'){ condition. Notice the double }} in the line before.
I've corrected the code and reformated it so you can clearly see the matching opening and closing braces.
This works on Tinkercad Circuits passing commands through the serial monitor - so imagine it would work fine taking commands from bluetooth.
#include <Servo.h>
Servo myservo;
int pos = 0;
char data = 0;
void setup() {
myservo.attach(9);
Serial.begin(9600);
}
void loop() {
if(Serial.available() > 0) {
data = Serial.read();
if(data == 'F'){
for (pos = 0; pos <= 180; pos += 1) {
myservo.write(pos);
delay(1);
}
}
else if(data == 'B'){
for (pos = 180; pos >= 0; pos -= 1) {
myservo.write(pos);
delay(1);
}
}
}
}

Related

How can I bitbang Arduino Nano RP2040 Connect for multiple AM2320 temperature sensors? Useage of bitbang libraries?

can you help me with the following?:
I'm a beginner and have an Arduino Nano RP2040 Connect and four AM2320 temperature sensors, but each have the same address, that can't be changed (this project is for my graduation exam). I got an info, that it is possible to create more I2C buses "in a software way" by using 4 functions (a start, a stop, one that can transmit a bit, and one that can read a bit). In this way, any of the digital pins can be programmed as SDA/SCL.
I have been told that there's a library named 'SoftwareWire', but I can't really understand the commands of it, and which comes after the other. Has anyone anything about this?
Searched for bitbanging in a 'software-way', found that there are libraries for this, but can't deal with the commands.
hi i will pass to you a function that works for me with 3 i2c sensors sh21 with same adress
#include <Wire.h>
#include "SHT2x.h"
uint32_t start;
uint32_t stop;
SHT2x sht;
float tempN1;
float humN1;
float dwn1;
float tempN2;
float humN2;
float dwn2;
float tempN3;
float humN3;
float dwn3;
int flip = 0;
void sht21read(){
if (flip == 0)
{
Wire.begin(21, 22); // 2
delay(100);
// myHTU21D.begin();
sht.begin(21, 22);
start = micros();
sht.read();
stop = micros();
delay(250);
tempN1 = sht.getTemperature();
humN1 = sht.getHumidity();
// dwn1 = SHT2x.GetDewPoint();
delay(250);
Wire.end();
flip = 1;
}
else if (flip == 1)
{
Wire.begin(32, 22); // 4
delay(100);
// myHTU21D.be sht.begin();gin();
sht.begin(32, 22);
start = micros();
sht.read();
stop = micros();
delay(250);
tempN2 = sht.getTemperature();
humN2 = sht.getHumidity();
// dwn2 = SHT2x.GetDewPoint();
delay(250);
Wire.end();
flip = 2;
}
else if (flip == 2)
{
Wire.begin(27, 22); // 13
delay(100);
// myHTU21D.begin();
sht.begin(27, 22);
start = micros();
sht.read();
stop = micros();
delay(250);
tempN3 = sht.getTemperature();
humN3 = sht.getHumidity();
// dwn3 = SHT2x.GetDewPoint();
delay(250);
Wire.end();
flip = 3;
}
else if (flip == 3)
{
flip = 0;
Serial.print("TEMPERATURA N1= ");
Serial.print(tempN1);
Serial.print("");
Serial.print("HUMEDAD N1= ");
Serial.print(humN1);
Serial.print("");
Serial.print("||||");
Serial.print("TEMPERATURA N2= ");
Serial.print(tempN2);
Serial.print("");
Serial.print("HUMEDAD N2= ");
Serial.print(humN2);
Serial.print("");
Serial.print("||||");
Serial.print("TEMPERATURA N3= ");
Serial.print(tempN3);
Serial.print("");
Serial.print("HUMEDAD N3= ");
Serial.print(humN3);
Serial.println("");
delay(5000);
}
}
then you run the function sht21read(); (or with your own name) on void loop and uala all working

Serial communication stuck after first attempt

I am using arduino Mega 2560 to control some vibration motors with touchdesigner through Serial Communication. I maped the pixels to control each motors, It works for a few seconds and gets stuck very soon. Is there anything wrong with my code?
Here is my arduino sketch:
#define MOTOR_COUNT 12
int motors[MOTOR_COUNT];
void setup()
{
Serial.begin(115200);
Serial.println("Ready to receive frames.");
for (int i = 0; i <= 12; i++) {
pinMode(i, OUTPUT);
}
void loop()
{
if (Serial.available())
{
char c = Serial.peek();
if (!(c >= '0' && c <= '9'))
{
Serial.read(); // Discard non-digit character
}
else if (Serial.read() == '\n')
{
for (uint16_t i = 0; i < MOTOR_COUNT; i++)
{
motors[i] = Serial.parseInt();
Serial.print("motor ");
Serial.print(i);
Serial.print(":");
Serial.println(motors[i]);
if (i <= 12) {
analogWrite(i + 2, motors[i]);
}
}
}
}
}

In an Arduino project, what does it mean to be missing a primary expression before else?

I only started programming a few days ago and ran into a few problems.
I'm trying to make a servo turn 180 degrees when I type 1 and 180 degrees the other way when I type 0, I'm using an HC-05 Bluetooth module connected to my phone, so I tried to "merge" the servo sweep code from Arduino IDE library and another code that turns a light on by Bluetooth (which works), so I've been trying to fix this without any results.
Here's what I've done so far:
#include <Servo.h>
Servo myservo;
int pos = 0;
char data = 0;
void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
myservo.attach(13);
}
void loop()
{
if (Serial.available() > 0)
{
data = Serial.read();
Serial.print(data);
Serial.print("\n");
if (data == '1') for (pos = 0; pos <= 180; pos += 1)
else if (Serial.available() > 1)
digitalWrite(13, myservo(pos = 180; pos >= 0; pos -= 1));
}
}
Arduino create keeps telling me I'm missing a primary expression before else.
You've added the start of a for loop, but not told the compiler what operations to repeat.
When you're beginning, it's useful to add braces whenever you use a control statement (if, while, for, do, switch, case) irrespective of whether you have to, and indent consistently, then you can see where things should go and where the body of the control statement starts and ends.
void loop()
{
if (Serial.available() > 0)
{
data = Serial.read();
Serial.print(data);
Serial.print("\n");
if (data == '1')
{
// execute the code from the 'sweep' example if the user sends '1'
for (pos = 0; pos <= 180; pos += 1)
{
// goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos);
delay(15);
}
}
else if (Serial.available() > 1)
{
// removed as code here made no real sense
}
}
}

Arduino - Function-definition is not allowed here before '{' token

I am trying to make an Arduino rfid door lock, with arduino uno, stepper motor, funduino rfid-rc522. Here is the code:
//declare variables for the motor pins
int motorPin1 = 6; // Blue - 28BYJ48 pin 1
int motorPin2 = 7; // Pink - 28BYJ48 pin 2
int motorPin3 = 8; // Yellow - 28BYJ48 pin 3
int motorPin4 = 9; // Orange - 28BYJ48 pin 4
// Red - 28BYJ48 pin 5 (VCC)
int motorSpeed = 1500
; //variable to set stepper speed
int count = 0; // count of steps made
int countsperrev = 12; // number of steps per full revolution
int lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};
#include <RFID.h>
/*
* Read a card using a mfrc522 reader on your SPI interface
* Pin layout should be as follows (on Arduino Uno):
* MOSI: Pin 11 / ICSP-4
* MISO: Pin 12 / ICSP-1
* SCK: Pin 13 / ISCP-3
* SS: Pin 10
* RST: Pin 5
*/
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <RFID.h>
#define SS_PIN 10
#define RST_PIN 5
RFID rfid(SS_PIN,RST_PIN);
int serNum[5];
#define I2C_ADDR 0x27 // <<----- Add your address here. Find it from I2C Scanner
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
int n = 1;
int a = 0;
int DA = A0;
int DO = 2;
int state = 0;
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
void setup(){
//declare the motor pins as outputs
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
Serial.begin(9600);
SPI.begin();
rfid.init();
lcd.begin (16,2); // <<----- My LCD was 16x2
// Switch on the backlight
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.home (); // go home
}
void loop(){
if(rfid.isCard()){
if(rfid.readCardSerial()) {
Serial.print(rfid.serNum[0],DEC);
/*Serial.print(" ");
Serial.print(rfid.serNum[1],DEC);
Serial.print(" ");
Serial.print(rfid.serNum[2],DEC);
Serial.print(" ");
Serial.print(rfid.serNum[3],DEC);
Serial.print(" ");
Serial.print(rfid.serNum[4],DEC);
Serial.println("");
*/
if(rfid.serNum[0]==199){
if (state == 0);{
Serial.print("opennnnnn");
Serial.println("");
lcd.setCursor(3,0);
lcd.print("Door Open!");
delay(1000);
lcd.home();
while (a<200){
if(count < countsperrev )
clockwise();
else if (count == countsperrev * 2)
count = 0;
else
count++;
delay(0);
a=a+1;
}
lcd.clear();
delay(5000);
a = 0;
state = 1;
}
if (state == 1) {
Serial.print("Door already open");
lcd.print("Door already open");
}
}
else{
if (state == 1){
Serial.print("closeeee");
Serial.println("");
lcd.setCursor(2,0);
lcd.print("Door Closed!");
delay(1000);
lcd.home();
while (a<200){
if(count < countsperrev )
anticlockwise();
else if (count == countsperrev * 2)
count = 0;
else
count++;
delay(0);
a=a+1;
}
lcd.clear();
delay(5000);
a = 0;
state = 0;
}
if (state == 0) {
lcd.print("Door Already Closed");
Serial.print("Door Already Closed");
}
rfid.halt();
}
//set pins to ULN2003 high in sequence from 1 to 4
//delay "motorSpeed" between each pin setting (to determine speed)
void anticlockwise()
{
for(int i = 0; i < 8; i++)
{
setOutput(i);
delayMicroseconds(motorSpeed);
}
}
void clockwise()
{
for(int i = 7; i >= 0; i--)
{
setOutput(i);
delayMicroseconds(motorSpeed);
}
}
void setOutput(int out)
{
digitalWrite(motorPin1, bitRead(lookup[out], 0));
digitalWrite(motorPin2, bitRead(lookup[out], 1));
digitalWrite(motorPin3, bitRead(lookup[out], 2));
digitalWrite(motorPin4, bitRead(lookup[out], 3));
}
I want it to only be able to open the door once, and then it must close to be opened again, but when i added the if (state == 0) state = 1; if (state == 1) if (state == 1) if (state == 0) the arduino IDE gave me an error of:
Arduino: 1.6.5 (Windows 7), Board: "Arduino Uno"
sketch_sep07d.ino: In function 'void loop()':
sketch_sep07d:158: error: a function-definition is not allowed here before '{' token
sketch_sep07d:181: error: expected '}' at end of input
sketch_sep07d:181: error: expected '}' at end of input
sketch_sep07d:181: error: expected '}' at end of input
a function-definition is not allowed here before '{' token
This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.
I have research the syntax for functions and it appears to be correct, i dont know what is going on.
If anyone could help, please do so.
Cheers
You're missing one or more close braces inside loop(). Verify that all open braces have a corresponding close brace in the expected location.

Arduino IR door

i want to make an IR sensor door with DFRobot IRsensor (switch) and Servo. the problem is in the if statement because the digitalread is always changing when there's nothing in front of the IR sensor which is the way it suppouse to be... so it rapidly closing and opening. but i want the door to be open when there's something in front of the sensor and then some delay, then its gonna close if there's something (new) in front of the door.
mycode:
#include <Servo.h>
Servo myservo;// create servo object to control a servo
// a maximum of eight servo objects can be created
int IRsensor =8;
int pos = 180; // variable to store the servo position
int d;
boolean kondisi;
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode (IRsensor,INPUT);
Serial.begin (9600);
}
void loop()
{
d = digitalRead (IRsensor);
bukatutup (d);
delay (15);
}
void bukatutup(int IR)
{
Serial.println (IR);
if (IR == 0 and (kondisi == false))
{
pos = 0;
Serial.println ("terbuka");
myservo.write (pos);
kondisi == true;
delay(1000);
}
else if ( IR == 0 and (kondisi == true))
{
pos = 180;
Serial.println ("tertutup");
kondisi == false;
myservo.write (pos);
delay (1000);
}
}
Thx,

Resources