I am sending the correct command and I can see it on the scope.However, I don't get 0x01 response(I keep getting 0xff) from SDcard(SDHC)(SanDisk Ultra 40MB/s ...16GB) I am using the following code:
uint8_t cmd0[6]={0x40,0x00,0x00,0x00,0x00,0x95};
uint8_t dumb=0xff;
FATFS_CS_HIGH;
for (int i = 0; i < 10; i++) {
HAL_SPI_Transmit(&hspi1, &dumb, 1, 10);
while (HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY);
}
FATFS_CS_LOW;
HAL_SPI_Transmit(&hspi1, cmd0, 6, 10);
while (HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY);
for (int i = 0; i < 10; i++) {
HAL_SPI_Transmit(&hspi1, &dumb, 1, 10);
while (HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY);
}
HAL_SPI_Receive(&hspi1, &c, 1, 10);// I keep getting 0xff
Here is the configuration I am using for SPI from cubeMX
Related
I am trying to work on an Arduino project and for some reason, I cannot get it to store the data on the Adafruit Metro M4 controller or data logger shield (saving so that when reconnected, the data entered would still be there).
I have an SD card for the TFT LCD, I would appreciate any help, not sure what I'm missing.
Here is basically what I have so far related to EEPROM functions and it seems to compile without errors but does not work for EEPROM. If I run it on a different board (Arduino Uno) it seems to activate but then the issue is that the code is intended to run only on SAMD21 or SAMD51 boards.
Is there a way to adapt the EEPROM functions for the Metro M4 or can any changes be made to get it functional? Please let me know if there are any questions, I appreciate any and all help!
void readAllConfiguration() {
readLogs();
AveragePeriodDuration = EEPROM.read(ADDR_AVERAGE_DAYS);
LogId = EEPROM.read(ADDR_LOGGED_COUNT);
Serial.print("Lod ID mem: ");
Serial.println(LogId);
if(LogId > 100)
LogId = 0;
if (AveragePeriodDuration > 100)
AveragePeriodDuration = 28;
}
bool checkResetEEPROM() {
uint8_t number = EEPROM.read(ADDR_MAGIC_NUMBER);
Serial.print("Magic number: ");
Serial.println(number);
if (number != MAGIC_NUMBER) {
for (uint8_t i=0; i<5; i++) {
saveLong(ADDR_START_TIME[i], 0);
saveLong(ADDR_END_TIME[i], 0);
for(uint8_t x=0; x<5; x++)
saveString(ADDR_SYMPTOMS[i][x], "");
}
EEPROM.write(ADDR_PERIOD_STATE, 0);
EEPROM.write(ADDR_AVERAGE_DAYS, 28);
EEPROM.write(ADDR_MAGIC_NUMBER, MAGIC_NUMBER);
EEPROM.commit();
Serial.println(EEPROM.read(ADDR_MAGIC_NUMBER));
return true;
}
return false;
}
void readLogs() {
if (LogId == 0)
return;
for (uint8_t i=0; i<5; i++) {
LogCycle[i].StartTime = readLong(ADDR_START_TIME[i]);
LogCycle[i].EndTime = readLong(ADDR_END_TIME[i]);
for (uint8_t x=0; x<5; x++)
LogCycle[i].symptoms[x] = readString(ADDR_SYMPTOMS[i][x]);
}
}
void saveString(int addr, const String &strToWrite) {
byte len = strToWrite.length();
for (int i = 0; i < len; i++){
EEPROM.update(addr + i, strToWrite[i]);
}
EEPROM.commit();
}
String readString(int addr) {
char read_data[16];
for (int i = 0; i < 16; i++)
read_data[i] = EEPROM.read(addr + i);
return String(read_data);
}
uint32_t readLong(int addr) {
long Byte4 = EEPROM.read(addr);
long Byte3 = EEPROM.read(addr + 1);
long Byte2 = EEPROM.read(addr + 2);
long Byte1 = EEPROM.read(addr + 3);
return ((Byte4 << 0) & 0xFF) + ((Byte3 << 8) & 0xFFFF) + ((Byte2 << 16) & 0xFFFFFF) + ((Byte1 << 24) & 0xFFFFFFFF);
}
void saveLong(int addr, uint32_t val) {
byte Byte4 = ((val >> 0) & 0xFF);
byte Byte3 = ((val >> 8) & 0xFF);
byte Byte2 = ((val >> 16) & 0xFF);
byte Byte1 = ((val >> 24) & 0xFF);
EEPROM.write(addr, Byte4);
EEPROM.write(addr + 1, Byte3);
EEPROM.write(addr + 2, Byte2);
EEPROM.write(addr + 3, Byte1);
EEPROM.commit();
}
void saveOneCycle(uint8_t i) {
saveLong(ADDR_START_TIME[i], LogCycle[i].StartTime);
saveLong(ADDR_END_TIME[i], LogCycle[i].EndTime);
for(uint8_t x=0; x<5; x++)
saveString(ADDR_SYMPTOMS[i][x], LogCycle[i].symptoms[x]);
EEPROM.commit();
}
void saveAllCycles() {
for(uint8_t i=0; i<5; i++) {
saveLong(ADDR_START_TIME[i], LogCycle[i].StartTime);
saveLong(ADDR_END_TIME[i], LogCycle[i].EndTime);
for(uint8_t x=0; x<5; x++)
saveString(ADDR_SYMPTOMS[i][x], LogCycle[i].symptoms[x]);
}
EEPROM.commit();
}
I wrote code to switch color on a LED strip depending on a value that I set (code below).
When I tried to load the same code on a NodeMCU ESP8266, this code doesn't work anymore. I used the library Adafruit Neopixel, which I thought is supported by the mentioned board.
Any help and/or advice is really welcome.
#include <Adafruit_NeoPixel.h>
//#define ATTINY
#define DIMCOLORE 3
#define NUMCOLORI (sizeof(colori) / ((DIMCOLORE)*sizeof(byte)))
#define DELTRANSIZIONE (tempoTransizione) / (numOfPixels)
#define SPENTO 0
#define ROSSO 1
#define VERDE 2
#define BLU 3
#define GIALLO 4
#define CIANO 5
#define MAGENTA 6
#define BIANCO 7
#define CELESTINO 8
const byte pinLuci = 4;
const byte numOfPixels = 51;
const int tresh_1 = 10; // white
const int tresh_2 = 20; // blue
const int tresh_3 = 30; // yellow
const int tresh_4 = 40; // red
const unsigned long tempoCritico = 5000;
const unsigned long tempoLampeggio = 300;
const unsigned long tempoTransizione = 270;
byte colori[][DIMCOLORE] = {
{ 0, 0, 0}, // 0 OF
{255, 0, 0}, // 1 ROSSO
{ 0, 255, 0}, // 2 VERDE
{ 0, 0, 255}, // 3 BLU
{255, 130, 0}, // 4 GIALLO
{ 0, 255, 255}, // 5 CIANO
{255, 0, 255}, // 6 MAGENTA
{255, 130, 50}, // 7 BIANCO
};
unsigned long inizioAlto = 0;
byte colore = BIANCO;
byte ultimoColore = colore;
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(numOfPixels, pinLuci, NEO_GRB + NEO_KHZ800);
void impostaColore(byte indiceColore) {
for (byte i = 0; i < numOfPixels; i++) pixels.setPixelColor(i, pixels.Color(colori[indiceColore][0], colori[indiceColore][1], colori[indiceColore][2]));
pixels.show();
}
void impostaColore(byte valR, byte valG, byte valB) {
for (byte i = 0; i < numOfPixels; i++) pixels.setPixelColor(i, pixels.Color(valR, valG, valB));
pixels.show();
}
void impostaSequenza(byte indiceColore) {
for (byte i = 0; i < numOfPixels; i++) {
pixels.setPixelColor(i, pixels.Color(colori[indiceColore][0], colori[indiceColore][1], colori[indiceColore][2]));
pixels.show();
delay(DELTRANSIZIONE);
}
}
void impostaSequenza(byte valR, byte valG, byte valB) {
for (byte i = 0; i < numOfPixels ; i++) {
pixels.setPixelColor(i, pixels.Color(valR, valG, valB));
pixels.show();
delay(DELTRANSIZIONE);
}
}
void lampeggia(byte indiceColore, byte volte, unsigned long tempoLamp) {
for (byte i = 0 ; i < volte ; i++) {
impostaColore(0);
delay(tempoLamp);
impostaColore(indiceColore);
delay(tempoLamp);
}
}
void setup() {
#ifndef ATTINY
Serial.begin(115200);
#endif
pixels.begin();
for (byte i = 0; i < 54; i++) {
pixels.setPixelColor(i, pixels.Color(colori[2][0], colori[2][1], colori[2][2]));
pixels.show();
}
for (byte j = 0 ; j <= NUMCOLORI ; j++) {
impostaColore(j);
delay(50);
}
impostaColore(BIANCO);
}
void loop() {
int value = 5;
#ifndef ATTINY
Serial.print(millis());
Serial.print('\t');
Serial.print(value);
#endif
if (value < tresh_1) {
#ifndef ATTINY
Serial.print(" 0 ");
#endif
inizioAlto = 0;
colore = BIANCO;
} else if ((value >= tresh_1) && (value < tresh_2) && (value < tresh_3) && (value < tresh_4)) {
#ifndef ATTINY
Serial.print(" BASSA ");
#endif
inizioAlto = 0;
colore = BLU;
} else if ((value >= tresh_2) && (value < tresh_3) && (value < tresh_4)) {
#ifndef ATTINY
Serial.print(" MEDIA ");
#endif
inizioAlto = 0;
colore = GIALLO;
} else if ((value >= tresh_3) && (value < tresh_4)) {
#ifndef ATTINY
Serial.print(" ALTA ");
#endif
inizioAlto = 0;
colore = ROSSO;
} else if (value >= tresh_4) {
#ifndef ATTINY
Serial.print(" MAX ");
#endif
if (inizioAlto == 0) {
#ifndef ATTINY
Serial.print(" ----- ");
#endif
inizioAlto = millis();
colore = ROSSO;
} else if ((millis() - inizioAlto) > tempoCritico) {
#ifndef ATTINY
Serial.print(" +++++ ");
#endif
lampeggia(ROSSO, 1, tempoLampeggio / 2);
}
}
if (colore != ultimoColore) {
impostaSequenza(colore);
ultimoColore = colore;
}
#ifndef ATTINY
Serial.println();
#endif
delay(tempoLampeggio / 2);
}
The unique line I changed it is about the pin name:
const byte pinLuci = D5;
I attached some photos about the hardware connection I made, maybe the problem could be there.
I want to make clear that with Arduino Uno everything works fine.
If the error is that i does not compile then post the error from the console.
Else it is properly the wrong pin you are using.
The pin numbers and GPIO numbers isn't the same.
If you want to use GPIO04 you should use pin D2 in your sketch. See the following pin mapping
static const uint8_t D0 = 16;
static const uint8_t D1 = 5;
static const uint8_t D2 = 4;
static const uint8_t D3 = 0;
static const uint8_t D4 = 2;
static const uint8_t D5 = 14;
static const uint8_t D6 = 12;
static const uint8_t D7 = 13;
static const uint8_t D8 = 15;
static const uint8_t D9 = 3;
static const uint8_t D10 = 1;
I have tried several different methods found around the internet, however none of them seem to work. This code works for cases 0-2 but when it goes into case 3 which is the rainbow chase loop, the pressing of the button does not interrupt the loop and move the counter forward. I'm assuming, as usual, I'm missing something stupid so many thanks in advance.
#define FASTLED_ALLOW_INTERRUPTS 1
#define FASTLED_INTERRUPT_RETRY_COUNT 1
#include <FastLED.h>
#define AnalogIn A0
#define SwIn 2
#define LED_Out 12
#define NUM_LEDS 5
int pushCounterz = 0;
volatile int buttonState; // Set volatile for interrupt DO NOT SHAKE!
int lastButtonState;
CRGB leds[NUM_LEDS];
void setup() {
// put your setup code here, to run once:
FastLED.setMaxRefreshRate(250);
FastLED.addLeds<WS2812, LED_Out, GRB>(leds, NUM_LEDS);
pinMode(SwIn, INPUT);
pinMode(LED_Out, OUTPUT);
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB ( 255, 0, 255 );
}
FastLED.show();
delay(120);
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB ( 0, 0, 0 );
}
FastLED.show();
Serial.begin(115200);
Serial.println(pushCounterz);
lastButtonState = digitalRead(SwIn); // Set the button state to the startup state
attachInterrupt((SwIn-2), button_ISR, CHANGE); // Set SwIn button as an interrupt pin // Change to Low???
}
void loop() {
if (pushCounterz != 3) {
FastLED.show();
}
Serial.println(pushCounterz);
delay(120);
}
void button_ISR () {
buttonState = digitalRead(SwIn);
digitalWrite(13, buttonState);
if (buttonState == LOW && buttonState != lastButtonState) {
if (pushCounterz > 3) {
//Serial.println("Reset to 0: ");
pushCounterz = 0;
} else {
pushCounterz = pushCounterz + 1;
//Serial.println("Incerment");
}
//Serial.println(pushCounterz);
switch (pushCounterz) {
case 0:
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB (255, 0, 0);
}
break;
case 1:
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB ( 0, 255, 0);
}
break;
case 2:
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB ( 0, 0, 255);
}
break;
case 3:
theaterChaseRainbow(1,50);
break;
default:
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB ( 0, 0, 0);
}
break;
}
}
lastButtonState = buttonState;
}
// Theater-style crawling lights with rainbow effect
void theaterChaseRainbow(int cycles, int speed) { // TODO direction, duration
for (int j = 0; j < 256 * cycles; j++) { // cycle all 256 colors in the wheel
for (int q = 0; q < 3; q++) {
for (int i = 0; i < NUM_LEDS; i = i + 3) {
int pos = i + q;
leds[pos] = Wheel( (i + j) % 255); //turn every third pixel on
}
FastLED.show();
delay(speed);
for (int i = 0; i < NUM_LEDS; i = i + 3) {
leds[i + q] = CRGB::Black; //turn every third pixel off
}
}
}
}
CRGB Wheel(byte WheelPos) {
if (WheelPos < 85) {
return CRGB(WheelPos * 3, 255 - WheelPos * 3, 0);
}
else if (WheelPos < 170) {
WheelPos -= 85;
return CRGB(255 - WheelPos * 3, 0, WheelPos * 3);
}
else {
WheelPos -= 170;
return CRGB(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
Your issue is not that the button is not changing the value, but rather your code has no exit point if it does; the button will change the value, but nothing in theaterChaseRainbow tells it to stop.
Simply add a check in the method to return out if the button state changes:
// Theater-style crawling lights with rainbow effect
void theaterChaseRainbow(int cycles, int speed) { // TODO direction, duration
for (int j = 0; j < 256 * cycles; j++) { // cycle all 256 colors in the wheel
for (int q = 0; q < 3; q++) {
for (int i = 0; i < NUM_LEDS; i = i + 3) {
int pos = i + q;
leds[pos] = Wheel( (i + j) % 255); //turn every third pixel on
}
FastLED.show();
if (pushCounterz != 3) return; //ADDED THIS HERE*****
delay(speed);
for (int i = 0; i < NUM_LEDS; i = i + 3) {
leds[i + q] = CRGB::Black; //turn every third pixel off
}
}
}
}
In addition, I would suggest simplifying your ISR to just increment the button and not have it handle the logic of the program as well. That should either be contained in the loop method or called from the loop method. This should make for some cleaner and less confusing code, as the ISR's job is simply to adjust the value of the button counter, and the loops job is to deal with the state that the program is currently in.
Also - you can't allow interrupts on AVR, or rather I should say it does nothing. I should put in a warning message when that's happening - AVR/arduino's ISR handling is so slow that even the clock tick ISR would be enough to disrupt writing out WS2812 data (resulting in FastLED cutting the frame off) so I yanked that code out of the avr WS2812 asm implementation. Most arm and esp platforms that FastLED supports do allow for interrupt handling to occur during in the small window between writing out each led's data - courtesy of their higher clock speeds.
If you're using an ARM or ESP based platform, then feel free to ignore this comment (mostly putting it here so folks who stumble on this question in a good search know what's up).
As a reference, the working code with the ISR cleanup. (mind you there is still some serial debugging code in there as I have more work to do with brightness etc)
#define FASTLED_ALLOW_INTERRUPTS 1
#define FASTLED_INTERRUPT_RETRY_COUNT 1
#include <FastLED.h>
#define AnalogIn A0
#define SwIn 2
#define LED_Out 12
#define NUM_LEDS 5
int pushCounterz = 4; // 4 = off
volatile int buttonState; // Set volatile for interrupt DO NOT SHAKE!
int lastButtonState;
CRGB leds[NUM_LEDS];
void setup() {
// put your setup code here, to run once:
FastLED.setMaxRefreshRate(250);
FastLED.addLeds<WS2812, LED_Out, GRB>(leds, NUM_LEDS);
pinMode(SwIn, INPUT);
pinMode(LED_Out, OUTPUT);
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB ( 255, 0, 255 );
}
FastLED.show();
delay(120);
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB ( 0, 0, 0 );
}
FastLED.show();
Serial.begin(19200);
Serial.println(pushCounterz);
lastButtonState = digitalRead(SwIn); // Set the button state to the startup state
attachInterrupt((SwIn-2), button_ISR, LOW); // Set SwIn button as an interrupt pin // Change to Low???
}
void loop() {
// if (pushCounterz != 3) {
//FastLED.show();
//Serial.println(pushCounterz);
// }
//delay(20);
switch (pushCounterz) {
case 0:
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB (255, 0, 0);
}
FastLED.show();
break;
case 1:
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB ( 0, 255, 0);
}
FastLED.show();
break;
case 2:
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB ( 0, 0, 255);
}
FastLED.show();
break;
case 3:
theaterChaseRainbow(1,50);
break;
default:
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB ( 0, 0, 0);
}
FastLED.show();
break;
}
}
void button_ISR () {
buttonState = digitalRead(SwIn);
//digitalWrite(13, buttonState);
if (buttonState == LOW && buttonState != lastButtonState) {
if (pushCounterz > 3 || pushCounterz < 0) {
Serial.println("Reset to 0: ");
pushCounterz = 0;
} else {
pushCounterz = pushCounterz + 1;
Serial.println("Incerment");
}
Serial.println(pushCounterz);
}
lastButtonState = buttonState;
}
// Theater-style crawling lights with rainbow effect
void theaterChaseRainbow(int cycles, int speed) { // TODO direction, duration
for (int j = 0; j < 256 * cycles; j++) { // cycle all 256 colors in the wheel
for (int q = 0; q < 3; q++) {
for (int i = 0; i < NUM_LEDS; i = i + 3) {
int pos = i + q;
leds[pos] = Wheel( (i + j) % 255); //turn every third pixel on
}
FastLED.show();
if (pushCounterz != 3) return;
delay(speed);
for (int i = 0; i < NUM_LEDS; i = i + 3) {
leds[i + q] = CRGB::Black; //turn every third pixel off
}
}
}
}
CRGB Wheel(byte WheelPos) {
if (WheelPos < 85) {
return CRGB(WheelPos * 3, 255 - WheelPos * 3, 0);
}
else if (WheelPos < 170) {
WheelPos -= 85;
return CRGB(255 - WheelPos * 3, 0, WheelPos * 3);
}
else {
WheelPos -= 170;
return CRGB(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
I am trying to control a few (8 for now) servo motors using this 16-channel board. I am running to some issues about accuracy, for example, when moving a couple of motors do draw a diagonal line, because of the delay between each servo, each motor will move in different timing resulting in incorrect drawings.
I am not sure about how to drive the motors in the fastest way in therms of code.
Where to set delays, the baud rate settings for this application, etc. I couldn't find a good example using all channels with minimum delay. In my case, messages are coming from serial, as explained in the code comment.
Is this the right way to drive this board channels?
I am using an arduino uno, but I would like to check if using a Teensy 3.2 results in best performances for this application.
Thanks in advance for any suggestions.
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
//#define SERVOMIN 150
//#define SERVOMAX 600
// temporary setting pins for 4 lights - it will be controlled by some decade counter...
//#define L1 4
//#define L2 7
//#define L3 8
//#define L4 10
#define L1 9
#define L2 10
#define L3 11
#define L4 12
/*
* a "pointer" device includes a light and 2 servos. Parameters from serial are:
* index,light,servo1,servo2; <- parameters separated by ',' end of pointer is ';'
*
* example of how serial is coming containing instructions for 4 pointers;
0,0,180,180;1,0,0,0;2,0,180,180;3,0,0,0;
0,0,90,90;1,0,90,90;2,0,90,90;3,0,90,90;
**most of the time these instructions doesn't come all for 4 pointers.
ex:
1,0,12,12;4,255,100,100;
**sometimes it comes only id and light parameter.
0,255;1,0;
(instructions only to turn light on/off)
*/
//values for 8 servos:
const uint8_t SERVOMIN[] = {150, 130, 150, 130, 150, 130, 150, 130};
const uint8_t SERVOMAX[] = {600, 500, 600, 500, 600, 500, 600, 500};
//boards (for now, only one board = 16 servos)
Adafruit_PWMServoDriver pwm [] = {
Adafruit_PWMServoDriver(0x40)
};
uint8_t servonum = 0;
uint8_t activeServos = 4; //not being used now
char buf [4]; //maybe too long
uint16_t currentPointer [4]; //index//light//servo1//servo2
byte lightPin [4] = {L1, L2, L3, L4};
uint8_t lightstatus [4] = {0, 0, 0, 0};
//debug
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
boolean feedback = false;
void setup() {
//temporally as digital outputs
pinMode(L1, OUTPUT);
pinMode(L2, OUTPUT);
pinMode(L3, OUTPUT);
pinMode(L4, OUTPUT);
Serial.begin(115200);//230400 //115200 //57600 //38400 ?
for ( uint8_t i = 0; i < sizeof(pwm); i++) {
pwm[i].begin();
pwm[i].setPWMFreq(60);
}
}
void loop() {
reply();
}
void reply() {
if (stringComplete) {
if (feedback) Serial.println(inputString);
// clear the string:
inputString = "";
stringComplete = false;
for ( int i = 0; i < sizeof(buf); ++i ) buf[i] = (char)0;
}
}
void serialEvent() {
static byte ndx = 0;
static int s = 0;
while (Serial.available()) {
char rc = (char)Serial.read();
inputString += rc;
//(2) setting pointer parameter
if ( rc == ',') {
setPointer(s);
s++;
for ( int i = 0; i < sizeof(buf); ++i ) buf[i] = (char)0;
ndx = 0;
}
//(3) end of this pointer instruction
else if (rc == ';') {
setPointer(s);
//executePointer(); //plan B
ndx = 0;
s = 0;
for ( int i = 0; i < sizeof(buf); ++i ) buf[i] = (char)0;
}
//(4) end of command line
else if (rc == '\n') {
//p = 0;
s = 0;
stringComplete = true;
}
//(1) buffering
else {
buf[ndx] = rc;
ndx++;
}
}
}
void setPointer(int s) {
//index//light//servo1//servo2
int value;
value = atoi(buf);
//index
if (s == 0) {
if (feedback) {
Serial.print("index:");
Serial.print(value);
Serial.print(", buf:");
Serial.println(buf);
}
currentPointer[0] = value;
}
//light
else if (s == 1) {
int index = currentPointer[0];
currentPointer[s] = value;
//Serial.println(index);
digitalWrite(lightPin[index], (value > 0) ? HIGH : LOW);
// analogWrite( lightPin[currentPointer[0]], currentPointer[1]); // implement later
if (feedback) {
Serial.print("light: ");
Serial.println(value);
}
//servos
} else {
int index = currentPointer[0];
if (feedback) {
Serial.print("servo ");
Serial.print(index * 2 + s - 2);
Serial.print(": ");
Serial.println(value);
}
uint16_t pulselen = map(value, 0, 180, SERVOMIN[index], SERVOMAX[index]);
currentPointer[s] = pulselen;
pwm[0].setPWM(index * 2 + (s - 2), 0, pulselen); //current pointer id * 2 + s (s is 2 or 3)
//delay(20);
}
}
// this was plan B - not using
void executePointer() {
int index = currentPointer[0];
analogWrite( lightPin[index], currentPointer[1]);
pwm[0].setPWM(index * 2, 0, currentPointer[2]);
pwm[0].setPWM(index * 2 + 1, 0, currentPointer[3]);
delay(20);
}
I've built an arduino sketch that attempts to do a couple of different lengths of windowing and some simple calculations (mean/variance) on the analog values from a couple of sensors. Previously I had the same code for 1 sensor working as intended but the code below has been expanded with a bunch of little for loops so that everything should run for both sensors now.
I simply can't get anything at all to print to serial - even the two serial prints i put in setup and start of loop just to debug - yet the code compiles and uploads without any errors or warnings.
I apologise for including the whole sketch, I couldn't think how to break it apart to show.
long int currentTime = 0;
long int stopTime[2] = {0,0};
long int shortWindowTime = 0;
int shortVal[2][40];
int reflexWindowStart = 0;
int reflexWindowTime = 0;
int reflexVal[2][500];
int mean[2] = {0,0};
unsigned int variance[2] = {0,0};
int lowVal[2] = {0,0};
int peakVal[2] = {0,0};
int lowIndex[2] = {0,0};
int peakIndex[2] = {0,0};
int stopIndex[2] = {0,0};
boolean stopped[2] = {false,false};
void setup(){
Serial.begin(9600);
Serial.println("wtf?");
for(int i=0;i<2;i++){
for(int j=0;j<40;j++){
shortVal[i][j] = 0;
}
for(int j=0;j<500;j++){
reflexVal[i][j] = 1023;
}
}
}
void loop() {
Serial.println("wtf?");
currentTime = micros();
if(currentTime - shortWindowTime > 500){
shortWindowTime = currentTime;
writeShortWindow();
meanVariance();
if(reflexWindowStart == 0){
reflexWindow();
}
reflexWindowStart++;
if(reflexWindowStart > 9){
reflexWindowStart = 0;
}
}
}
void writeShortWindow(){
for(int i=0;i<2;i++){
for(int j=39; j>0; j--){
shortVal[i][j] = shortVal[i][j-1];
}
int ground = analogRead(A5);
shortVal[0][0] = analogRead(A1);
analogRead(A5);
shortVal[i][0] = analogRead(A2);
}
}
void meanVariance(){
for(int i=0;i<2;i++){
for(int j=0; j<39; j++){
mean[i] = mean[i] + shortVal[i][j];
}
mean[i] = mean[i] / 40;
for(int j=0; j<39; j++){
variance[i] = variance[i] + sq(mean[i] - shortVal[i][j]) ;
}
variance[i] = variance[i] / 40;
}
}
void reflexWindow(){
for(int i=0;i<2;i++){
if(stopped[i] == true){
if((millis() - stopTime[i] > 20) && (peakVal[i] - shortVal[i][0] > 20) && (variance[i] <= 1)){
stopped[i] = false;
stopIndex[i] = 0;
Serial.println("................................NOTstopped");
}
}
}
for(int i=0;i<2;i++){
if(stopped[i] == false){
lowVal[i] = 1023;
peakVal[i] = 0;
for(int j=stopIndex[i]; j>0; j--){
reflexVal[i][j] = reflexVal[i][j-1];
if(reflexVal[i][j] < lowVal[i]){
lowVal[i] = reflexVal[i][j];
lowIndex[i] = j;
}
}
reflexVal[i][0] = shortVal[i][0];
for(int j=lowIndex[i]; j>=0; j--){
if(reflexVal[i][j] > peakVal[i]){
peakVal[i] = reflexVal[i][j];
}
}
}
}
for(int i=0;i<2;i++){
if(stopped[i] == false){
if(peakVal[i] - lowVal[i] >= 50){
Serial.print(i);
Serial.println("...................................stopped");
stopTime[i] = millis();
stopped[i] = true;
}
}
}
for(int i=0;i<2;i++){
if(stopIndex[i] < 499){
stopIndex[i]++;
}
}
Serial.print(shortVal[0][0]);
Serial.print(" ... ");
Serial.print(lowVal[0]);
Serial.print(" ... ");
Serial.print(peakVal[0]);
Serial.print(" ........ ");
Serial.print(shortVal[1][0]);
Serial.print(" ... ");
Serial.print(lowVal[1]);
Serial.print(" ... ");
Serial.println(peakVal[1]);
}
If you have a Leonardo board you will most likely not see the Serial.print in the setup function.
Try changing your setup to this (notice the extra while loop waiting for the Serial)
void setup(){
Serial.begin(9600);
while (!Serial);
Serial.println("wtf?");
for(int i=0;i<2;i++){
for(int j=0;j<40;j++){
shortVal[i][j] = 0;
}
for(int j=0;j<500;j++){
reflexVal[i][j] = 1023;
}
}
}
The reason behind this you can read in the Arduino docs for Leonardo http://arduino.cc/en/Guide/ArduinoLeonardo#toc3 but in short is that Leonardo doesnt resets the serial port when opening the serial stream.
Have you checked your BAUD rate in serial monitor, because if it is different, it will not show anything.
be sure the one in serial monitor and in Serial.begin(<BAUD rate here>) is the same