Why is my if statement in my program not working? - arduino

I'm trying to make a program so when 2 switches are flipped they light up two 4 pin LEDs, but only when both switches are flipped. I'm trying to make a cool BattleBots arena and thought something like that would be cool to have as a starting sequence! If you spot any extra errors feel free to correct me.
int buttonPin = 13;
int buttonPin2 = 3;
int counter = 0;
int redpin = 11; //select the pin for the red LED
int bluepin =10; // select the pin for the blue LED
int greenpin =9;// select the pin for the green LED
int redpin2 = 7; //select the pin for the 2 red LED
int bluepin2 =6; // select the pin for the 2 blue LED
int greenpin2 =5;// select the pin for the 2 green LED
int pinButton = 13; //the pin where we connect the button
int pinButton2 = 3; //the pin where we connect the button
int val;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(redpin, OUTPUT);
pinMode(bluepin, OUTPUT);
pinMode(greenpin, OUTPUT);
pinMode(redpin2, OUTPUT);
pinMode(bluepin2, OUTPUT);
pinMode(greenpin2, OUTPUT);
Serial.begin(9600);
}
void loop()
{ int buttonState;
buttonState = digitalRead(buttonPin);
int buttonState2;
buttonState2 = digitalRead(buttonPin2);
}
int main()
if (buttonState == LOW && buttonState2 == LOW);
analogWrite(11, 0);
analogWrite(10, 225);
analogWrite(9, 0);
delay(5);
analogWrite(7, 0);
analogWrite(6, 225);
analogWrite(5, 0);
delay(5);
}else{
analogWrite(11, 225);
analogWrite(10, 0);
analogWrite(9, 0);
delay(5);
analogWrite(7, 0);
analogWrite(6, 0);
analogWrite(5, 225);
delay(5);
}

Your code seem to be a cut and paste from several sources, here's what it should look like, I added notes in the code:
const int buttonPin = 13;
const int buttonPin2 = 3;
const int redpin = 11; //select the pin for the red LED
const int bluepin = 10; // select the pin for the blue LED
const int greenpin = 9; // select the pin for the green LED
const int redpin2 = 7; //select the pin for the 2 red LED
const int bluepin2 = 6; // select the pin for the 2 blue LED
const int greenpin2 = 5; // select the pin for the 2 green LED
/*
Unused variables removed:
int counter = 0;
int pinButton = 13;
int pinButton2 = 3;
int val;
*/
void setup() {
pinMode(buttonPin, INPUT);
pinMode(redpin, OUTPUT);
/*
Depending on your hardware you may want to pull-up the button pins:
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
*/
pinMode(bluepin, OUTPUT);
pinMode(greenpin, OUTPUT);
pinMode(redpin2, OUTPUT);
pinMode(bluepin2, OUTPUT);
pinMode(greenpin2, OUTPUT);
Serial.begin(9600);
}
void loop() {
int buttonState;
buttonState = digitalRead(buttonPin);
int buttonState2;
buttonState2 = digitalRead(buttonPin2);
/*
main() function had several issues, it was a function returning an integer without
any return statement and it is never called by your code. Looks like it should be
part of loop() like so:
*/
if (buttonState == LOW && buttonState2 == LOW) {
analogWrite(11, 0);
analogWrite(10, 225);
analogWrite(9, 0);
delay(5);
analogWrite(7, 0);
analogWrite(6, 225);
analogWrite(5, 0);
delay(5);
} else {
analogWrite(11, 225);
analogWrite(10, 0);
analogWrite(9, 0);
delay(5);
analogWrite(7, 0);
analogWrite(6, 0);
analogWrite(5, 225);
delay(5);
}
}

Related

AttachIntrerrupt stops NRF24L01+ from working - ARDUINO

If I remove attachInterrupt(digitalPinToInterrupt(encoder1),readEncoder,RISING); The code works. But once its added, the radio.available doesnt let anything under it run.
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
struct InputData // define stuct
{
int x;
int y;
};
InputData data;
// Motor A connections
int motor_enA = 9;
int motor_in1 = 10;
int motor_in2 = 6;
int encoder1 = 2;
int encoder2 = 3;
int counter = 0;
int angle = 0;
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(1, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
// Set all the motor control pins to outputs
pinMode(motor_enA, OUTPUT);
pinMode(motor_in1, OUTPUT);
pinMode(motor_in2, OUTPUT);
// Turn off motors - Initial state
digitalWrite(motor_in1, LOW);
digitalWrite(motor_in2, LOW);
analogWrite(motor_enA, 255);
pinMode (encoder1, INPUT);
pinMode (encoder2, INPUT);
attachInterrupt(digitalPinToInterrupt(encoder1),readEncoder,RISING);
}
void loop() {
readEncoder();
if (radio.available()) {
radio.read(&data, sizeof(data));
// Serial.println(data.y);
if (data.y > 5) {
digitalWrite(motor_in1, HIGH);
digitalWrite(motor_in2, LOW);
}
else if (data.y < -5) {
digitalWrite(motor_in1, LOW);
digitalWrite(motor_in2, HIGH);
}
else {
digitalWrite(motor_in1, LOW);
digitalWrite(motor_in2, LOW);
}
}
if(counter>1){
counter=0;
angle+=2;
}else if(counter<-1){
counter=0;
angle-=2;
}
Serial.print("Position: ");
Serial.println(angle);
}
void readEncoder()
{
if(digitalRead(encoder1)==HIGH){
int b = digitalRead(encoder2);
if(b>0){
counter++;
}
else{
counter--;
}
}
}
I have tried removing and adding the line, as described above^^
as mentioned by Hcheung, make counter volatile and remove readEncoder(); from loop.
I simplify a bit ISR readEncoder();
volatile int counter = 0;
[....]
void readEncoder() {
//if(digitalRead(encoder1)==HIGH){ //we are precisely here because digitalRead(encoder1) = HIGH !
if(digitalRead(encoder2)) counter++;
else counter--;
}

Some LED fade and others will flash when all LED should flash Adrunio Nano

I am having trouble with my Arduino code and circuit. The goal is to have each LED fade one after another. This is not happening correctly. Some LED will fade up and down properly and then some will blink instead. I have been trying to troubleshoot and here is what I have done and has not fixed it.
used a different board
Swap LEDs
Used different resistors
Swapped pins that blink to pins that faded and the blink will be fading
Moved circuit to a different breadboard
Check that the code is sending the correct light level through the serial monitor
Here is a picture of my board
Here is the code:
const int BUTTON = 2; // Naming switch button pin
const int LED1 = 3; // Namin LED pin
const int LED2 = 4;
const int LED3 = 5;
const int LED4 = 6;
const int LED5 = 7;
const int LED6 = 8;
const int LED7 =9;
const int LED8 = 10;
const int LED9 = 11;
int BUTTONstate = 0; // A variable to store Button Status / Input
int brightness = 0;
int fadeAmount =5;
void setup(){
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);
pinMode(LED7, OUTPUT);
pinMode(LED8, OUTPUT);
pinMode(LED9, OUTPUT);
pinMode (BUTTON, INPUT);
Serial.begin(9600);
}
void loop() {
BUTTONstate = digitalRead(BUTTON); // Reading button status / input
if (BUTTONstate == HIGH) // Condition to check button input
{
FlashingLight();
}
else
{
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);
digitalWrite(LED8, LOW);
digitalWrite(LED9, LOW);
}
}
void FlashingLight()
{
for ( int i = 0; i<=4;i++){
//digitalWrite(LED, LOW);
fading(LED1); //Fades
fading(LED2); // blinks
fading(LED3); //fades
fading(LED4); //fades
fading(LED5); //blinks
fading(LED6); //blinks
fading(LED7); //fades
fading(LED8); //fades
fading(LED9); //fades
delay(1000);
}
}
void fading(int val) {
//brightness =0;
//digitalWrite(LED, LOW);
analogWrite(val,brightness);
for (brightness = 0; brightness <= 150; brightness += 5) {
analogWrite(val,brightness);
delay(30);
Serial.println(brightness);
}
for (brightness = 150; brightness >= 0; brightness -= 5) {
analogWrite(val,brightness);
delay(30);
Serial.println(brightness);
}
delay(100);
//brightness =0;
}
Thank you for your help and let me know if you have any questions,
According to this Arduino.cc reference not all pins of the Arduino Nano are suitable for PWM control (using analogWrite()). On the Nano, only pins 3, 5, 6, 9, 10, 11 can be used to output a PWM signal.

RF transmitted Hex string comparison Vs embedded string Arduino

I am using the VirtualWire library on my Arduino Micro. I am having trouble in comparing the string of HEX received via rf module on pin 2 of the board. When reaching if stringOne == stringVal2, LED3 is always lit. I am not sure where to go from here or where to even begin on reading to figure out converting HEX to a readable comparator.
#include <VirtualWire.h>
#define RX 2
#define LED A0
#define LED2 PIND7
#define LED3 PIND6
void setup(){
Serial.begin(9600);
Serial.print("setup complete");
pinMode(RX, INPUT);
pinMode(LED, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
vw_set_rx_pin(RX);
vw_setup(2000);
vw_rx_start();
}
void loop(){
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)){
int i;
Serial.print("Got:");
for (i = 0; i < buflen; i++){
//Serial.print(buf[i], HEX);
//String stringVal = String('65'+'6c'+'6c'+'6f'+'20'+'*','\0');
String stringVal2 = String('73'+'6F'+'20'+'6C'+'6F'+'6E'+'*', HEX);
String stringOne = String(buf[i], HEX);
Serial.print(stringOne);
Serial.print(' ');
digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(LED,LOW);
delay(1000);
if (stringOne == stringVal2){
digitalWrite(LED2, HIGH);
}
else{
digitalWrite(LED3, HIGH);
}
}
Serial.println();
}
}
Here is the code for my transmitter as well. Using Adafruit Trinket5v 8Mhz
#include <VirtualWire.h>
const int TX = 3;
const int LED = 2;
const int buttonPin = 0; //Yellow Button
const int buttonPin2 =4; //Red Button
int buttonState = 0;
int buttonState2 = 0;
void setup(){
vw_set_tx_pin(TX);
// vw_set_ptt_pin(txpin);
// vw_set_ptt_inverted(false);
vw_setup(2000);
pinMode(LED, OUTPUT); //Signals button press/transmission being sent
pinMode(buttonPin, INPUT);
}
byte count = 1;
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(LED, HIGH);
char msg[7] = {'h','e','l','l','o',' ','#'};
msg[6] = count;
vw_send((uint8_t *)msg, 7);
vw_wait_tx();
count = count + 1;
}
else {
// turn LED off:
digitalWrite(LED, LOW);
}
buttonState2 = digitalRead(buttonPin2);
if (buttonState2 == HIGH){
digitalWrite(LED, HIGH);
char msg[7] = {'12','11','10','9','8','7','6'};
msg[6] = count;
vw_send((uint8_t *)msg, 7);
vw_wait_tx();
count = count + 1;
}
else {
digitalWrite(LED, LOW);
}
}
You are comparing a string that is just text to an array of chars. These will never evaluate out properly
If you want to initialise the stringVal2 properly to compare it directly you need to do this:
char strVal[] = {0x73, 0x6f, 0x20, 0x6c, 0x6f, 0x6e};
String stringVal2 = String(strVal);
This prints out the same as the other string you brought in.
Also, since you are basically just catching a char array then you might as well use a char array directly to evaluate the results and convert to a string when you need to output to a human

Controlling a motor with potentiometer

I am trying to use my arduino and a potentiometer to make a motor spin one way when the potentiometer is past 0 and spin the other when the potentiometer is past 0 the other direction. The code is working on the SensorValue < 512 side but not on the >507 side.
const int analogInPin = A1; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; //
const int analogOutPin_2 = 11; //
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(analogInPin);
if (sensorValue < 507) {
analogWrite(analogOutPin, LOW);
outputValue = map(sensorValue, 0, 512, 0, 255);
analogWrite(analogOutPin_2, outputValue);
}
if (512 > sensorValue) {
analogWrite(analogOutPin_2, LOW);
outputValue = map(sensorValue, 512, 1023, 0, 255);
analogWrite(analogOutPin, outputValue);
}
else {
}
delay(2);
}
For solving issues such as this. I recommend a few strategic prints and a for loop to simulate the data range, reveals the following code with a few tweaks works better
const int analogInPin = A1; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; //
const int analogOutPin_2 = 11; //
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
delay(1000);
Serial.println("Begin1");
}
void loop() {
// sensorValue = analogRead(analogInPin);
for (sensorValue = 0; sensorValue < 1024; sensorValue++) {
Serial.print("sensorValue = ");
Serial.print(sensorValue);
if (sensorValue < 507) {
outputValue = map(sensorValue, 0, 512, 255, 0);
Serial.print(" Forward : ");
Serial.print(outputValue);
analogWrite(analogOutPin, LOW);
analogWrite(analogOutPin_2, outputValue);
} else if (512 < sensorValue) {
outputValue = map(sensorValue, 512, 1023, 0, 255);
Serial.print(" Reverse : ");
Serial.print(outputValue);
analogWrite(analogOutPin_2, LOW);
analogWrite(analogOutPin, outputValue);
}
Serial.println();
}
while(1);
delay(1000);
}

Stepper motor with mp3 shield Arduino

For a college project I want to make a stepper motor combined with a mp3shield.
It is al working fine, but when I put them in the same code, is it acting really weird and my stepper motor starts shaking.
My code is below:
int sensor = A0;
int led1 = A1;
int led2 = A2;
int led3 = A3;
int led4 = A4;
int led5 = A5;
int button = 12;
int dirpin = 10;
int steppin = 11;
int buttonState;
void setup()
{
Serial.begin(9600);
pinMode(sensor, INPUT);
pinMode(button, INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(dirpin, OUTPUT);
pinMode(steppin, OUTPUT);
}
void loop()
{
buttonState = digitalRead(button);
Serial.println(buttonState);
delay(10);
if(buttonState == HIGH)
{
rotateDeg(360, .1);
playSong(6);
}
}
The code for letting the stepper motor turn is the following:
void rotateDeg(float deg, float speed){
int dir = (deg > 0)? HIGH:LOW;
digitalWrite(dirpin,dir);
int steps = abs(deg)*(1/0.225);
float usDelay = (1/speed) * 70;
for(int i=0; i < steps; i++){
digitalWrite(steppin, HIGH);
delayMicroseconds(usDelay);
digitalWrite(steppin, LOW);
delayMicroseconds(usDelay);
}
}
The code for playing a song:
#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <SFEMP3Shield.h>
SdFat sd;
SFEMP3Shield MP3player;
void playSong(int song){
Serial.begin(9600);
//start the shield
sd.begin(SD_SEL, SPI_HALF_SPEED);
MP3player.begin();
//start playing track 1
MP3player.playTrack(song);
}
I use a driver for the stepper motor with 12V 1000mA input. If I want to play a song without the motor, is works fine.
If I want to rotate the motor without sound, it works fine as well.
Hopefully you are able to help.
Thank you in advance,
NiSh
EDIT:
I have also some LEDs, which work fine. When I press a button there will be a random pattern.
When I put it in the code like:
if(buttonState == HIGH)
{
setLed();
rotateDeg(360, .1);
}
It works like I want that it works. But if I try:
if(buttonState == HIGH)
{
playSound(4);
rotateDeg(360, .1);
}
the motor starts shaking when playing the song, and never makes that 360 degree turn.

Resources