So the code is not working properply, there are two leds that wont turn off "highliten" the problem. when i run the Else part of the program. i want to turn them off in the else part. :)
include
byte ledPin[] = {8, 9, 10, 11, 12, 13}; //--------------------------------.
int ledDelay; // Del 1
int direction = 1;
int currentLED = 0;
unsigned long changeTime;
int potPin = 0;
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
int va;
void setup()
{
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, INPUT);
myservo.attach(3); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
for (int x=0; x<6; x++) {
pinMode(ledPin[x], OUTPUT); }
changeTime = millis();
}
void loop() {
int on = digitalRead(6);
if (on == HIGH)
{
myservo.attach(3);
// Here is the problem!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if va < 523)
{
digitalWrite(5, HIGH);
}
else if (va > 555)
{
digitalWrite(4, HIGH);
}
else
{
digitalWrite(4, LOW);
digitalWrite(5, LOW);
}
// Here is the problem!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
va = analogRead(potPin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(va, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(1); // waits for the servo to get there
}
else
{
myservo.detach();
digitalWrite(5, LOW);
digitalWrite(4, LOW);
ledDelay = analogRead(potPin) / 4;
if ((millis() - changeTime) > ledDelay)
{
changeLED();
changeTime = millis();
}
}
}
void changeLED() {
for (int x=0; x<6; x++)
{
digitalWrite(ledPin[x], LOW);
}
digitalWrite(ledPin[currentLED], HIGH);
currentLED += direction;
if (currentLED == 6) {direction = -1;}
if (currentLED == 0) {direction = 1;}
}
in advance Thank you!
Right at the end of the sketch you have the following line:
if (currentLED == 6) { direction = -1; }
I think, without actually running the program, that the problem is here. In the previous line you have added one to the value of currentLED and you are checking to see if you have gone off the end of the ledPin array. You change the direction but you don't reset the currentLED position to be back inside the ledPin range.
The next time changeLED is called it tries to call digitalWrite(ledPin[currentLED], HIGH); but the value of currentLED is 6, which is outside the ledPin array. The Arduino probably gets upset at this point.
I think you simply need to change the statement to check whether currentLED == 5 rather than 6. This will mean that next time that changeLED is called the last LED will be turned on and the value of currentLED will be decremented (direction == -1), keeping it inside the ledPin range.
Related
I have these two sets of code and they both run fine on their own, but when I am trying to merge them together I am getting a ton of error messages. I'm new to coding and don't really know a whole lot so I was wondering if someone could just quickly look it over and see what's wrong. The compiler is saying that there is an expected unqualified id before the '{' token at the bottom where I have the code for the pushbutton. However I have tried and removed this and then I get the same message but instead of '{' it says 'if'
Thank you very much
EDIT: I don't know what properly formatting an error message looks like but here is my attempt:
Complete_Code:69:1: error: expected unqualified-id before '{' token
{
^
exit status 1
expected unqualified-id before '{' token
Here is the code in question
int pinButton = 5;
#include <Servo.h>
int servo1Pin = 8;
Servo servo1;
#include <Servo.h>
#define mainArm 0
#define jib 1
#define bucket 2
#define slew 3
Servo servo[2]; //code used for attaching upto 4 servos
byte angle[2] = {90, 90}; // middle point for servo angle
byte potPin[2] = {A1, A2}; // input pins to attach your potentiometers
byte servoPin[2] = {7, 6};
void setup() {
Serial.begin(9600);
Serial.println("Starting DiggerCode.ino");
for (byte n = 0; n < 4; n++) {
servo[n].attach(servoPin[n]);
}
pinMode(pinButton, INPUT);
pinMode(12, OUTPUT); //red LED
pinMode(11, OUTPUT); // yellow LED
pinMode(10, OUTPUT); // green LED
servo1.attach(servo1Pin);
}
void loop() {
readPotentiometers();
moveServos();
delay(10);
}
void readPotentiometers() {
int potVal;
for (byte n = 0; n < 4; n++) {
potVal = analogRead(potPin[n]);
if (potVal < 200) { // dead zone for the joystick I used is 200 to 550.
angle[n] += 1;
if (angle[n] > 170) {
angle[n] = 170;
}
}
if (potVal > 550) { // deadzone upper value
angle[n] -= 1;
if (angle[n] < 10) {
angle[n] = 10;
}
}
}
}
void moveServos() {
for (byte n = 0; n < 4; n++) {
servo[n].write(angle[n]);
}
}
{
int stateButton = digitalRead(pinButton); //read the state of the button
if (stateButton == HIGH) { //if is pressed
Serial.println("Switch = High");
digitalWrite(12, HIGH);
delay(1000);
digitalWrite(12, LOW);
digitalWrite(11, HIGH);
delay(1000);
digitalWrite(11, LOW);
digitalWrite(10, HIGH);
delay(1000);
digitalWrite(10, LOW);
delay(500);
servo1.write(180);
delay(1000);
// Make servo go to 180 degrees
servo1.write(90);
delay(1000);
servo1.write(180);
}
}
It seems you dropped the first things of function definition for some reason.
Add function return type, name and arguments for example
void anImportedFunction()
Before the part starting from
{
int stateButton = digitalRead(pinButton); //read the state of the button
You may also need to call the added function from somewhere.
I looked through your code and edited, some unnecessary and missing parts.
I checked it, no more syntax errors. I think it must work.
#include <Servo.h>
#define mainArm 0
#define jib 1
#define bucket 2
#define slew 3
Servo servo1;
Servo servo[4]; //code used for attaching upto 4 servos
int pinButton = 5;
int servo1Pin = 8;
byte angle[2] = {90, 90}; // middle point for servo angle
byte potPin[2] = {A1, A2}; // input pins to attach your potentiometers
byte servoPin[2] = {7, 6};
void setup() {
Serial.begin(9600);
Serial.println("Starting DiggerCode.ino");
for (byte n = 0; n <= 4; n++) {
servo[n].attach(servoPin[n]);
}
pinMode(pinButton, INPUT);
pinMode(12, OUTPUT); //red LED
pinMode(11, OUTPUT); // yellow LED
pinMode(10, OUTPUT); // green LED
servo1.attach(servo1Pin);
}
void loop() {
readPotentiometers();
moveServos();
delay(10);
}
void readPotentiometers() {
int potVal;
for (byte n = 0; n <= 4; n++) {
potVal = analogRead(potPin[n]);
if (potVal < 200) { // dead zone for the joystick I used is 200 to 550.
angle[n] += 1;
if (angle[n] > 170) {
angle[n] = 170;
}
}
if (potVal > 550) { // deadzone upper value
angle[n] -= 1;
if (angle[n] < 10) {
angle[n] = 10;
}
}
}
}
void moveServos() {
for (byte n = 0; n <= 4; n++) {
servo[n].write(angle[n]);
}
}
void Switch() //here instead of "Switch" you can use any function name you want
{
int stateButton = digitalRead(pinButton); //read the state of the button
if (stateButton == HIGH) { //if is pressed
Serial.println("Switch = High");
digitalWrite(12, HIGH);
delay(1000);
digitalWrite(12, LOW);
digitalWrite(11, HIGH);
delay(1000);
digitalWrite(11, LOW);
digitalWrite(10, HIGH);
delay(1000);
digitalWrite(10, LOW);
delay(500);
servo1.write(180);
delay(1000);
// Make servo go to 180 degrees
servo1.write(90);
delay(1000);
servo1.write(180);
}
}
I have a circuit with 2 proximity sensors, two leds and a toggle to start/end the program.
Sensor down (sD) turns on (ledB) and turns off (ledG) when activated.
Sensor up (sU) turns on (ledG) and turns off (ledB) when activated.
Context:
The proximity sensors sence once a piston pump has reached end of stroke which will tell the arduino to switch a
solenoid valve in the other direction, reversing the direction of the pump. (leds represent solenoid for ease of
testing)
The Problem:
When the toggle is switched to the closed position and if the pump is mid stroke it wont move until the solenoid valve has
been turned on and the pump reaches end of stroke which will activate a sensor starting the process.
I need one led/solenoid valve to light up and then shut off as soon as one of the proxy sensors are activated.
Edit:
Video of what I have and what I'm trying to achieve:
https://drive.google.com/file/d/15bgbLU_OcVZIzw9IDD_5R_cjSMGxHwGZ/view?usp=sharing
Thanks for any input...
This has me stumped
-Sam
int sD = 4;
int ledB = 2;
int ledG = 3;
int sU = 5;
int mainSwitch = 7;
int ledBin = 8;
int ledGin = 9;
void setup()
{
// put your setup code here, to run once:
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT);
pinMode(9, INPUT);
}
void loop()
{
mainSwitch = digitalRead(7);
if (mainSwitch == true) //Ends Program
{
sD = digitalRead(4);
sU = digitalRead(5);
digitalWrite(ledB, LOW);
digitalWrite(ledG, LOW);
}
if (mainSwitch == false)//Starts program
{
sD = digitalRead(4);
sU = digitalRead(5);
if (sD == false) digitalWrite(ledB, HIGH); //sD(proximity sensor) Turns on ledB turns off ledG when activated
if (sD == false) digitalWrite(ledG, LOW);
if (sU == false) digitalWrite(ledG, HIGH); //sU(poximity sensor) Turns on ledG turns off ledB when activated
if (sU == false) digitalWrite(ledB, LOW);
}
}
From what I can understand
When you use digitalRead() on the Arduino it can return either "HIGH" or "LOW"
if (sD == false)
if (sU == false)
Try changing the "false" to "LOW" and see if that works for you.
Don't forget to check here for more detailed info.
I understand you need either one or the other LED (piston) active, while the main switch is ON. You change them at the end point, and you keep them unchanged, when no end point is reached.
On startup, you have to start with an arbitrary (or predefined) direction but not with both LEDs off, as you have it now. So there's not too much missing.
And don't confuse constant pin numbers and their variable states :)
const byte ledB = 2;
const byte ledG = 3;
// other pin numbers, hardcoded usage in code
// sU = 5; sD = 4;
// mainSwitch = 7;
bool dir = true; // Direction: default = up
void setup()
{
pinMode(ledG, OUTPUT);
pinMode(ledB, OUTPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(7, INPUT_PULLUP);
}
void loop()
{
bool mainSwitch = digitalRead(7);
if (mainSwitch == true) // stops Program
{
digitalWrite(ledB, LOW);
digitalWrite(ledG, LOW);
}
else
{ // normal mode, one of both LED on
// Read end switches
bool sD = digitalRead(4);
bool sU = digitalRead(5);
delay(5); // debounce, if these are mechanical switches
if (sD) dir = true; // switch to Up
if (sU) dir = false; // switch to Down
// else don't change direction
if (dir) {
digitalWrite(ledG, LOW);
digitalWrite(ledB, HIGH);
}
else {
digitalWrite(ledB, LOW);
digitalWrite(ledG, HIGH);
}
}
}
I have solved my own question thanks for the help.
-Sam
int sD = 4;
int ledB = 2;
int ledG = 3;
int sU = 5;
int mainSwitch = 7;
int ledBin = 8;
int ledGin = 9;
void setup()
{
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT);
pinMode(9, INPUT);
}
void loop()
{
mainSwitch = digitalRead(7);
if (mainSwitch == true) //Ends Program
{
sD = digitalRead(4);
sU = digitalRead(5);
digitalWrite(ledB, LOW);
digitalWrite(ledG, LOW);
}
if (mainSwitch == false)//Starts program
{
sU = digitalRead(5);
sD = digitalRead(4);
if (sU == HIGH) //Turns on ledG(down solenoid) at first start. sU(upper
sensor) in deactivated
{
ledGin = digitalRead(9);
ledBin = digitalRead(8);
if (ledBin == LOW)digitalWrite(ledG, HIGH);
}
else if (sU == LOW) //sU(upper sensor) is activated turning on ledB(down
solenoid) turning off ledG(up solenoid)
{
ledBin = digitalRead(8);
ledGin = digitalRead(9);
if (ledBin == LOW) digitalWrite(ledB, HIGH);//ledB(down solenoid) turn on
if (ledGin == HIGH) digitalWrite(ledG, LOW);//ledG(up solenoid) turn off
}
if (sD == LOW)//sD(Bottom sensor) is activated turning on ledB(down
solenoid) turning off ledG(down solenoid)
{
ledBin = digitalRead(8);
ledGin = digitalRead(9);
if (ledBin == HIGH) digitalWrite(ledB, LOW);//ledB(down solenoid) turn off
if (ledGin == LOW) digitalWrite(ledG, HIGH);//ledB(up solenoid) turn on
}
}
}.
I have a game that consist of 4 direction of movement (up down left and right)
using Arduino Nano and analog joystick, seems like code is right as check before other posts.
This is the Arduino code:
byte x_axis = A3;
byte y_axis = A1;
byte btn1 = 8;
byte btn2 = 9;
byte btn3 = 10;
byte btn4 = 11;
byte btn5 = 12;
byte led = 13;
void setup(){
pinMode(x_axis, INPUT);
pinMode(y_axis, INPUT);
pinMode(btn1, INPUT);
pinMode(btn2, INPUT);
pinMode(btn3, INPUT);
pinMode(btn4, INPUT);
pinMode(btn5, INPUT);
digitalWrite(btn1, HIGH);
digitalWrite(btn2, HIGH);
digitalWrite(btn3, HIGH);
digitalWrite(btn4, HIGH);
digitalWrite(btn5, HIGH);
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
Serial.begin(9600);
}
void loop(){
Int read_x = analogRead(x_axis);
int read_y = analogRead(y_axis);
if(read_x > 600){
Serial.println("R");
digitalWrite(led, HIGH);
}
if(read_x < 400){
Serial.println("L");
digitalWrite(led, HIGH);
}
if(read_y > 600){
Serial.println("D");
digitalWrite(led, HIGH);
}
if(read_y < 400){
Serial.println("U");
digitalWrite(led, HIGH);
}
if(digitalRead(btn1) == LOW){
Serial.println("1");
digitalWrite(led, HIGH);
}
if(digitalRead(btn2) == LOW){
Serial.println("2");
digitalWrite(led, HIGH);
}
if(digitalRead(btn3) == LOW){
Serial.println("3");
digitalWrite(led, HIGH);
}
if(digitalRead(btn4) == LOW){
Serial.println("4");
digitalWrite(led, HIGH);
}
if(digitalRead(btn5) == LOW){
Serial.println("5");
digitalWrite(led, HIGH);
}
delay(10);
digitalWrite(led, LOW);
}
But when I use serial monitor to check it, it non stop show me U and L even without touching the joystick.
How can I fix this problem?
I'm feeling generous, so you have some jitter issues in the code and you really could use some cleanup. This code compiles. Ok now when a joystick is 0,0 in x/y physically it jitters in code. One thing you could do is remap out the jitter to give a wider center. print the raw analog values to the serial monitor and then map them out to your 0 point with a little padding. reference: https://www.arduino.cc/reference/en/language/functions/math/map/
byte x_axis = A3;
byte y_axis = A1;
byte btn[] = {8, 9, 10, 11, 12}; // 2,3,4,5,6
byte stat[] = {1, 2, 3, 4, 5};
byte led = 13;
int dval = 50;
void setup() {
pinMode(x_axis, INPUT);
pinMode(y_axis, INPUT);
for (int i = 0; i < 5; i++) {
pinMode(btn[i], INPUT);
digitalWrite(btn[i], HIGH);
}
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
Serial.begin(9600);
}
void loop() {
int read_x = analogRead(x_axis);
delay(dval);
int read_y = analogRead(y_axis);
delay(dval);
if (read_x > 600) {
Serial.println("R");
digitalWrite(led, HIGH);
}
if (read_x < 400) {
Serial.println("L");
digitalWrite(led, HIGH);
}
if (read_y > 600) {
Serial.println("D");
digitalWrite(led, HIGH);
}
if (read_y < 400) {
Serial.println("U");
digitalWrite(led, HIGH);
}
for (int i = 0; i < 5; i++) {
if (digitalRead(btn[i]) == LOW) {
Serial.println(stat[i]);
digitalWrite(led, HIGH);
}
}
delay(10);
digitalWrite(led, LOW);
}
Emad joon:
1- Check that the ground of your joy stick and the Vdd is connected to your arduino.
2- connect the x and y of the joy stick to the arduino analog inputs.
use this code as starter:
#define X_AXIS A1
#define Y_AXIS A3
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("X = ");
Serial.println(analogRead(X_AXIS);
Serial.print("Y = ");
Serial.println(analogRead(Y_AXIS);
delay(150);
}
See what values you get when your Joy Stick is at initial position. You can build up on this code for buttons.
Also, do not forget to use debouncing for your button readings:
if(digitalRead(Button1)==0){
delay(40);
if(digitalRead(Button1==0){
buttonPressed=true;
}
}
I am creating 3 LEDs that will light up by a remote. I am able to light up the LEDs individually but I need the power button to shut off all of the LEDs. How can I create a 4th case to turn off all LEDs?
#include <IRremote.h>
int RECV_PIN = 3; // the pin where you connect the output pin of TSOP4838
int led1 = 2;
int led2 = 4;
int led3 = 7;
int itsONled[] = {0,0,0,0};
/* the initial state of LEDs is OFF (zero)
the first zero must remain zero but you can
change the others to 1's if you want a certain
led to light when the board is powered */
#define code1 12495 // code received from button A
#define code2 6375 // code received from button B
#define code3 31365 // code received from button C
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600); // you can comment this line
irrecv.enableIRIn(); // Start the receiver
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
}
void loop() {
if (irrecv.decode(&results)) {
unsigned int value = results.value;
switch(value) {
case code1:
if(itsONled[1] == 1) { // if first led is on then
digitalWrite(led1, LOW); // turn it off when button is pressed
itsONled[1] = 0; // and set its state as off
} else { // else if first led is off
digitalWrite(led1, HIGH); // turn it on when the button is pressed
itsONled[1] = 1; // and set its state as on
}
break;
case code2:
if(itsONled[2] == 1) {
digitalWrite(led2, LOW);
itsONled[2] = 0;
} else {
digitalWrite(led2, HIGH);
itsONled[2] = 1;
}
break;
case code3:
if(itsONled[3] == 1) {
digitalWrite(led3, LOW);
itsONled[3] = 0;
} else {
digitalWrite(led3, HIGH);
itsONled[3] = 1;
}
break;
}
Serial.println(value); // you can comment this line
irrecv.resume(); // Receive the next value
}
}
I'm having trouble creating a toggle switch using an analog input connected to an RF receiver. I'm attempting to make a servo turn 180 degrees when the voltage reads higher than 800 and stay at 180 degrees until the button is pressed again. It only reads higher than 800 when I press a button on my key fob (RF transmitter). In my code it does this, but it doesn't hold at 180 degrees and wait until the button is pressed again to go back to 0 degrees.
#include <Servo.h>
Servo myservo;
const int analogInPin = A0;
int led = 13;
float sensorValue = 0;
void setup(){
Serial.begin(2400);
pinMode(led, OUTPUT);
myservo.attach(6);
myservo.write(0);
}
void loop(){
sensorValue = analogRead(analogInPin);
Serial.print("Voltage Output = ");
Serial.print(sensorValue);
Serial.println(" ");
delay(100);
if (sensorValue > 800) {
digitalWrite(led, HIGH);
myservo.write(180);
delay(100);
}
else{
digitalWrite(led, LOW);
myservo.write(0);
delay(100);
}
}
EDIT: I'm pretty close with this edited code. I added a variable and an if statement. It turns the LED on and it stays on, but when pressed again it doesn't turn off. So close...
const int analogInPin = A0;
int led = 13;
int ledState = 0;
float sensorValue = 0;
void setup(){
Serial.begin(2400);
pinMode(led, OUTPUT);
}
void loop(){
sensorValue = analogRead(analogInPin);
Serial.print("Voltage Output = ");
Serial.print(sensorValue);
Serial.println(" ");
delay(100);
if (sensorValue < 400 && ledState == 0) {
digitalWrite(led, HIGH);
delay(500);
ledState == 1;
}
if (sensorValue < 400 && ledState == 1) {
digitalWrite(led, LOW);
delay(500);
ledState == 0;
}
}
OK. I think I solved it! Take a look. It now turns the LED on when the button is pressed and stays on until the button is pressed again. This is how to create a switch using an analog input reading.
const int analogInPin = A0;
int led = 13;
int ledState = 0;
float sensorValue = 0;
void setup(){
Serial.begin(2400);
pinMode(led, OUTPUT);
}
void loop(){
sensorValue = analogRead(analogInPin);
Serial.print("Voltage Output = ");
Serial.print(sensorValue);
Serial.println(" ");
delay(100);
if (sensorValue < 400 && ledState == 0) {
digitalWrite(led, HIGH);
delay(100);
ledState = 1;
Serial.println(ledState);
delay(500);
}
else {
if (sensorValue < 400 && ledState == 1) {
digitalWrite(led, LOW);
delay(100);
ledState = 0;
Serial.println(ledState);
delay(500);
}
}
}