I have 4 esc's hooked up and I want to control them using SoftwareServo.
I am able to control them with Servo.h by doing so:
for(int i = 0; i < 4; i++)
escS[i].attach(escPins[i]);
for(int i = 0; i < 4; i++)
escS[i].write(180);
delay(2000);
for(int i = 0; i < 4; i++)
escS[i].write(90);
delay(2000);
escS[0].write(0);
escS[1].write(0);
escS[2].write(0);
escS[3].write(0);
I have to do that to initialize the ESC's, but I am unable to achieve the same affect using SoftwareServo.h; I must be doing something wrong:
for(int i = 0; i < 4; i++)
escSS[i].attach(escPins[i]);
for(int i = 0; i < 4; i++)
escSS[i].write(180);
SoftwareServo::refresh();
delay(2000);
for(int i = 0; i < 4; i++)
escSS[i].write(90);
SoftwareServo::refresh();
delay(2000);
escSS[0].write(0);
escSS[1].write(0);
escSS[2].write(0);
escSS[3].write(0);
I was able to solve this by arming the ESC's using the Servo.h servos and then detaching them and attaching the SoftwareServo.h SoftwareServos to the same esc's and writing to them from then on.
You need to call SoftwareServo::refresh() constantly during those delay() events. The code example here skips it for 2000 ms at a time.
Related
I wrote a program for Arduino UNO with attached Funshield, which will animate the following pattern on the four vertical LEDs.At any given moment, exactly one LED (of four) is turned on (we are starting with the topmost one). In each step of the animation, the active LED moves one slot down. When it hits the bottom, it bounces and moves upwards again, until it reaches top. The animation repeats itself forever.
One step of the animation takes exactly 300ms
#include <funshield.h>
int ledPin[] = {led1_pin, led2_pin, led3_pin, led4_pin};
void setup() {
for (int i = 0; i<5; i++)
pinMode(ledPin[i], OUTPUT);
}
void loop() {
int i = 0;
for (i = 5-1; i>=0; i--)
digitalWrite(ledPin[i], LOW);
delay(1000);
digitalWrite(ledPin[i], HIGH);
for (i; i<5; i++) {
digitalWrite(ledPin[i], LOW);
delay(500);
digitalWrite(ledPin[i], HIGH);
}
}
if i using the delay() function, it works perfectly,but when i used millis() function , 4 LEDs light up at the same time.I would like to know what is causing the animation to stall.
#include "funshield.h"
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 300;
const byte liu = 4;
int ledPin[] = { led1_pin, led2_pin, led3_pin, led4_pin };
void setup() {
for (int i = 0; i < 5; i++)
pinMode(ledPin[i], OUTPUT);
startMillis = millis();
}
void loop()
{
currentMillis = millis();
if (currentMillis - startMillis >= period)
int i = 0;
for (int i = 0; i > 4; i++) {
digitalWrite(liu, !digitalRead(liu));
digitalWrite(ledPin[i], LOW);
digitalWrite(ledPin[i], HIGH);
startMillis = currentMillis;
}
for (int i = 4; i >= 0; i--) {
digitalWrite(liu, !digitalRead(liu));
digitalWrite(ledPin[i], LOW);
digitalWrite(ledPin[i], HIGH);
startMillis = currentMillis;
}
}
first of all unsigned long nowTime; should be at the top, outside of your loop
secondly, you need to use nowTime = millis(); for the specific time that you want to record the current time (this should be before you use the (millis()-nowtime>300)
and lastly, remove (unsigned long)
from this line
if(unsigned long)(millis()-nowtime>300)
for further clarification on how to use millis, read this article on arduino's official website
I'm working on a program for an addressable LED strip. It is working and at this point I'm trying to make my code better. I have 3 LED strips and I made a function which all three has to do. In the function I want to specify which one needs to be updated so I used attributes, but this doesn't seem to work. I can't find this on the FastLed documentation.
//Number of leds powered
int led_state_1 = 0;
int led_state_2 = 0;
int led_state_3 = 0;
// This is an array of leds. One item for each led in your strip.
CRGB leds1[NUM_LEDS];
CRGB leds2[NUM_LEDS];
CRGB leds3[NUM_LEDS];
void CheckAndUpdateLed(CRGB LedArray, int led_state){
resetLedStrip(LedArray);
for(int whiteLed = 0; whiteLed < led_state; whiteLed = whiteLed + 1) {
// Turn our current led on to white, then show the leds
LedArray[whiteLed] = CRGB::White;
// Show the leds (only one of which is set to white, from above)
FastLED.show();
}
}
When I change LedArray to leds1 it is working. I'm calling the function as CheckAndUpdateLed(leds1, led_state_1);
I think my question was a bit unclear sorry for that. I came up with another way of doing this. Instead of 1 led strip I check them all in the same function.
#define NUM_STRIPS 3
#define NUM_LEDS_PER_STRIP 15
CRGB leds[NUM_STRIPS][NUM_LEDS_PER_STRIP];
int led_states[] = {0, 0, 0};
void CheckAndUpdateLeds(){
//resets the leds to black
resetLedStrips();
// This outer loop will go over each LED strip, one at a time
for(int x = 0; x < NUM_STRIPS; x++) {
// This inner loop will go over each led in the current strip, one at a time till the amount of light is the same as in the led_state
for(int led = 0; led < led_states[x]; led = led + 1) {
// Turn our current led on to white, then show the leds
leds[x][led] = CRGB::White;
FastLED.show();
}
}
}
void resetLedStrips(){
for(int x = 0; x < NUM_STRIPS; x++) {
for(int led = 0; led < NUM_LEDS_PER_STRIP; led = led + 1) {
leds[x][led] = CRGB::Black;
}
}
}
I am writing a piece of code in the Arduino IDE. I have an IF statement which has a long comparison of arrays to see if they are all equal to 0. If they are then carry out the code within the statement.
I'm not sure if this is possible but thought I would ask if the code can be simplified. For all the other code I have been able to use for loops, but I cannot see this working with this statement.
The statement will have about 20 arrays to compare in the end.
if (Tbit[0] == 0 && Tbit[1] == 0 && Tbit[2] == 0 && Tbit[3] == 0){
digitalWrite(Tout[0], HIGH);
digitalWrite(Tout[1], HIGH);
}
EDIT UPDATE
I have tried PiNaKa30 suggestion below which I thought would work but still the outputs for the HIGH statement are not being carried out. Below is the full void loop code with my code above commented out and PiNaKa30 codes added.
The bit being read at the beginning only goes HIGH '1' for 1 second when a command input to the arduino is received. Normal state is 0
void loop() {
cmri.process();
for (int i=0; i<2; i++){
Tbit[i] = (cmri.get_bit(i));
}
for (int i=0; i<2; i++){
if (Tbit[i] == 1){
digitalWrite(Tout[i], !Tbit[i]);
}
}
// if (Tbit[0] == 0 && Tbit[1] == 0){
// digitalWrite(Tout[0], HIGH);
// digitalWrite(Tout[1], HIGH);
// }
for(int i=0; i < 2; i++){
val = val || Tbit[i]; // Even a single 1 in Tbit will make val=1
}
if(val == 0){
digitalWrite(Tout[0], HIGH);
digitalWrite(Tout[1], HIGH);
}
}
You can maintain a variable which will be the OR of all values of the array. This variable must be updated as soon as any value changes in the array, and be used in your if statement.
int val = 0; // initialize
updateVal(){
for(int i=0;i<Tbit.length;i++)
val = val || Tbit[i]; // Even a single 1 in Tbit will make val=1
}
if(val == 0){
digitalWrite(Tout[0], HIGH);
digitalWrite(Tout[1], HIGH);
}
I am assuming here that Tbit can only have values 0 and 1. Otherwise change the function slightly to suit your needs.
Got it to work with the help of PiNaKa30 suggestion and a little tweeting.
void loop() {
val = 0;
cmri.process();
for (int i=0; i<2; i++){
Tbit[i] = (cmri.get_bit(i));
}
for (int i=0; i<2; i++){
if (Tbit[i] == 1){
digitalWrite(Tout[i], !Tbit[i]);
}
}
for(int i=0; i < 2; i++){
val = Tbit[i]; // Even a single 1 in Tbit will make val=1
}
if(val == 0){
digitalWrite(Tout[0], HIGH);
digitalWrite(Tout[1], HIGH);
}
}
Sometimes when you looking for an exception its easier to start from the other side:
I would have divided it into two parts
first create a variable to tell us result of a check
bool correct=1;
next check each if it contains any other then correct value
for(uint8_t i=0; i<**Number of array members**;i++) {
Now check if any of them are wrong, and if yes assume incorrect
if(Tbit[i]!=0) correct=0;
and now only process your code if none was incorrect
if(correct) ...
Which would at the end look like:
void loop() { // or any while statement you would have it in
cmri.process();
for (int i=0; i<2; i++){
Tbit[i] = (cmri.get_bit(i));
}
bool correct=1;
for(uint8_t i=0; i<**Number of array members**;i++) {
if(Tbit[i]!=0) correct=0;
}
if(correct) {
digitalWrite(Tout[0], HIGH);
digitalWrite(Tout[1], HIGH);
}
}
I am trying to blink an Array of LEDs hooked up to a pin on the Arduino.
When I upload the code onto my Arduino :
//array of pins
int allLEDPins[4] = {2, 3, 4, 5};
//the chase function
void Chaster(int* anArray) {
for (int i = 0; i < 5; i++) {
digitalWrite(allPins[i], HIGH);
delay(200);
digitalWrite(allPins[i], LOW);
delay(200);
}
}
//setup pins
void setup() {
pinMode(allPins[0], OUTPUT);
pinMode(allPins[1], OUTPUT);
pinMode(allPins[2], OUTPUT);
pinMode(allPins[3], OUTPUT);
}
void loop() {
Chaster(allLEDPins);
}
The loop function does not loop. I am using an Arduino Zero in Arduino IDE 1.6.8 on my Windows 10 machine. Thank you in advance.
You are accessing an index out of range in the for loop inside the function Chaster. Note that your array allLEDPins have only 4 elements and you tried to access allLEDPins[4] while the last element is allLEDPins[3]. This causes an error during running time.
In order to fix that, replace for (int i = 0; i < 5; i++) by for (int i = 0; i < 4; i++)
I've written the following code to light up a row of LEDs one at a time.
int ledPins[] = {7,8,9,10,11,12,13};
void setup() {
for (int i = 0; i < sizeof(ledPins); i++) {
pinMode(ledPins[i], OUTPUT);
}
}
void loop() {
for (int i = 0; i < sizeof(ledPins); i++) {
digitalWrite(i, HIGH);
delay(1000);
digitalWrite(i, LOW);
delay(1000);
}
}
The above works fine. However after completing the for loop there is a long delay (about 10 seconds) before it repeats.
Why would there be such a long delay? Is this expected or is it a problem with my code?
the function sizeof(array) return the size of the array in the memory, in bytes. and because sizeof(int) is probably not 1, you get a larger value than expected.
sizeof can be use to determine the number of elements in an array, by taking the size of the entire array and dividing it by the size of a single element.
so this line:
for (int i = 0; i < sizeof(ledPins); i++) {
should be rewritten as:
for (int i = 0; i < sizeof(ledPins) / sizeof(int); i++) {
see:
http://en.wikipedia.org/wiki/Sizeof
In this case as the array has a fixed size it's simpler to do this:
#define NO_LEDS 7
int ledPins[NO_LEDS] = {7,8,9,10,11,12,13};
void setup() {
for (int i = 0; i < NO_LEDS; i++) {
pinMode(ledPins[i], OUTPUT);
}
}
Or move the calculation of the array's size into a variable and use that.
int ledPins[] = {7,8,9,10,11,12,13};
int noLeds;
void setup() {
noLeds = sizeof(ledPins) / sizeof(int);
for (int i = 0; i < noLeds; i++) {
pinMode(ledPins[i], OUTPUT);
}
}