Using RGB Lights with Arduino Struct - struct

I am attempting to use a struct with Arduino to turn on multiple RGB LEDs to specific colors. With this sample code, I created a struct to hold a red pin num, a blue pin num, a blue value, and a red value. I am only using two of the 3 pins on the LED since I only need the colors red, blue, and purple for my application. When I run this code, the incorrect light turns on and in the incorrect color. I am not sure if I correctly understand how to use a struct in the Arduino environment. I used this source http://playground.arduino.cc/Code/Struct to find the basic syntax for a struct in Arduino. I am using it similar to how one might use an object in OOP. I am looking for clarity as to how to use the struct in Arduino and specifically I am not able to get the expected result. I expect for the LED that is connected to pin3 and pin4 to light up purple (as its red and blue pins are both set to HIGH) but instead the LED connected to pin1 and pin2 will light up red (as if pin1 is set to HIGH). Moreover, when I remove the print statements nothing turns on at all (even though this is the only change made). I have checked my wiring countless times and have determined that it is not a hardware issue. Thank you for any help that you can provide.
struct light {
int redPin ;
int bluePin;
int redValue;
int blueValue;
};
light light1;
void setup() {
Serial.begin(9600);
pinMode(light1.redPin, OUTPUT);
pinMode(light1.bluePin, OUTPUT);
light1.redPin = 3;
light1.bluePin = 4;
light1.redValue = HIGH;
light1.blueValue = HIGH;
}
void loop() {
Serial.print(light1.redPin);
Serial.println(light1.redValue);
Serial.print(light1.bluePin);
Serial.println(light1.blueValue);
digitalWrite(light1.redPin, light1.redValue);
digitalWrite(light1.bluePin, light1.blueValue);
}

As Pawel suggested, you are doing things in the wrong order. This would make a lot more sense:
light1.redPin = 3;
light1.bluePin = 4;
light1.redValue = HIGH;
light1.blueValue = HIGH;
pinMode(light1.redPin, OUTPUT);
pinMode(light1.bluePin, OUTPUT);
I am not sure if I correctly understand how to use a struct in the Arduino environment.
It is exactly the same as in C++.
but instead the LED connected to pin1 and pin2 will light up red (as if pin1 is set to HIGH)
Your serial prints will set pin D1 (Tx) to an output and you are seeing your serial prints as turning on the pins.
when I remove the print statements nothing turns on at all
As expected, as you are not sending data to those pins.
The default for an uninitialized global variable is zero, so I would expect that you have set pin D0 (the first pin, labelled Rx) to be an output, and then you are writing to it.
(Edited to add)
Actually, once you have done a Serial.begin the serial hardware takes over pins 0 and 1, and thus attempts to write to them fail.
The output you see on pin D1 is the Serial.print as I mentioned before, and the output on pin D0 is just the internal pull-up used to keep Rx high in the event that you are not using it just now. If you plug in an LED you will see that D0 is duller than D1.

Related

FreeRTOS cannot poll input pins when using vTaskDelayUntil()

I'm facing weird behavior with FreeRTOS code.
Especially when using vTaskDelayUntil() and vTaskDelay()
I'm trying to read an input pin from my PIR sensor.
On the scope I see that the PIR is holding 3.3v high for at least 1 second.
The code below only reads my PIR input when I comment out the ' vTaskDelayUntil' line. As soon as I activate that line, PINC register is always 0.
Also when I'm sure there is 3.3v on my input pin.
static void TaskStatemachine(void *pvParameters)
{
(void) pvParameters;
TickType_t xLastWakeTime;
const TickType_t xFrequency = 100;
xLastWakeTime = xTaskGetTickCount();
for(;;)
{
printf("PINC.1 = %d\n", (PINC & (1<<1) ));
vTaskDelayUntil( &xLastWakeTime, ( xFrequency / portTICK_PERIOD_MS ) );
}
}
What is happening here?
I changed xFrequency to different values, but without any luck.
As an experiment, simplify the output thus:
putchar( (PINC & (1<<1)) == 0 ? '0' : '1' ) ;
You will then get a continuous stream of 1 or 0.
If that works with or without the delay, then it seems likely that that the task has too small a stack to support printf(). Try increasing the stack and putting the printf() back in.

Change servo angle based on Bluetooth input

I'm using an app to either turn on an LED or change the angle of a micro servo depending on which button is pressed (using Arduino). My code works for the LED (while the button is pressed, the LED is on) but nothing happens when I press the button meant to change the angle of the servo to 40.
// Bluetooth serial:
#include <SoftwareSerial.h> // import the serial library
// setup the bluetooth coms
SoftwareSerial BTSerial(8,7);
#include <Servo.h>
int servoPin = 0;
Servo servo;
int angle = 0; // servo position in degrees
int input = 0;
int led2 = 13;
void setup() {
// put your setup code here, to run once:
servo.attach(servoPin);
Serial.begin(9600); // coms w/ computer
BTSerial.begin(9600); // coms w/ Bluetooth
pinMode(led2, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if (BTSerial.available())
{
input = BTSerial.read();
digitalWrite(led2, LOW);
switch(input) {
case 'E':
angle = 40;
break;
case 'C':
digitalWrite(led2, HIGH);
break;
}
servo.write(angle);
}
}
The input is right as I checked by also turning the LED on in case 'E' where it worked as normal. I had also tried using servo.write() within the case function as well but this didn't work either.
case 'E':
servo.write(40);
break;
You cannot use digital pins 0 or 1 as input:
As mentioned, those are serial send and receive pins. If you power your computer through USB, it can interfere if you try to use them, since it's reading from both the USB to Serial and the pin. Also you have an issue with anything connected (well, not anything, but remove it just to be safe) when you're trying to program. If you use the pins as intended, and program then run it off battery, it should be no problem at all.
Most of us stay away from it because it's a bit of a hassle
Depending on your Arduino model, servo.attach only supports pins 9 and 10:
Note that in Arduino 0016 and earlier, the Servo library supports only servos on only two pins: 9 and 10.
Or you could just use one of those anyway.

Serial monitor constantly say 0 when using arduino and sound sensor

I am having trouble with the arduino sound sensor and LEDs. I keep on getting the value of 0 in my serial monitor, the same thing happens with another sound sensor that I have. I am currently trying to make it light up the LEDs based on the sound but with the serial monitor reading 0 it will not activate the code. There should be a picture attached. The lights on the sound sensor is lighting up so I know the GND and 5V is working. Since it is hard to tell I am using 330 ohm resisters. I got the sound sensor from an elegoo starter kit, so I know it might be cheap. The picture is in the link at the end. Thank you.
int MicPin = A0;
int MicValue1 = 0;
int MicValue2 = 0;
int led1 = 2;
int led2 = 4;
int led3 = 6;
int led4 = 8;
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(MicPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
MicValue1 = analogRead(MicPin);
Serial.println(MicValue1);
delay(1);
MicValue2 = analogRead(MicPin);
Serial.println(MicValue2);
if (MicValue1 - MicValue2 > 1) {
digitalWrite(led1, HIGH);
delay(2000);
}
else {
digitalWrite(led1, LOW);
}
}
enter image description here
I assume that you have a simple analog-output sensor module that provides 10bit analog values based on the environment volume level. If this assumption is correct, you wired all pins correctly and the received value is always out of range or at the maximum, you maybe had to integrate a resistor to get a valid value. Try a small resistor and increase the resistance until you receive suitable values. Maybe the documentation of your module provides further information.
There is a small rotating knob connected to the sensor on which we need to rotate to adjust particular resistance values, you can see different values in serial monitor out.
Useful link

Servo continuous rotation Arduino Serial

I'm working on a Bluetooth controlled Arduino robotic arm.
I want that when I send an integer, a servo moves, and when I send another Int, it stops. All I have found on forums are systems where the servo moves to a specific position, but I want it to really rotate, by incrementing its angle.
Here is my code, which doesn't work:
#include <Servo.h>
int val;
int angle;
int charsRead;
char buffer[10];
Servo servo;
void setup() {
Serial.begin(9600);
servo.attach(6);
angle = servo.read();
}
void loop() {
servo.write(170);
serialCheck();
if(val == 4021){
servo.write(angle++);
delay(50);
}
}
else if(val == 4022){
servo.write(angle);
}
serialCheck();
}
void serialCheck(){
while(Serial.available() > 0){
charsRead = Serial.readBytesUntil('\n', buffer, sizeof(buffer) - 1);
buffer[charsRead] = '\0';
val = atoi(buffer);
Serial.println(val);
}
}
The app I use basically sends '4021' when I long press a button, and sends '4022' when I release it.
I've been working on this for hours and I haven't found anyone on any forum who has had the same issue...
Please help.
Your problem lies here:
void loop() {
servo.write(170);
...
}
What this piece of code does is set servo angle to 170 at every iteration of loop. As this will happen thousands times per second, it will essentially overwrite any servo settings introduced via serial commands. What you should do is to move servo.write(170); to setup(), before servo.read(), which I think will yield middle position currently, or even some undefined result. According to Arduino documentation it will:
Read the current angle of the servo (the value passed to the last call
to write()).
And BTW: servo has limited range of movement, so you should check if the desired angle doesn't exceed servo limits.

Computer to Arduino mega via serial changing values

to start off, this might not be a problem with arduino code or arduino, but I figured I would post here because I really just can not figure out what is wrong.
I am working on this project just for fun to send key strokes from the keyboard, through the computer, and out through the USB to my arduino mega. No additional hardware is here, just the computer, the arduino, and the USB cable.
I am using Microsoft Visual Studio Express 2012 to write code to receive key strokes and send them to the USB. This is the code I am using:
#include "stdafx.h"
#include "conio.h"
using namespace System;
using namespace System::IO::Ports;
int main(array<System::String ^> ^args)
{
String^ portName;
String^ key;
int baudRate=9600;
Console::WriteLine("type in a port name and hit ENTER");
portName=Console::ReadLine();
//arduino settings
SerialPort^ arduino;
arduino = gcnew SerialPort(portName, baudRate);
//open port
try
{
arduino->Open();
while(1)
{
int k = getch();
key = k.ToString();
Console::WriteLine(key);
arduino->Write(key);
if (k == 32)
return 0;
}
}
catch (IO::IOException^ e )
{
Console::WriteLine(e->GetType()->Name+": Port is not ready");
}
}
This code works fine, and sends commands to the arduino. I might as well ask this as well, but after 35 key strokes it just stops sending key strokes, I am unsure as to why, but that is not an arduino problem (I don't think).
So when the certain value for key gets sent to the arduino, it changes. For example, the values that are assigned to the variable key for pressing the number 1 and 2 are 49 and 50, respectively. However, when they get sent to the arduino, the values are different some how. 1 is now 57, and 2 is now 48. I am unsure as to why this is happening. I tried 4 and 5 and they both have their values shift down 2 like the key 2. This is the code I have on the arduino:
int ledPin = 13;
int key=0;
int c;
void setup()
{
pinMode(ledPin, OUTPUT); // pin will be used to for output
Serial.begin(9600); // same as in your c++ script
}
void loop()
{
if (Serial.available() > 0)
{
key = Serial.read(); // used to read incoming data
if (key == 57)
{
digitalWrite(ledPin, HIGH);
}
else if (key == 48)
{
digitalWrite(ledPin, LOW);
}
}
c = key;
Serial.println(c);
}
As of right now it is just to switch a light on and off. I am hoping to involve many more keys and having the values be consistent would be very convenient. Anyways, if anyone could help me with why the values are different that would be awesome. I am not completely new to programming but I am certainly no expert and have not gotten too far into advanced stuff.
Thank you for any help or advice.
This has to do with what you are sending through visual studio. You are converting a keypress to its ASCII value, then converting that ASCII value to string, then sending that string through serial. The arduino is expecting a number, not a string.
For example, if you press the 1 key, your visual studio code converts that to an ASCII number 49, which is then converted to string "49", which the Arduino receives - but since you are sending "49", which is a "4" and an "9", the Arduino is reading 9 which corresponds to 57, as you have seen.
Similarly, pressing 2 converts it to "50", and the Arduino reads "0" which corresponds to the value 48 which you were getting.
To fix this, send the number directly, don't convert it into a string.

Resources