I wrote a code designed to automatically turn on and off a generator based on whether the battery is full or empty.
There are some bugs in lcd.begin() and lcd.clear() (They both don't work).
error : invalid use of non-static member function.
Thank you!
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
bool Settings = false;
unsigned long time1;
#define RELAY_PORT 10
float Voltage = 0.00;
int VoltOn = 47;
int VoltOff = 56;
int upbutton = 1;
int downbutton = 2;
int okbutton = 3;
int backbutton = 4;
void setup() {
pinMode(upbutton, INPUT);
pinMode(downbutton, INPUT);
pinMode(okbutton, INPUT);
pinMode(backbutton, INPUT);
pinMode(RELAY_PORT, OUTPUT);
// initialize the LCD
lcd.begin();
// Turn on the blacklight and print a message.
lcd.backlight();
lcd.setCursor(0, 1);
lcd.print ("generator: off");
}
void loop() {
int analog_value = analogRead(A0);
Voltage = ((analog_value * 5.0) / 1020) * 12;
lcd.setCursor(0, 0);
lcd.print("Volt: ");
lcd.print(Voltage);
lcd.print("V");
if (Voltage <= VoltOn && digitalRead(RELAY_PORT) == LOW)
{
digitalWrite(RELAY_PORT, HIGH);
lcd.setCursor(0, 1);
lcd.print ("generator: on");
}
if (Voltage >= VoltOff && digitalRead(RELAY_PORT) == HIGH)
{
digitalWrite(RELAY_PORT, LOW);
lcd.setCursor(0, 1);
lcd.print ("generator: off");
}
if ((digitalRead(upbutton) == HIGH && digitalRead(downbutton) == HIGH && digitalRead(okbutton) == HIGH && digitalRead(backbutton) == HIGH) || (Settings = true))
{
lcd.clear;
lcd.print("Settings:");
delay(2000);
time1 = millis();
bool Setting = false;
while (digitalRead(upbutton) == LOW && digitalRead(downbutton) == LOW && digitalRead(okbutton) == LOW && digitalRead(backbutton) == LOW)
{
if (Setting == false)
{
lcd.clear;
lcd.scrollDisplayLeft();
lcd.print("press Up to set turn on");
lcd.setCursor(0, 1);
lcd.print("press down to set turn off");
Setting = true;
}
if (millis() > time1 + 60000)
{
loop();
}
}
time1 = millis();
while (millis() > time1 + 60000)
{
if (upbutton == HIGH)
{
time1 = millis();
//lcd.clear;
lcd.scrollDisplayLeft();
lcd.print("press Up/ Down to up/ Down Voltage turn on");
while (digitalRead(upbutton) == LOW && digitalRead(downbutton) == LOW && digitalRead(okbutton) == LOW && digitalRead(backbutton) == LOW)
{
if (millis() > time1 + 60000)
{
loop();
}
}
time1 = millis();
while (millis() > time1 - 60000)
{
if (upbutton == HIGH)
{
int xdelay = 1000;
time1 = millis();
(VoltOn) = (VoltOn) + 1;
lcd.setCursor(0, 1);
lcd.print("Voltage-on: ");
lcd.print(VoltOn);
lcd.print("V");
delay(xdelay);
xdelay = xdelay / 1.5;
}
if (downbutton == HIGH)
{
int xdelay = 1000;
time1 = millis();
(VoltOn) = (VoltOn) - 1;
lcd.setCursor(0, 1);
lcd.print("Voltage-on: ");
lcd.print(VoltOn);
lcd.print("V");
delay(xdelay);
xdelay = xdelay / 1.5;
}
if (okbutton == HIGH)
{
loop();
}
if (backbutton == HIGH)
{ Settings = true;
}
if (millis() > time1 + 60000)
{
loop();
}
}
}
if (downbutton == HIGH)
{
time1 = millis();
lcd.clear;
lcd.scrollDisplayLeft();
lcd.print("press Up/ Down to up/ Down Voltage turn off");
while (digitalRead(upbutton) == LOW && digitalRead(downbutton) == LOW && digitalRead(okbutton) == LOW && digitalRead(backbutton) == LOW)
{
if (millis() > time1 + 60000)
{
loop();
}
}
time1 = millis();
while (millis() > time1 - 60000)
{
if (upbutton == HIGH)
{
int xdelay = 1000;
time1 = millis();
(VoltOff) = (VoltOff) + 1;
lcd.setCursor(0, 1);
lcd.print("Voltage-off: ");
lcd.print(VoltOff);
lcd.print("V");
delay(xdelay);
xdelay = xdelay / 1.5;
}
if (downbutton == HIGH)
{
int xdelay = 1000;
time1 = millis();
(VoltOff) = (VoltOff) - 1;
lcd.setCursor(0, 1);
lcd.print("Voltage-off: ");
lcd.print(VoltOff);
lcd.print("V");
delay(xdelay);
xdelay = xdelay / 1.5;
}
if (okbutton == HIGH)
{
loop();;
}
if (backbutton == HIGH)
{
Settings = true;
}
if (millis() > time1 + 60000)
{
loop();
}
}
}
if (backbutton == HIGH)
{
loop();
}
if (okbutton == HIGH)
{
loop();
}
}
}
}
I think you shouldn't ask on stackoverflow for someone just to do your work. People don't like unspecific questions here, that show little to no effort of own research..
That said, one thing that stands out is, that all your lcd.clear are missing brackets, they should be lcd.clear();
Try to split up your code in smaller sections and test individual components, if something doesn't work.
1.How to make this code (at the beginning of the loop) only run every half a second without rivet the rest of the code?
lcd.setCursor(0, 0);
lcd.print("Volt: ");
lcd.print(Voltage);
lcd.print("V");
2.The LCD displays (that the current coming to Arduino from the accumulator is lower than its starting power) "generator: onf" and not "generator: off":
lcd.print ("generator: off");
if (Voltage <= VoltOn && digitalRead(RELAY_PORT) == LOW)
{
digitalWrite(RELAY_PORT, HIGH);
lcd.setCursor(0, 1);
lcd.print ("generator: on");
}
if (Voltage >= VoltOff && digitalRead(RELAY_PORT) == HIGH)
{
digitalWrite(RELAY_PORT, LOW);
lcd.setCursor(0, 1);
lcd.print ("generator: off");
Related
Beginner in Arduino and ESP-32 needs help.
Hello together,
I’m using the Pololu - VNH5019 Motor Driver Carrier to control a 12v motor with an ESP32.
In the following sketch i can speed up and speed down the ramp with delay();.
I tried to archiv the same result with millis(), but until now i could not make it.
What i am missing in my code.
Thanks in advance.
#define MOTOR_IN1 27
#define MOTOR_IN2 16
#define PWMPIN 14
#define frequency 40000
#define resolutionbit 8
const unsigned long eventInterval = 30;
unsigned long previousTime = 0;
void setup() {
pinMode(MOTOR_IN1, OUTPUT);
pinMode(MOTOR_IN2, OUTPUT);
ledcAttachPin(PWMPIN, 0); // assign the speed control PWM pin to a channel
ledcSetup(0, frequency, resolutionbit);
}
void loop() {
//with_delay();
with_millis();
}
//------------------------------------------
void with_delay() {
// set direction
digitalWrite(MOTOR_IN1, HIGH);
digitalWrite(MOTOR_IN2, LOW);
// ramp speed up
for (int i = 0; i <= 255; i++) {
ledcWrite(0, i);
delay(30);
}
// ramp speed down
for (int i = 255; i >= 0; i--) {
ledcWrite(0, i);
delay(30);
}
}
//-------------------------------------------
void with_millis() {
unsigned long currentTime = millis();
if (currentTime - previousTime >= eventInterval) {
digitalWrite(MOTOR_IN1, HIGH);
digitalWrite(MOTOR_IN2, LOW);
for (int i = 0; i <= 255; i++) {
ledcWrite(0, i);
previousTime = currentTime;
}
}
if (currentTime - previousTime >= eventInterval) {
digitalWrite(MOTOR_IN1, HIGH);
digitalWrite(MOTOR_IN2, LOW);
for (int i = 255; i >= 0; i--) {
ledcWrite(0, i);
previousTime = currentTime;
}
}
}
Your problem is that the program gets stuck in the for loop.
You need to also create direction variable so the program that knows which if statement to execute.
You need to create some other logic that will increase the i variable without stopping the whole program.
The code:
//Initialize the i variable globaly:
int i = 0;
bool direction = 0;
//Your function:
void with_millis() {
unsigned long currentTime = millis();
if ((currentTime - previousTime >= eventInterval) && direction == true) {
digitalWrite(MOTOR_IN1, HIGH);
digitalWrite(MOTOR_IN2, LOW);
i++;
if (i <= 255) {
ledcWrite(0, i);
previousTime = currentTime;
} elif (i > 255) {
i = 0;
direction = false;
}
}
if ((currentTime - previousTime >= eventInterval) && direction == false) {
digitalWrite(MOTOR_IN1, LOW);
digitalWrite(MOTOR_IN2, HIGH);
i++;
if (i <= 255) {
ledcWrite(0, i);
previousTime = currentTime;
} elif (i > 255) {
i = 0;
direction = true;
}
}
}
I'm trying to create a bop it game with 2 players where:
Green LED – Gyro Sensor (Z-axis)
Red LED – Temperature sensor
Blue LED – Sound sensor
Yellow LED – Light Sensor
However when I press the left button to start the game, only the yellow light above GND goes on and doesn't respond to anything, what can be wrong with my code?
long randNumber;
int temp;
int val;
int number;
int score;
boolean state;
int light;
uint8_t pixeln = 0;
int sound;
int shake;
unsigned long lastmillis = 0;
boolean game;
const byte numPins = 7;
byte pins[] = {0, 1, 2, 3, 4, 5, 6, 7};
int player1 = 0;
int player2 = 0;
#include "Adafruit_CircuitPlayground.h"
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
CircuitPlayground.begin();
CircuitPlayground.clearPixels();
//timer();
}
void loop() {
if (digitalRead(4) == HIGH && digitalRead(19) == LOW){
Serial.println("Button Pressed");
lastmillis = millis();
for (int i=0; i < 10; i ){
randNumber = random(4);
//Serial.println("Loop");
switch (randNumber){
//Serial.println("Switch");
case 0:
while (pixeln < 10){
CircuitPlayground.setPixelColor(pixeln , 255,0,0);
CircuitPlayground.strip.show();
}
pixeln = 0;
state = true;
temp = analogRead(A0);
//Serial.println(temp);
while (state == true) {
val = analogRead(A0);
number = val - temp;
if (number > 10){
score ;
state = false;
//Serial.println("Temperature");
delay(500);
}
}
break;
case 1:
while (pixeln < 10){
CircuitPlayground.setPixelColor(pixeln , 127,127,0);
CircuitPlayground.strip.show();
}
pixeln = 0;
state = true;
light = analogRead(A5);
//Serial.println(light);
while (state == true) {
val = analogRead(A5);
number = light - val;
if (number > 20){
score ;
state = false;
//Serial.println(number);
delay(500);
}
}
break;
case 2:
while (pixeln < 10){
CircuitPlayground.setPixelColor(pixeln , 0,0,255);
CircuitPlayground.strip.show();
}
pixeln = 0;
state = true;
sound = analogRead(A4);
//Serial.println(sound);
while (state == true) {
val = analogRead(A4);
number = val - sound;
if (number > 50){
score ;
state = false;
//Serial.println(number);
delay(500);
}
}
break;
case 3:
while (pixeln < 10){
CircuitPlayground.setPixelColor(pixeln , 0,255,0);
CircuitPlayground.strip.show();
}
pixeln = 0;
val = CircuitPlayground.motionZ();
//Serial.println(val);
state = true;
while (state == true) {
shake = CircuitPlayground.motionZ();
number = val - shake;
//Serial.println(number);
if (number > 15 or number < -15){
score ;
state = false;
//Serial.println("shake");
delay(500);
}
}
break;
case 4:
CircuitPlayground.clearPixels();
CircuitPlayground.setPixelColor(0, 0,255,0);
CircuitPlayground.setPixelColor(1, 0,255,0);
CircuitPlayground.strip.show();
delay(3000);
break;
case 5:
CircuitPlayground.clearPixels();
CircuitPlayground.setPixelColor(3, 255,0,0);
CircuitPlayground.setPixelColor(4, 255,0,0);
CircuitPlayground.strip.show();
delay(3000);
break;
case 6:
CircuitPlayground.clearPixels();
CircuitPlayground.setPixelColor(5, 127,0,255);
CircuitPlayground.setPixelColor(6, 127,0,255);
CircuitPlayground.strip.show();
delay(3000);
break;
case 7:
CircuitPlayground.clearPixels();
CircuitPlayground.setPixelColor(8, 255,128,0);
CircuitPlayground.setPixelColor(9, 255,128,0);
CircuitPlayground.strip.show();
delay(3000);
}
}
if (CircuitPlayground.slideSwitch()) {
player1 = (millis() - lastmillis) / 1000;
Serial.println(player1);
CircuitPlayground.clearPixels();
byte num = player1;
for (byte i=0; i < numPins; i ){
byte state = bitRead(num, i);
if (state == 1){
CircuitPlayground.setPixelColor(pins[i], 255,255,255);
CircuitPlayground.strip.show();
}
}
} else {
player2 = (millis() - lastmillis) /1000;
Serial.println(player2);
CircuitPlayground.clearPixels();
byte num = player2;
for (byte i=0; i < numPins; i ){
byte state = bitRead(num, i);
if (state == 1){
CircuitPlayground.setPixelColor(pins[i], 255,255,255);
CircuitPlayground.strip.show();
}
}}
CircuitPlayground.playTone(330, 500);
}
while (digitalRead(19) == HIGH && digitalRead(4) == LOW){
if (player1 < player2){
CircuitPlayground.clearPixels();
delay(250);
for (int i =0; i < 5; i ){
CircuitPlayground.setPixelColor(i, 255,255,255);
CircuitPlayground.strip.show();
}
delay(250);
CircuitPlayground.clearPixels();
}
else if (player1 > player2){
CircuitPlayground.clearPixels();
delay(250);
for (int i =5; i < 10; i ){
CircuitPlayground.setPixelColor(i, 255,255,255);
CircuitPlayground.strip.show();
}
delay(250);
CircuitPlayground.clearPixels();
}
else {
CircuitPlayground.clearPixels();
delay(250);
for (int i =0; i < 10; i ){
CircuitPlayground.setPixelColor(i, 255,255,255);
CircuitPlayground.strip.show();
}
delay(250);
CircuitPlayground.clearPixels();
}
}
if (digitalRead(19) == HIGH && digitalRead(4) == HIGH){
player1 = 0;
player2 = 0;
for (int a =0; a < 10; a ){
CircuitPlayground.clearPixels();
delay(100);
for (int i =0; i < 10; i ){
CircuitPlayground.setPixelColor(i, CircuitPlayground.colorWheel(25 * i));
CircuitPlayground.strip.show();
}
delay(100);
CircuitPlayground.clearPixels();
}
}
}
visual after pressing left button:
So I've been working on an arduino project for an escape room puzzle. It's essentially a bomb with a timer on an lcd screen. After all of the elements of the bomb are solved the timer stops and the lcd screen displays that the bomb has been defused and gives the group a clue to the next puzzle. 9 times out of 10 it works perfectly. But every once in a while when it is supposed to display that the bomb is defused the lcd screen just shows random broken characters. I haven't had any luck diagnosing the problem. Hoping somebody here might have an idea.
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Tone.h>
#define pound 14
Tone tone1;
int Scount = 0;
int Mcount = 0;
int Hcount = 1;
int DefuseTimer = 0;
long secMillis = 0;
long interval = 1000;
char password[6] = "594432";
int currentLength = 0;
int i = 0;
char entered[6];
int ledPin = 23;
int ledPin2 = 25;
int ledPin3 = 27;
int ledPin4 = 29;
int ledPin5 = 31;
int ledPin6 = 34;
const int plugin1 = 44;
const int plugin2 = 46;
const int plugin3 = 48;
const int plugin4 = 50;
const int plugin5 = 52;
int plugin1State = 0;
int plugin2State = 0;
int plugin3State = 0;
int plugin4State = 0;
int plugin5State = 0;
const int switch1 = 37;
int switch1State = 0;
const int key1 = 40;
int key1State = 0;
int puzzle1 = 0;
int puzzle2 = 0;
int puzzle3 = 0;
int solved = 0;
LiquidCrystal lcd(7, 8, 10, 11, 12, 13);
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {A0, A1, A2, A3};
byte colPins[COLS] = {A4, A5, A6};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
pinMode(ledPin6, OUTPUT);
pinMode(plugin1, INPUT);
pinMode(plugin2, INPUT);
pinMode(plugin3, INPUT);
pinMode(plugin4, INPUT);
pinMode(plugin5, INPUT);
digitalWrite(plugin1, HIGH);
digitalWrite(plugin2, HIGH);
digitalWrite(plugin3, HIGH);
digitalWrite(plugin4, HIGH);
digitalWrite(plugin5, HIGH);
pinMode(switch1, INPUT);
digitalWrite(switch1, HIGH);
pinMode(key1, INPUT_PULLUP);
digitalWrite(key1, HIGH);
tone1.begin(9);
lcd.begin(16, 2);
Serial.begin(9600);
lcd.clear();
lcd.setCursor(0, 0);
tone1.play(NOTE_E6, 200);
delay(3000);
lcd.clear();
currentLength = 0;
}
void loop()
{
timer();
plugin1State = digitalRead(plugin1);
plugin2State = digitalRead(plugin2);
plugin3State = digitalRead(plugin3);
plugin4State = digitalRead(plugin4);
plugin5State = digitalRead(plugin5);
if (plugin1State == LOW && plugin2State == LOW && plugin3State == LOW && plugin4State == LOW && plugin5State == LOW)
{
puzzle1 = 1;
}
if (puzzle1 == 1)
{
digitalWrite(ledPin4, HIGH);
switch1State = digitalRead(switch1);
if (switch1State == LOW)
{
puzzle2 = 1;
}
}
if (puzzle2 == 1)
{
digitalWrite(ledPin5, HIGH);
key1State = digitalRead(key1);
if (key1State == LOW)
{
puzzle3 = 1;
}
if (key1State == HIGH)
{
digitalWrite(ledPin6, LOW);
}
}
if (puzzle3 == 1)
{
digitalWrite(ledPin6, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Code: ");
while (currentLength < 6)
{
timer();
char key2 = keypad.getKey();
if (key2 == "#")
{
currentLength = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Code: ");
}
else if (key2 != NO_KEY)
{
lcd.setCursor(currentLength + 7, 0);
lcd.cursor();
lcd.print(key2);
entered[currentLength] = key2;
currentLength++;
tone1.play(NOTE_C6, 200);
delay(100);
lcd.noCursor();
lcd.setCursor(currentLength + 6, 0);
lcd.print("*");
lcd.setCursor(currentLength + 7, 0);
lcd.cursor();
}
}
if (currentLength == 6)
{
if (entered[0] == password[0] && entered[1] == password[1] && entered[2] == password[2] && entered[3] == password[3] && entered[4] == password[4] && entered[5] == password[5])
{
solved = 1;
while (solved == 1)
{
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("BOMB 1 DEFUSED");
currentLength = 0;
digitalWrite(ledPin3, HIGH);
delay(1500);
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("RELEASE");
delay(1500);
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("TOXIC GAS");
delay(1500);
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("CLUE: %&#$#");
delay(6000);
}
}
else
{
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("Wrong Password!");
delay(1500);
currentLength = 0;
}
}
}
}
void timer()
{
if (Hcount <= 0)
{
if ( Mcount < 0 )
{
lcd.noCursor();
lcd.clear();
lcd.home();
lcd.print("The Bomb Has ");
lcd.setCursor (0, 1);
lcd.print("Exploded!");
while (Mcount < 0)
{
digitalWrite(ledPin, HIGH); // sets the LED on
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin, LOW); // sets the LED off
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin2, HIGH); // sets the LED on
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin2, LOW); // sets the LED off
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin3, HIGH); // sets the LED on
tone1.play(NOTE_A2, 90);
delay(100);
digitalWrite(ledPin3, LOW); // sets the LED off
tone1.play(NOTE_A2, 90);
delay(100);
}
}
}
lcd.setCursor (0, 1); // sets cursor to 2nd line
lcd.print ("Timer:");
if (Hcount >= 10)
{
lcd.setCursor (7, 1);
lcd.print (Hcount);
}
if (Hcount < 10)
{
lcd.setCursor (7, 1);
lcd.write ("0");
lcd.setCursor (8, 1);
lcd.print (Hcount);
}
lcd.print (":");
if (Mcount >= 10)
{
lcd.setCursor (10, 1);
lcd.print (Mcount);
}
if (Mcount < 10)
{
lcd.setCursor (10, 1);
lcd.write ("0");
lcd.setCursor (11, 1);
lcd.print (Mcount);
}
lcd.print (":");
if (Scount >= 10)
{
lcd.setCursor (13, 1);
lcd.print (Scount);
}
if (Scount < 10)
{
lcd.setCursor (13, 1);
lcd.write ("0");
lcd.setCursor (14, 1);
lcd.print (Scount);
}
if (Hcount < 0)
{
Hcount = 0;
}
if (Mcount < 0)
{
Hcount --;
Mcount = 59;
}
if (Scount < 1) // if 60 do this operation
{
Mcount --; // add 1 to Mcount
Scount = 59; // reset Scount
}
if (Scount > 0) // do this oper. 59 times
{
unsigned long currentMillis = millis();
if (currentMillis - secMillis > interval)
{
tone1.play(NOTE_G5, 200);
secMillis = currentMillis;
Scount --; // add 1 to Scount
digitalWrite(ledPin2, HIGH); // sets the LED on
delay(10); // waits for a second
digitalWrite(ledPin2, LOW); // sets the LED off
delay(10); // waits for a second
//lcd.clear();
}
}
}
That would most likely be a an electrical issues either with your circuit of your LCD. The fact that a simple code like yours (not trying to insult you in any way) works 9/10 of times means that there is probably nothing wrong with the code (I've seen none).
For quick check, try reinstalling the IDE. That might update the Arduino libraries that get downloaded with the IDE. I doubt that it would solve the issue but that is an quick free easy way to try
I would personnaly suggest to disconnect everything, and reconnecting them. And if it doesn't work, then return that LCD and get yourself a new one.
I had the same issue when I used a non shielded 1m ribbon cable for the 16x2 LCD. The main board with the MCU had to be placed further away from the display. It showed random character for any electromagnetic interference. For example, turning on a fluorescent tube, starting an electric screwdriver, relay switches etc. Shortening the ribon cable solved the problem.
Anyway, the LCD is sensitive to electromagnetic interference. Keep it away from these sources.
I'm using a pushbutton as a toggle switch. Press it and it does "stuff A." Press it again and it does "stuff B." Why am I not able to call my method checkButtons_slow()?
int prev = 0;
int current = 0;
int val4 = 0;
int val5 = 0;
int ledPin = 13;
int prev = 0;
int current = 0;
Servo ZServo;
void setup() {
ZServo.attach(9);
pinMode(pushD3, INPUT_PULLUP);
digitalWrite(3, HIGH);
pinMode(pushD4, INPUT_PULLUP);
digitalWrite(4, HIGH);
pinMode(pushD5, INPUT_PULLUP);
digitalWrite(5, HIGH);
pinMode(pushD6, INPUT_PULLUP);
digitalWrite(6, HIGH);
pinMode(ledPin, OUTPUT);
}
void loop() {
if(digitalRead(3) == LOW) {
current = 1 - current;
}
if(current == 1 && prev == 0) {
checkButtons_slow();
//test: ZServo.write(110);
delay(500); //half a second
}
if(current == 0 && prev == 1) {
ZServo.write(80);
delay(500); //half a second
}
prev = current;
}
Here's my method:
void checkButtons_slow() {
val4 = digitalRead(pushD4);
val5 = digitalRead(pushD5);
if (val4 == LOW) {
ZServo.write(88);
} else if (val5 == LOW) {
ZServo.write(99);
} else {
ZServo.write(91); //GUESSED ON 92; SHOULD TECHNICALLY BE 90
}
}
So the commented out //test: ZServo.write(110); works. What am I missing with the checkButtons_slow();?
If you change void loop() to this then it will work to toggle the method on and off.
void loop() {
if (digitalRead(3) == LOW) {
num_presses++;
delay(500);
}
if ((num_presses % 2) == 0) {
//even
checkButtons_slow();
}
else if(num_presses == 0) {
ZServo.write(90);
}
else {
ZServo.write(85);
}
}
I am using an Arduino Mega to implement a Rectangle pattern movement of a device which mean the length and breadth should decrease each time it reaches the end edges. I have written the code but it is not working
//servo header
#include <Servo.h>
#include <Math.h>
Servo myservo;//servo Object
//motor pin config
int m1r = 9;
int m1f = 10;
int m2f = 12;
int m2r = 11;
//infrared counter
int ir1val=0;//pulse count from irval
int ir2val=0;//pulse count from irval
int len=1;
int bred=2;
//Different sensors used
int temp;
int buzz = 8;
const float radpulse = 0.05;//1 pulse = 0.05 meter
double pulsecount1;//pulse counter;
double pulsecount2;//pulse counter;
int pc1;//integer
int pc2;
void setup() {
// initialize the Motor pin as an output:
pinMode(m1f, OUTPUT);
pinMode(m1r, OUTPUT);
pinMode(m2f, OUTPUT);
pinMode(m2r, OUTPUT);
pinMode(buzz,OUTPUT);
myservo.attach(A0);//BLDC motor attachment
Serial.begin(9600);// standard serial bandwidth of all modules
}
void loop()
{
pulsecount1=len/radpulse;
pc1=(int)round(pulsecount1);
pulsecount2=bred/radpulse;
pc2=(int)round(pulsecount2);
pc1=3;
pc2=2;
myservo.write(30);
delay(5000);
labelL1:
if((pc2 == 0)&&(pc1 == 0))
{
goto Stop;
}
while(pc1 > ir1val)
{
temp=((5.0*analogRead(A3)*100)/1024);
if(temp >60)
{
// goto hibernate;
}
/* if((analogread(A14) <= 300 || analogread(A14) >= 400) || (analogread(A15) <= 300) || analogread(A15) == 400))
{
goto Stop;
}*/
digitalWrite(m2f,HIGH);
digitalWrite(m1f,HIGH);
//digitalWrite(m2f,HIGH);
if(digitalRead(A2))
{
ir1val++;
}
}
if(pc1 == ir1val)
{
/*if((analogread(A14) <= 300 || analogread(A14) >= 400) || (analogread(A15) <= 300) || analogread(A15) == 400))
{
goto Stop;
}*/
pc1--;
ir1val=0;
digitalWrite(m1f, HIGH);
digitalWrite(m2f,LOW);
delay(5000);
goto labelB1;
}
labelB1:
while(pc2 != ir1val)
{
temp=((5.0*analogRead(A3)*100)/1024);
if(temp >60)
{
// goto label2;
}
/* if((analogread(A14) <= 300 || analogread(A14) >= 400) || (analogread(A15) <= 300) || analogread(A15) == 400))
{
goto Stop;
}*/
digitalWrite(m1f,HIGH);
digitalWrite(m2f,HIGH);
if(digitalRead(A2))
{
ir2val++;
}
}
if(pc2 == ir2val)
{
/* if((analogread(A14) <= 300 || analogread(A14) >= 400) || (analogread(A15) <= 300) || analogread(A15) == 400))
{
goto Stop;
}*/
pc2=pc2--;
ir2val=0;
digitalWrite(m1f, HIGH);
digitalWrite(m2f,LOW);
delay(5000);
goto labelL1;
}
Stop:
myservo.write(0);// stop servo
digitalWrite(m1f,LOW);// stop motor
digitalWrite(m2f,LOW);//stop motor
digitalWrite(buzz,HIGH);//Buzzer
delay(5000);
goto Stop;
/*
hibernate:
temp=((5.0*analogRead(A2)*100)/1024);
if(temp >40)
{
delay(1000);
goto hibernate;
}
*/}