Loop in simple arduino code is not working - arduino

I am working on simple project, arduino relay controled via mobile phone. I am trying to create a loop, which makes the relay switch between HIGH and LOW only with sending 1 to pin 7. I tried multiple variations but it never works, I am unable to find a mistake. HereĀ“s the code, thanks in advance.`
int relay = 7;
int prev; //previous value
int val; //actual value
void setup()
{
pinMode(relay, OUTPUT);
Serial.begin(9600);
}
void loop()
{
if(Serial.available()>0) //send data when recieved
{
val = Serial.read(); //read value
}
if (val == '1')
{
if (prev == '1')
{
digitalWrite(relay, LOW);
prev=val;
}
else
{
digitalWrite(relay, HIGH);
prev=val;
}
}
else if (val == '0');
{
if (prev == '0')
{
digitalWrite(relay, HIGH);
prev=val;
}
else
{
digitalWrite(relay, LOW);
prev=val;
}
}
}

Current code attempts to read the serial port, and if has something to read sets it into variable val.
In the case of where it has read a value, but doesnt have another value, val is still set to the same value as before.
So it then probally starts toggling the high/low transition as fast as the loop can go.
Instead, you could move the logic code to within the if(Serial.available()>0) statement so that it only gets called when the key is pressed.

Related

Why is my single button LED light control only working sometimes?

I just started getting into Arduino and I put together a very simple led control with one button to change the on and off state. When I push the button the light will come on, but it will only stay on about 50% of the time (so I have to push it multiple times until it actually stays on), and The same thing happens when I try to turn it off.
Is there some problem with my code? Or is it likely to be a wiring issue?
//define variables
int ledPin = 5;
int btnOnPin = 9;
bool isItOn = false;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(btnOnPin, INPUT_PULLUP);
}
void loop() {
if(digitalRead(btnOnPin) == LOW)
{
if(isItOn == false)
{
digitalWrite(ledPin, HIGH);
isItOn = true;
}
else if (isItOn == true)
{
digitalWrite(ledPin, LOW);
isItOn = false;
}
}
}
The button is connected to ground and pin9, the led is connected to ground, and pin5 through a 220ohm resistor.
This should be super simple, but for some reason I can't get it to work properly.
Thanks for any help
try putting the arduiton to sleep for a few milli seconds after changing its val
void loop() {
if(digitalRead(btnOnPin) == LOW)
{
if(isItOn == false)
{
digitalWrite(ledPin, HIGH);
isItOn = true;
}
else if (isItOn == true)
{
digitalWrite(ledPin, LOW);
isItOn = false;
}
sleep(K);
}

How to Make Program Stop after Number of Iterations

So, I have my program here, I am trying to make a program that, we manually pull the wire out and place it back, each time I pull it out, it is 1 iteration and placing it back in is 1 iteration. Whenever it is connected to the port the external LED is dark, when it is pulled out, the external LED is bright. I should be able to do this for 10 iterations and stop once I get there.
The problem is, when I include the while(true); statement in, my external LED does not work, but without the statement in there, the program runs the way I want except that it does not stop working after 10 iterations, any help is appreciated!
#include<EEPROM.h>
const int LED = 12;
const int SWITCH = 4;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(LED, OUTPUT); //LED is always outputting information
pinMode(LED_BUILTIN, OUTPUT); //Built in LED is always outputting information
pinMode(SWITCH, INPUT_PULLUP); //Switch inputs value when in/out of ground
}
void loop() {
// put your main code here, to run repeatedly:
int addr = 0; //Declaring variables
int count = 0;
int seconds;
if (digitalRead(SWITCH) == LOW) { //If wire starts in ground, record values
Serial.println("----Recording----");
while (count <= 10) { //While count value is less than or equal to 10
if (digitalRead(SWITCH) == LOW) { //When wire is connected to 4
count = count + 1; //Add one to count in each iteration
digitalWrite(LED, LOW); //LED light is off in this position
delay(50); //Checks switch state every 0.05 seconds
}
else if (digitalRead(SWITCH) == HIGH) { //When wire isnt connected to 4
count = count + 1; //Add one to count in each iteration
digitalWrite(LED, HIGH); //LED light is on in this position
delay(50); //Checks switch state every 0.05 seconds
}
while (true);
}
}
}
Why it's not working? Let's rewrite your code a little:
while (count <= 10) {
count = count + 1;
digitalWrite(LED, digitalRead(SWITCH));
delay(50);
while (true); // aaand kill it
}
I'm ommiting the race condition if the first digital read returns HIGH and then the second returns LOW (it wouldn't wait for 50ms to stop)
I'd expect something like counting on pin change or so:
while (count <= 10) {
byte = digitalRead(SWITCH);
if (byte != digitalRead(LED))
{
count = count + 1;
digitalWrite(LED, byte);
}
delay(50);
}
while (true); // aaand kill it

Arduino control with serial input more then 1 character

I have the script below and it works.
But I want to control it with a word like on or off instead of one character.
I tried and searched a lot but without success.
/*
Simple LED sketch
*/
int led = 13; // Pin 13
void setup()
{
pinMode(led, OUTPUT); // Set pin 13 as digital out
// Start up serial connection
Serial.begin(115200); // baud rate
}
void loop()
{
if (Serial.available()) {
int ser = Serial.read(); //read serial as ascii integer
if (ser == 'a') { //is this serial byte the ASCII equivalent of 0 through 9?
digitalWrite(led, HIGH); // on
Serial.println("aan");
}
else if (ser == 'u') {
digitalWrite(led, LOW); // off
Serial.println("uit");
}
}
}
Use Serial.readStringUntil(terminator) to read a string from the serial.
The sent string needs to be terminated with a newline character.
Chose Newline in the Arduino IDE's Serial Monitor.
String cmd = "";
void loop()
{
if (Serial.available()) {
cmd = Serial.readStringUntil('\n');
if (cmd == "on") {
digitalWrite(led, HIGH); // on
Serial.println("aan");
}
else if (cmd == "off") {
digitalWrite(led, LOW); // off
Serial.println("uit");
}
}
}
Your problem is that you are using a char you need to declare a string so you can compare that string to the input, the guy above gave a ok/very slacky/too complex for you answer (sorry no offense I am just trying to show contrast between a complex and a easy answer not trying to offend you) and partially is because he did not explain what he is doing and because he is doing unnecessary/useless work. There is a function in c++ called Serial.readString(), much easier then the stuff up there. Assuming your level of programming (no offense) from your question here is a quick review on data types:
String = ""
int = integer number {1,2,3,4,5,6,...}
char = '' <- Notice the difference from String = ""
float = floating point number {1.2,4.5,...}
(These are not all of them, there is more like byte and so on but just make sure you know how to use the above first)
/*
Simple LED sketch
*/
int led = 7; // Pin 13
String ser; //Declare the string that is going to store what your are going to
//write in the serial
void setup(){
// Start up serial connection
//It's good convention to start the serial before pinMode or any other thing
//in setup
Serial.begin(9600); //I don't know why you need such an high baud rate in
//your thing, 9600 for what you have to do is more than fine, this is just at
//what speed the serial is read (in a very general explanation)
pinMode(led, OUTPUT); // Set pin 13 as digital out
//make sure you put in here wheter you want to start the led "on" or "off"
//Eg you want it to start on, then digitalWrite(led, HIGH);
}
void loop(){
ser = Serial.readString();
if(Serial.available() == 0) { //You can also use a while loop if you want to
//This is telling the arduino: if there is something in the serial, then do...
if(ser == "on"){
Serial.println("on");
digitalWrite(led, HIGH);
}else if(ser == "off"){
Serial.println("off");
digitalWrite(led, LOW);
}
}
}
Hope it helped!
Also notice how the above code with
if(Serial.available())
This is a quite WEIRD and SHADY statement and might not work. That's because you are not really telling the int value into the function Serial.available
As the arduino guidelines specify:
int incomingByte = 0; // for incoming serial data
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
}
viewable here

Arduino click, double click and hold button

I am trying to implement three different functions for one button in an Arduino project. Click, double click and hold.
I have to use interrupts and let the system sleep as much as possible, because the final product will have to run on a coin cell for a few months.
#include <Ports.h>
#include <RF12.h>
#include <avr/sleep.h>
#include <PinChangeInt.h>
#include <VirtualWire.h>
ISR(WDT_vect) { Sleepy::watchdogEvent(); }
char *controller;
const int buttonPin = 3;
bool stateSingle = false;
bool stateDouble = false;
bool stateLong = false;
void setup() {
pinMode(13, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(5, OUTPUT);
// vw_set_ptt_inverted(true);
// vw_set_tx_pin(12);
// vw_setup(4000);
//
Serial.begin(9600);
PCintPort::attachInterrupt(buttonPin, wakeUp, HIGH);
}
void wakeUp() {
}
void loop() {
cli();
int i = 0;
while (digitalRead(buttonPin) == HIGH) { // Wait until button is LOW, or has been high for more than 600ms
Sleepy::loseSomeTime(50);
if (i > 12)
break;
i++;
}
if (digitalRead(buttonPin) == HIGH)
longTapAction();
else {
i = 0;
while (digitalRead(buttonPin) == LOW) { // Wait for possible double press
Sleepy::loseSomeTime(50);
if (i > 8)
break;
i++;
}
if (digitalRead(buttonPin) == HIGH) {
doubleTapAction();
while (digitalRead(buttonPin) == HIGH)
Sleepy::loseSomeTime(50);
} else
singleTapAction();
}
}
void singleTapAction() {
stateSingle = !stateSingle;
digitalWrite(5, stateSingle ? HIGH : LOW);
sei();
Sleepy::powerDown();
}
void doubleTapAction() {
stateDouble = !stateDouble;
digitalWrite(6, stateDouble ? HIGH : LOW);
sei();
Sleepy::powerDown();
}
void longTapAction() {
stateLong = !stateLong;
digitalWrite(7, stateLong ? HIGH : LOW);
sei();
Sleepy::powerDown();
}
The problem is that this is not always correctly working.
Because I'm using interrupts, millis() inside void loop() is not reliable, for some reason.
For any double click, and for any hold action, the single click function also gets called. I suspect this is due to multiple interrupts firing, but I have no way to test this. Also, sometimes, the double click seems to need only one click. Is my thinking wrong, did I forget something?
If you are seeing singleTapAction and doubleTapAction triggering too often, the problem could be that your method doesn't really debounce the button inputs, meaning you may read spurious noise on any click as a single press or double press. E.G your first while loop will exit almost immediately if there is a noisy input, which makes the following behavior difficult to predict.
https://www.arduino.cc/en/Tutorial/Debounce
If you have a look at the linked example on the arduino site - a possible solution is to record the period of time an input has been present and ignore any inputs of less than a certain period. Modifying your code to do this could stop the spurious calls.

Arduino Micro triggering with no contact

I'm using an Arduino Micro to trigger specific events on a front end I have. For some reason, however, some of the key combos are just triggering randomly. I am not even touching the arduino when this happens.
I have it set up so when you press a button, it only triggers the key stroke once, rather than repeating it while the button is pressed.
Am I missing something, or screw something up?
int escButton = 12, valEsc, valEsc2, stateEsc;
int volUp = 11, valVup, valVup2, stateVup;
int volDown = 10, valVdown, valVdown2, stateVdown;
int rebootButton = 9, valReboot, valReboot2, stateReboot;
char ctrlKey = KEY_LEFT_GUI;
char shiftKey = KEY_LEFT_SHIFT;
void pressKeyCombos(int combo){
switch(combo) {
case 1:
Keyboard.press(KEY_ESC);
delay(100);
Keyboard.releaseAll();
break;
case 2:
Keyboard.press(shiftKey);
Keyboard.press(KEY_F1);
delay(100);
Keyboard.releaseAll();
break;
case 3:
Keyboard.press(shiftKey);
Keyboard.press(KEY_F3);
delay(100);
Keyboard.releaseAll();
break;
case 4:
Keyboard.press(shiftKey);
Keyboard.press(KEY_F2);
delay(100);
Keyboard.releaseAll();
break;
}
}
void setup() {
pinMode(escButton, INPUT);
stateEsc = digitalRead(escButton);
Keyboard.begin();
}
void loop(){
valEsc = digitalRead(escButton);
delay(10);
valEsc2 = digitalRead(escButton);
valVup = digitalRead(volUp);
delay(10);
valVup2 = digitalRead(volUp);
valVdown = digitalRead(volDown);
delay(10);
valVdown2 = digitalRead(volDown);
valReboot = digitalRead(rebootButton);
delay(10);
valReboot2 = digitalRead(rebootButton);
if (valVup == valVup2) {
if (valVup != stateVup) {
if (valVup == LOW) {
pressKeyCombos(4);
}
}
stateVup = valVup;
}
if (valVdown == valVdown2) {
if (valVdown != stateVdown) {
if (valVdown == LOW) {
pressKeyCombos(3);
}
}
stateVdown = valVdown;
}
if (valReboot == valReboot2) {
if (valReboot != stateReboot) {
if (valReboot == LOW) {
pressKeyCombos(2);
}
}
stateReboot = valReboot;
}
if (valEsc == valEsc2) {
if (valEsc != stateEsc) {
if (valEsc == LOW) {
pressKeyCombos(1);
}
}
stateEsc = valEsc;
}
}
The inputs are high impedance by default. From your description of the beahvior, I can guess that you have not added external pull up/down resistor and that the pins are connected to a normally open switch. The random input would be just as expected and noted in docs for digitialRead():
Note
If the pin isn't connected to anything, digitalRead() can return
either HIGH or LOW (and this can change randomly).
see reference page for digitialRead()
The micro does have the ability to provide an internal pull up.
Your code does not show you have enabled the internal pull up resistors. This is accomplished in a manner that may seem odd, by writing to the input pin. From the Arduino docs:
There are also convenient 20K pullup resistors built into the Atmega
chip that can be accessed from software. These built-in pullup
resistors are accessed in the following manner.
pinMode(pin, INPUT); // set pin to input
digitalWrite(pin, HIGH); // turn on pullup resistors
see Digital Pins
So in your case, add four digitalWrite()'s in the setup() to enable the pull ups and you look good to go.

Resources